gpt4 book ai didi

flutter - 错误 : Could not find the correct Provider above this widget Flutter [PropertiesGrid]

转载 作者:行者123 更新时间:2023-12-03 02:55:05 24 4
gpt4 key购买 nike

我想调用一个简单的 GridWiget在我的主页里面

这是 MyHomePage StatefulWidget :

import './app_theme.dart';
import 'package:flutter/material.dart';
import './widgets/properties_grid.dart';

class MyHomePage extends StatefulWidget {
const MyHomePage({Key key}) : super(key: key);

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
var _showOnlyFavorites = false;
AnimationController animationController;
bool multiple = true;

@override
void initState() {
animationController = AnimationController(
duration: const Duration(milliseconds: 2000), vsync: this);
super.initState();
}

Future<bool> getData() async {
await Future<dynamic>.delayed(const Duration(milliseconds: 0));
return true;
}

@override
void dispose() {
animationController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppTheme.white,
body: FutureBuilder<bool>(
future: getData(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
} else {
return Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
appBar(),
Expanded(
child: FutureBuilder<bool>(
future: getData(),
builder:
(BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
} else {
return PropertiesGrid(_showOnlyFavorites);
}
},
),
),
],
),
);
}
},
),
);
}

Widget appBar() {
return SizedBox(
height: AppBar().preferredSize.height,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8, left: 8),
child: Container(
width: AppBar().preferredSize.height - 8,
height: AppBar().preferredSize.height - 8,
),
),
Expanded(
child: Center(
child: Padding(
padding: const EdgeInsets.only(top: 4),
child:
Image.asset('assets/images/logo.png', fit: BoxFit.contain),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 8, right: 8),
child: Container(
width: AppBar().preferredSize.height - 8,
height: AppBar().preferredSize.height - 8,
color: Colors.white,
child: Material(
color: Colors.transparent,
child: InkWell(
borderRadius:
BorderRadius.circular(AppBar().preferredSize.height),
child: Icon(
multiple ? Icons.dashboard : Icons.view_agenda,
color: AppTheme.dark_grey,
),
onTap: () {
setState(() {
multiple = !multiple;
});
},
),
),
),
),
],
),
);
}
}


这是 PropertiesGrid StatelessWidget :
import 'package:aradi_online_vtwo/providers/properties.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../providers/properties.dart';
import './property_item.dart';

class PropertiesGrid extends StatelessWidget {
final bool showFavs;

PropertiesGrid(this.showFavs);

@override
Widget build(BuildContext context) {
final productsData = Provider.of<Properties>(context);
final products = showFavs ? productsData.favoriteItems : productsData.items;
return GridView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: products.length,
itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
value: products[i],
child: PropertyItem(
),
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
childAspectRatio: 3 / 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
),
);
}
}


和假人 provider数据如下所示:
import 'package:flutter/material.dart';
import './property.dart';

class Properties with ChangeNotifier {
List<Property> _items = [
Property(
id: 'p1',
purpose: '',
title: 'House Title sdfsdffdgdfgdfs',
),
Property(
id: 'p3',
purpose: '',
title: 'land title',
),
];

List<Property> get items {
return [..._items];
}

List<Property> get favoriteItems {
return _items.where((propItem) => propItem.isFavorite).toList();
}

Property findById(String id) {
return _items.firstWhere((prop) => prop.id == id);
}

void addProperty(Property property) {
final newProperty = Property(
id: DateTime.now().toString(),
purpose: property.purpose,
title: property.title,
);
_items.add(newProperty);
notifyListeners();
}

void updateProperty(String id, Property newProperty) {
final propIndex = _items.lastIndexWhere((prop) => prop.id == id);
if (propIndex >= 0) {
_items[propIndex] = newProperty;
notifyListeners();
} else {
print('...');
}
}

void deleteProperty(String id) {
_items.removeWhere((prop) => prop.id == id);
notifyListeners();
}
}


GridItem是下面的代码:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../providers/property.dart';

class PropertyItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
final property = Provider.of<Property>(context, listen: false);
// final cart = Provider.of<Cart>(context, listen: false);
return InkWell(
onTap: () => {},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
elevation: 7,
margin: EdgeInsets.all(2),
child: Stack(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15),
topRight: Radius.circular(15),
),
// color: Colors.transparent,
child: Image.asset(
property.image,
fit: BoxFit.fill,
),
),
Positioned(
top: 8,
right: 8,
child: Consumer<Property>(
builder: (ctx, property, _) => IconButton(
icon: Icon(
property.isFavorite ? Icons.favorite : Icons.favorite_border,
),
color: Colors.red,
onPressed: () {
property.toggleFavoriteStatus();
},
),
),
),
Positioned(
right: 20,
top: 100,
child: Container(
width: 300,
color: Colors.black54,
padding: EdgeInsets.symmetric(
vertical: 5,
horizontal: 20,
),
child: Text(
property.title,
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
softWrap: true,
overflow: TextOverflow.fade,
),
),
)
],
),
),
);
}
}


这是 ModelProvider :
import 'package:flutter/foundation.dart';

class Property with ChangeNotifier {
final String id;
final String purpose;
final String title;
bool isFavorite;

Property({
@required this.id,
@required this.purpose,
@required this.title,
});

void toggleFavoriteStatus() {
isFavorite = !isFavorite;
notifyListeners();
}
}


作为这部分的错误:
return PropertiesGrid(_showOnlyFavorites);

我希望有人可以帮助我发布所有信息可能是...

最佳答案

您必须添加一个更改通知程序提供程序作为父级,以便从后代小部件访问属性模型。

ChangeNotifierProvider(
create: (context) => Property(),
child: PropertiesGrid(_showOnlyFavorites),
),

现在您可以使用 Provider.of<Property>(context, listen: false); 访问属性更改通知程序模型或使用 Consumer .

关于flutter - 错误 : Could not find the correct Provider above this widget Flutter [PropertiesGrid],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62116714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com