RenderFlex子级具有非零的flex,但是传入的宽度限制是无界的

时间:2020-07-15 16:50:58

标签: android ios flutter flutter-layout flutter-test

我正在扑朔迷离地为我的新闻项目编写代码。我已从一个教程中复制了此代码。但是我的代码中有异常和错误。有人请帮助我解决问题。我的代码是

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(97),
          child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(top: 32),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      border: Border(
                          bottom: BorderSide(
                        width: 0.5,
                        color: Colors.white,
                      ))),
                  child: AppBar(
                    title: Text(
                      'News',
                      style: TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.bold,
                        fontSize: 30,
                      ),
                    ),
                  ),
                  // height: 100,
                )
              ]),
        ),
        body: ListView.builder(
          itemBuilder: (context, index) {
            return _listItem(index);
          },
          itemCount: _newsInApp.length,
        ));
  }

  

我的控制台输出是:

Launching lib/main.dart on iPhone 11 Pro Max in debug mode...
Running Xcode build...
Xcode build done.                                           22.9s
Syncing files to device iPhone 11 Pro Max...
flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during performLayout():
flutter: RenderFlex children have non-zero flex but incoming width constraints are unbounded.

1 个答案:

答案 0 :(得分:1)

将您的listItem小部件方法替换为:

_listItem(index) {
  return Padding(
    padding: const EdgeInsets.only(left: 15, top: 1, right: 1, bottom: 1),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Expanded(
          child: Padding(
            padding: const EdgeInsets.only(bottom: 15),
            child: Text(
              _newsInApp[index].title,
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
          ),
        ),
        IconButton(
          iconSize: 16,
          color: Colors.black26,
          icon: Icon(Icons.arrow_forward_ios),
          onPressed: () => Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) {},
            ),
          ),
        ),
      ],
    ),
  );
}