【问题标题】:ListView widget isn't showing on my screen and not working properly (in Flutter)ListView 小部件未显示在我的屏幕上并且无法正常工作(在 Flutter 中)
【发布时间】:2020-08-06 09:08:27
【问题描述】:

我想在我的应用程序中有一个 ListView,但是当我运行它时出现错误并且它没有显示(而且我的自定义小部件也没有显示):

Exception caught by rendering library ═════════════════════════════════
RenderBox was not laid out: RenderRepaintBoundary#2aa9b relayoutBoundary=up5 NEEDS-PAINT
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1687 pos 12: 'hasSize'
The relevant error-causing widget was
Column

当我删除该列时,我得到了同样的错误,但相关的导致错误的小部件是 ListView。 有人知道如何解决吗? (我是 Flutter 的新手,我不知道具体应该如何工作)。

这是我的代码:(我从 Flutter 文档中获取了 ListView 代码。我会在它工作时更改它)

class DashboardPage extends StatefulWidget {
  @override
  _DashboardPageState createState() => _DashboardPageState();
}

class _DashboardPageState extends State<DashboardPage> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
        children: <Widget>[
          CustomAppBar('Dashboard'),
          ListView(
            padding: const EdgeInsets.all(8),
            children: <Widget>[
              Container(
                height: 50,
                color: Colors.amber[600],
                child: const Center(child: Text('Entry A')),
              ),
              Container(
                height: 50,
                color: Colors.amber[500],
                child: const Center(child: Text('Entry B')),
              ),
              Container(
                height: 50,
                color: Colors.amber[100],
                child: const Center(child: Text('Entry C')),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

CustomAppBar 是我制作的自定义小部件,没有 ListView 也能正常工作。

编辑:截图: screenshot of what I see on the emulator

编辑:来自我的自定义小部件的代码:

class CustomAppBar extends StatelessWidget {

  CustomAppBar(this.title);

  String title = '';

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: MediaQuery.of(context).size.height * 0.30,
      width: double.infinity,
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.vertical(
            bottom: Radius.elliptical(MediaQuery.of(context).size.width, 60.0)
          ),
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [const Color(0XFF3632EA), const Color(0XFF2A27C1)]
          ),
        ),
        child: Container(
          alignment: Alignment.bottomLeft,
          padding: EdgeInsets.only(bottom: 50.0, left: 30.0, right: 30.0),
          child: Text(
            title,
            style: Theme.of(context).textTheme.headline,
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 在此处添加CustomAppBar小部件的代码。
  • 我添加了代码

标签: listview flutter flutter-layout


【解决方案1】:

将 listView 的 shrinkWrap 设置为 true。它会解决你的问题。

 ListView(
              shrinkWrap: true,
              padding: const EdgeInsets.all(8),

【讨论】:

  • 它显示正确但不会滚动。我用文本(文本),文本(文本)替换了孩子,文本只是一些 lorem ipsum 段落。它说:底部溢出 X 像素
  • 你能用截图更新你的代码吗?我不明白你想说什么。
猜你喜欢
  • 2019-06-01
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 2021-10-30
  • 2020-06-29
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多