【问题标题】:how to distribute space between multiple items如何在多个项目之间分配空间
【发布时间】:2018-11-25 14:26:23
【问题描述】:

我正在尝试添加一个覆盖屏幕 前 20% 的图像,而其他 80% 应该是卡片网格。图片需要在正文中,并且不在应用栏上。我制作了卡片网格,然后尝试将图像和网格放在一个列中。实现如下。

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        primary: true,
        appBar: AppBar(
        elevation: 4.0,
        backgroundColor: Color(0xfff8f8f8),
        title: Center(child: titleLogo,),
        ),
  //------------ PROBLEM BELOW ----------------
        body: new Column(
          children: <Widget>[
            titleLogo,   //Image object
            TheGridView().build()  //Returns a GridView object
            ],
      ),
  ),
);

}

我收到以下错误

I/flutter (21751): The following assertion was thrown during performResize():
I/flutter (21751): Vertical viewport was given unbounded height.
I/flutter (21751): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter (21751): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter (21751): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter (21751): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter (21751): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter (21751): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter (21751): the height of the viewport to the sum of the heights of its children.

非常感谢任何建议。提前谢谢你。

【问题讨论】:

  • 您能解释一下TheGridView().build 的作用吗?对我来说似乎很不习惯
  • TheGridView() 是一个类,TheGridView.build() 返回一个 GridView 对象,其每个网格元素都是一个 card 对象。

标签: dart flutter flutter-layout


【解决方案1】:

要在多个项目之间分配空间,您应该像这样使用Expanded 小部件:

return new Column(
  children: <Widget>[
    new Expanded(
      flex: 2,
      child: new Container(
        color: Colors.red,
      ),
    ),
    new Expanded(
      flex: 8,
      child: new Container(//use your Gridview instead 
        color: Colors.green,
      )
    )
  ],

); 

【讨论】:

  • 非常感谢! @Raouf Rahiche。这解决了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 2022-01-18
  • 2017-06-13
相关资源
最近更新 更多