android - 如何通过创建一个由3个容器(左,中,右部分)组成的单独的ListView来避免无限的高度声明

标签 android flutter dart

我正在尝试创建一个单独的Listview,如下图所示。每个ListView-elemet都应包含3个容器(左侧部分,中间部分,右侧部分)。有一个应用程序按钮,它将在按下时创建一个新的Listview-Element。
enter image description here

我已经成功创建了listview和容器,但是收到以下异常:rendering呈现库捕获到异常═════════════════════ ════════════════════════════════
在performLayout()期间引发了以下断言:
BoxConstraints强制无限高。

这是我的代码:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: ToDo()));

class ToDo extends StatelessWidget {
  // This widget is the root of your application.
  @override
  final List<String> list=[ 'x', 'y', 'y', 'x'];

  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBar(
        title: Text('AppName',style: TextStyle(
            fontSize: 22.0,
            fontStyle: FontStyle.italic,
            fontWeight: FontWeight.w600,
            color: Colors.white
        ),

        ),leading: Icon(Icons.menu),  backgroundColor: Colors.redAccent,
        ),

        body: ListView.separated(
          separatorBuilder: (context, index)=> Divider(
            color: Colors.white,
          ),
          itemCount: eintraege.length,
          itemBuilder: (context, i){
        return new
        RowElement();
          },

    ),
    floatingActionButton: FloatingActionButton(
        onPressed: newEntry,
        child: Icon(Icons.add_circle_outline),
    )
    );

  }
}


class RowElementextends StatelessWidget {
  final leftSection = new Container();
  final middleSection = new Container();
  final rightSection = new Container();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Container(

          child: new Row(
            children: <Widget>[
              leftSection, middleSection, rightSection

            ],
          )
      ),
    );
  }
}
final leftSection = new Container(
  child: new CircleAvatar(
    //backgroundImage: new NetworkImage(url),
    backgroundColor: Colors.orangeAccent,
    radius: 24.0,
  ),
);

final middleSection = new Container(
  child: new Column(
    children: <Widget>[
      new Text("Name"),
      new Text("Hi whatsup?"),
    ],
  ),
);

final rightSection = new Container(

  child: new Column(
    mainAxisAlignment: MainAxisAlignment.spaceAround,
    children: <Widget>[
      new Text("9:50",
        style: new TextStyle(
            color: Colors.red,
            fontSize: 12.0),),
      new CircleAvatar(
        backgroundColor: Colors.orangeAccent,
        radius: 10.0,
        child: new Text("2",
          style: new TextStyle(color: Colors.white,
              fontSize: 12.0),),
      )
    ],
  ),
);

最佳答案

我不知道为什么要这么做,但是您不应该将小部件另存为类的字段,这不是一个好习惯。
这是一个可行的解决方案:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: ToDo()));

class ToDo extends StatelessWidget {
  // This widget is the root of your application.
  @override
  final List<String> list = ['x', 'y', 'y', 'x'];

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            'AppName',
            style: TextStyle(
                fontSize: 22.0,
                fontStyle: FontStyle.italic,
                fontWeight: FontWeight.w600,
                color: Colors.white),
          ),
          leading: Icon(Icons.menu),
          backgroundColor: Colors.redAccent,
        ),
        body: ListView.separated(
          separatorBuilder: (context, index) => Divider(
            color: Colors.white,
          ),
          itemCount: 10,//put your item count
          itemBuilder: (context, i) {
            return new RowElement();
          },
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {},//implement onPressed
          child: Icon(Icons.add_circle_outline),
        ));
  }
}

class RowElement extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        //left element
        Expanded(
          child: Container(
            child: new CircleAvatar(
              //backgroundImage: new NetworkImage(url),
              backgroundColor: Colors.orangeAccent,
              radius: 24.0,
            ),
          ),
        ),
        //middle element
        Expanded(
          child: Container(
            child: new Column(
              children: <Widget>[
                new Text("Name"),
                new Text("Hi whatsup?"),
              ],
            ),
          ),
        ),
        //right element
        Expanded(
          child: Container(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                new Text(
                  "9:50",
                  style: new TextStyle(color: Colors.red, fontSize: 12.0),
                ),
                new CircleAvatar(
                  backgroundColor: Colors.orangeAccent,
                  radius: 10.0,
                  child: new Text(
                    "2",
                    style: new TextStyle(color: Colors.white, fontSize: 12.0),
                  ),
                )
              ],
            ),
          ),
        )
      ],
    );
  }
}

关于android - 如何通过创建一个由3个容器(左,中,右部分)组成的单独的ListView来避免无限的高度声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61289056/

相关文章:

android - Chromecast 在尝试播放来自 DS Video 的电影后重新启动

android - 从动态创建的列表中动态删除小部件 flutter

android - 奥托和 Volley 在方向

flutter - 有没有办法自动滚动到 ListView.builder 中的元素?

generics - 通用类中的多个约束

Android 程序-收到 Skype 消息时有反应吗?

flutter - 代码是正确的,但容器没有移动

regex - 如何在WhitelistingTextInputFormatter的正则表达式中允许单引号?

android - Flutter - 无法发出 POST 请求

flutter - 尝试分配新值时,无法将元素类型 'int' 分配给映射值类型 'FieldValue'