首页 > 解决方案 > 如何在简单的代码中修复 ParentDataWidget 的错误使用

问题描述

我尝试解决不正确使用 ParentDataWidget

在此处输入图像描述

小部件库捕获的异常:

ParentDataWidget 的使用不正确。

    import 'package:flutter/material.dart';
    import 'package:user_profile_example/data.dart';
    
    import 'package:user_profile_example/widget/cookbook_item.dart';
    
    class bookmarked_recipes extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(children: [
            // Design a clickable container which has a row of icon and text.
            InkWell(
              child: Align(
                // Align them on the left of the screen.
                alignment: Alignment.centerLeft,
                child: Container(
                    margin: EdgeInsets.all(15),
                    child: Row(
                      children: [
                        Icon(Icons.add),
                        Text("Add new cookbook", style: TextStyle(fontSize: 16)),
                      ],
                    )),
              ),
              onTap: () {},
            ),
            // make the rest of the screen for the gridview items.
            Expanded(
              child: GridView.count(
                crossAxisCount: 2, // 2 items in each row
                padding: EdgeInsets.all(25),
                // map all available cookbooks and list them in Gridviwe.
                children: Cookbook_Data.map((c) => cookbook_item(
                      key,
                      c.id,
                      c.cookbookName,
                      c.imageURLCookbook,
                    )).toList(),
              ),
            ),
          ]),
        );
      }
    }

标签: flutter

解决方案


试试下面的代码希望它对你有帮助。尝试只需删除您的Expanded小部件将我的孩子更改为您的孩子,请参阅GridView.count() 此处

    Column(
        children: [
          InkWell(
            child: Align(
              // Align them on the left of the screen.
              alignment: Alignment.centerLeft,
              child: Container(
                margin: EdgeInsets.all(15),
                child: Row(
                  children: [
                    Icon(Icons.add),
                    Text(
                      "Add new cookbook",
                      style: TextStyle(fontSize: 16),
                    ),
                  ],
                ),
              ),
            ),
            onTap: () {},
          ),
          // make the rest of the sceen for the gridview items.
          GridView.count(
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            crossAxisCount: 2, // 2 items in each row
            padding: EdgeInsets.all(25),
            // map all available cookbooks and list them in Gridviwe.
            children: List.generate(
              10,
              (index) {
                return Center(
                  child: Text(
                    'Item $index',
                    style: Theme.of(context).textTheme.headline5,
                  ),
                );
              },
            ),
          ),
         ],
       ),

您的结果屏幕->在此处输入图像描述


推荐阅读