flutter - 断言失败:第 551 行 pos 12:'child.hasSize':不正确
问题描述
我是 Flutter 的新手,我正在开发一个电子商务网站,我想在我的欢迎页面中添加一个显示最近产品的 Grid,我正在尝试使用 GridVeiw.builder,但出现错误
断言失败:第 551 行第 12 行:'child.hasSize':不正确。
我不明白为什么我会看到这个错误。
我的欢迎屏幕
import 'package:ecommerce_practice/components/carousel.dart';
import 'package:ecommerce_practice/components/horizontal_list.dart';
import 'package:ecommerce_practice/components/products.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class WelcomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('E-commerce'),
backgroundColor: Colors.red,
actions: [
IconButton(
icon: Icon(
Icons.search,
),
color: Colors.white,
onPressed: () {},
),
IconButton(
icon: Icon(Icons.shopping_cart),
color: Colors.white,
onPressed: () {},
)
],
),
drawer: Drawer(
child: ListView(
children: [
UserAccountsDrawerHeader(
accountName: Text("Aleem"),
accountEmail: Text('aleem.alam@outlook.com'),
currentAccountPicture: GestureDetector(
child: CircleAvatar(
backgroundColor: Colors.grey,
child: Icon(
Icons.person,
color: Colors.white,
),
),
),
decoration: BoxDecoration(color: Colors.red),
),
DrawerMenuButton(
title: 'Home',
icon: Icon(
Icons.home,
color: Colors.red,
),
),
DrawerMenuButton(
title: 'My Account',
icon: Icon(
Icons.person,
color: Colors.red,
),
),
DrawerMenuButton(
title: 'My Order',
icon: Icon(
Icons.shopping_basket,
color: Colors.red,
),
),
DrawerMenuButton(
title: 'Category',
icon: Icon(
Icons.category,
color: Colors.red,
),
),
DrawerMenuButton(
title: 'Favourites',
icon: Icon(
Icons.favorite,
color: Colors.red,
),
),
Divider(),
DrawerMenuButton(
title: 'Settings',
icon: Icon(
Icons.settings,
color: Colors.blue,
),
),
DrawerMenuButton(
title: 'About Us',
icon: Icon(
Icons.help,
color: Colors.blue,
),
),
],
),
),
body: ListView(
children: [
ImageCarousel(),
Padding(
padding: EdgeInsets.all(10.0),
child: Text('Category'),
),
HorizontalList(),
Padding(
padding: EdgeInsets.all(10.0),
child: Text('Recent Product'),
),
Products(),
],
),
);
}
}
class DrawerMenuButton extends StatelessWidget {
DrawerMenuButton({this.title, this.icon});
final String title;
final Icon icon;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: ListTile(
title: Text(title),
leading: icon,
),
);
}
}
产品.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Products extends StatefulWidget {
@override
_ProductsState createState() => _ProductsState();
}
class _ProductsState extends State<Products> {
List<dynamic> products = [
{
'name': 'Shirt',
'image': 'images/category.jpg',
'old_price': 200,
'price': 140,
},
{
'name': 'Shirt',
'image': 'images/category.jpg',
'old_price': 200,
'price': 140,
}
];
@override
Widget build(BuildContext context) {
return GridView.builder(
itemCount: products.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemBuilder: (context, index) {
return Product(
name: products[index]['name'],
image: products[index]['image'],
oldPrice: products[index]['old_price'],
finalPrice: products[index]['price'],
);
},
);
}
}
class Product extends StatelessWidget {
Product({this.name, this.image, this.oldPrice, this.finalPrice});
final String name, image;
final int oldPrice, finalPrice;
@override
Widget build(BuildContext context) {
return Card(
child: Hero(
tag: name,
child: Material(
child: InkWell(
onTap: () {},
child: GridTile(
footer: Container(
color: Colors.white,
child: ListTile(
leading: Text(
name,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
),
child: Image.asset(
image,
fit: BoxFit.cover,
),
),
),
),
),
);
}
}
解决方案
尝试将shrinkWrap
属性设置为GridView
,true
以便它只占用基于它的项目所需的空间:
我以您的代码为例添加了一个演示:
return GridView.builder(
itemCount: products.length,
shrinkWrap: true, // new line
physics: NeverScrollableScrollPhysics(), // new line
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemBuilder: (context, index) {
return Product(
name: products[index]['name'],
image: products[index]['image'],
oldPrice: products[index]['old_price'],
finalPrice: products[index]['price'],
);
},
);
编辑:为了防止在您的中GridView
单独滚动ListView
,将 的 设置physics
为。GridView
NeverScrollablePhysics
推荐阅读
- php - 如何在 PHP 中根据匹配值和顺序比较两个数组?
- java - 如何用正则表达式替换组的内容
- css - 所有全局伪元素的样式,例如 :before?
- reactjs - “rollup-plugin-node-resolve”无法与 react 的“useState”一起使用,如何解决?
- php - 如何使用morris js为饼图的动态数据着色?
- php - Laravel eloquent orderby 相关模型的列总和(有很多关系)
- python - 使用 tf.data.Dataset 时如何解决 OOM 错误?
- xml - XSLT 是否支持 Clark 表示法?
- node.js - 在 node.js 中将 ini.txt 转换为 JSON
- sql - SQL Server:选择加入