ParentDataWidget 的使用不正确。Flutter

3

有人知道这个错误是什么吗?我认为某些子节点无法放置在某些东西里面?

接收不兼容父级数据的RenderObject的所有权链为: _GestureSemantics ← RawGestureDetector ← GestureDetector ← Expanded ← ColoredBox ← Container ← Padding ← ColoredBox ← ConstrainedBox ← Container ← ⋯

Container(
                        height: 35,
                        width: 150,
                        color: Colors.blueGrey,
                        child: Row(
                          children: [
                            Container(
                              width: 35,
                              height: double.infinity,
                              color: Colors.blue,
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Container(
                                  color: Colors.black,
                                  child: Expanded(
                                    child: GestureDetector(
                                      onTap: () {
                                        setState(() {
                                          intAmount = int.parse(amount);
                                          intAmount--;
                                          amount = intAmount.toString();
                                          print(intAmount);
                                        });
                                      },
                                      child: SizedBox(
                                        child: Image(
                                          image:
                                              AssetImage('images/plus.png'),
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            Expanded(
                              child: Container(
                                height: double.infinity,
                                color: Colors.black,
                                child: Center(
                                  child: GestureDetector(
                                    onTap: () {},
                                    child: Text(
                                      '$intAmount',
                                      style: TextStyle(
                                        color: Colors.amber,
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            Container(
                              width: 35,
                              height: double.infinity,
                              color: Colors.orange,
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Container(
                                  color: Colors.black,
                                  child: GestureDetector(
                                    onTap: () {
                                      setState(() {
                                        intAmount = int.parse(amount);
                                        intAmount++;
                                        amount = intAmount.toString();
                                        print(intAmount);
                                      });
                                      //*********************************** */
                                    },
                                    child: Expanded(
                                      child: SizedBox(
                                        child: Image(
                                          image: AssetImage(
                                              'images/minus.png'),
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      )

指出出现错误的那一行。 - Bensal
1个回答

1
我已经更新了你的代码。问题是你不能在子控件中使用Expand,这意味着一个Expanded widget必须是其父或祖先控件。
例如:
 Expanded(
      child: MyWidget(),
    ),

Row(
  children: [
    Expanded(
      child: MyWidget(),
    ),
    Expanded(
      child:Text("Text Widget"),
    ),
  ],
)

in Column

Column(
  children: [
    Expanded(
      child: MyWidget(),
    ),
    Expanded(
      child:Text("Text Widget"),
    ),
  ],
),

不要这样
Container(
  child: Expanded(
    child: MyWidget(),
  ),
)

你的更新代码。
Column(
    children: <Widget>[
      Container(
        height: 35,
        width: 150,
        color: Colors.blueGrey,
        child: Row(
          children: [
            Container(
              width: 35,
              height: double.infinity,
              color: Colors.blue,
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Container(
                  color: Colors.black,
                  child: GestureDetector(
                    onTap: () {
                     setState(() {
                        intAmount = int.parse(amount);
                        intAmount--;
                        amount = intAmount.toString();
                        print(intAmount);
                      });
                    },
                    child: SizedBox(
                      child: Image(
                        image: AssetImage('images/plus.png'),
                      ),
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              child: Container(
                height: double.infinity,
                color: Colors.black,
                child: Center(
                  child: GestureDetector(
                    onTap: () {},
                    child: Text(
                      '$intAmount',
                      style: TextStyle(
                        color: Colors.amber,
                      ),
                    ),
                  ),
                ),
              ),
            ),
            Container(
              width: 35,
              height: double.infinity,
              color: Colors.orange,
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Container(
                  color: Colors.black,
                  child: GestureDetector(
                    onTap: () {
                     setState(() {
                        intAmount = int.parse(amount);
                        intAmount++;
                        amount = intAmount.toString();
                        print(intAmount);
                      });
                    },
                    child: SizedBox(
                      child: Image(
                        image: AssetImage('images/minus.png'),
                      ),
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
      )
    ],
  ),

结账 扩展类


是的,也许你是对的,但是这段代码有一个问题,扩展小部件必须需要一个列小部件,否则它会崩溃。https://api.flutter.dev/flutter/widgets/Column-class.html - vb10
1
是的,你说得对,我的错。我已经更新了答案 @vb10 - Abhijith
谢谢你们的帮助,真的帮了我很大的忙。 - Genzo
兄弟,没问题 @GensoIGN - Abhijith

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