断言失败:第551行,第12个位置: 'child.hasSize'不为真

10

我是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,
      ),
    );
  }
}


products.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,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

HorizontalList是另一个可以水平滚动的ListView吗? - void
问题出现是因为您将GridView放在ListView内部。ListView/Gridview具有无限高度。这意味着您将一个无限大小的小部件推入另一个无限大小的小部件中,这是错误的。首先,您需要确定GridView的高度。但我认为这里可能会出现另一个问题。GridView可以滚动,ListView也可以滚动(嵌套滚动)。因此,我认为最好的解决方案是:您可以使用CustomscrollView或NestesScrollView代替ListView,并必须确定GridView的大小。 - darkness
1个回答

20
尝试将 GridViewshrinkWrap 属性设置为 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'],
      );
    },
  );

编辑:为了防止在你的 ListViewGridView 单独滚动,请将 GridViewphysics 设置为 NeverScrollablePhysics


1
如果我的回答解决了您在帖子中提到的问题,请接受并点赞。@Armaan - void
谢谢,shrinkWrap: true 也解决了我在 Listview.builder 中的问题。 - Antonin GAVREL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接