【问题标题】:Incorrect use of ParentDataWidget. FlutterParentDataWidget 的使用不正确。扑
【发布时间】:2020-11-13 18:56:02
【问题描述】:

Anyne 知道这个错误是什么吗?我认为有些孩子不能放在里面?

接收到不兼容父数据的 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'),
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      )

【问题讨论】:

  • 指出错误出现的行

标签: flutter


【解决方案1】:

我已经更新了你的代码,问题是你不能在孩子中使用 Expand,这意味着一个 Expanded 小部件必须是后代或父母

示例:

 Expanded(
      child: MyWidget(),
    ),

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

在列中

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'),
                      ),
                    ),
                  ),
                ),
              ),
            )
          ],
        ),
      )
    ],
  ),

结帐Expanded class

【讨论】:

  • 是的\也许你是对的,但是这段代码有问题,扩展的小部件必须需要一个列小部件,否则它会崩溃。 api.flutter.dev/flutter/widgets/Column-class.html
  • 是的,你是对的,我的错误我已经更新了答案@vb10
  • 谢谢你们的帮助,真的帮助了我
  • 没问题兄弟@GensoIGN
猜你喜欢
  • 2021-10-04
  • 2021-01-22
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 2020-11-18
  • 2021-04-14
  • 2022-06-12
  • 1970-01-01
相关资源
最近更新 更多