Flutter 2中的Column布局中使用StreamBuilder

4
我有两个streambuilder,里面都有一个ListView builder。我想将这两个streambuilder显示在一个可滚动的列中,但是我尝试使用Expanded或Flexible包装它们,但就是无法实现。问题在于使用expanded widget时,即使widget没有任何子项,它仍然占据了屏幕的很大一部分。例如,当streambuilder 1没有数据时,它仍然使用屏幕的一部分。 提前感谢您!
以下是我的代码简述:
SingleChildScrollView(
      child: Column(
        children: [
          Expanded(
            child: StreamBuilder(
              stream: stream1,
              builder: (context, snapshot) {
                return ListView.builder();
              },
            ),
          ),
          Expanded(
            child: StreamBuilder(
              stream: stream2,
              builder: (context, snapshot) {
                return ListView.builder();
              },
            ),
          ),
        ],
      ),
    ),

1
只需尝试移除 SingleChildScrollView - Ravi Singh Lodhi
我还需要一个替代expanded的方法(我编辑了问题以包括这个要求)。 - Dewaeq
1个回答

5
SingleChildScrollView(
  child: Column(
    mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      Expanded(
        child: StreamBuilder(
          stream: stream1,
          builder: (context, snapshot) {
            return  ListView(
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              children: firstList(),
            ),
          },
        ),
      ),
      Expanded(
        child: StreamBuilder(
          stream: stream2,
          builder: (context, snapshot) {
            return  ListView(
             physics: NeverScrollableScrollPhysics(),
             shrinkWrap: true,
             children: secondList(),
           ),
          },
        ),
      ),
    ],
  ),
)

你可以通过将ListView widget的physics属性设置为physics: NeverScrollableScrollPhysics()来使它永远不可滚动, 并且 使用shrinkWrap: true,你可以改变这个行为,让ListView只占用它需要的空间(当有更多的项时它仍然可以滚动)。 这将在单个可滚动区域中创建两个不同的StreamBuilder widget。


谢谢,最终我通过你的解决方案和将扩展替换为灵活的,并将适合度设置为松散,使其正常工作。 - Dewaeq
太棒了!我必须删除第一个Expanded,并将第二个widget从Expanded更改为Flexile。然后它就可以工作了。 - giorgio79

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