【问题标题】:Is there a way in flutter to generate Widgets in a List without using Listview , Gridview?有没有办法在不使用 Listview、Gridview 的情况下在 List 中生成小部件?
【发布时间】:2021-01-23 15:38:10
【问题描述】:

我想要做的是将我的小部件放在列表中的堆栈中。列表中的每个小部件都会返回一个具有不同对齐方式的容器。

Container( alignment: Alignment(xValue,yValue))

我的堆栈如下:

stack(
children: <Widget>[
SizedBox(
width :MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Container(
color:Colors.greenAccent,
child: ListView.builder(
           physics: const NeverScrollableScrollPhysics(),
           itemCount: widgetList.length,
           itemBuilder(BuildContext contet, int index){
                return widgetList[index]; })
))])

即使我的容器出现在堆栈中,它们的位置也不是我想要的位置。它们的对齐是在 listtile 内完成的。我想要的是在 SizedBox 内的小部件列表中对齐这些容器。我可以按如下方式完成。

stack(
   children: <Widget>[
        SizedBox(
            width :MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: Container( color: Colors.greenAccent)),
        widgetList[0],
        widgetList[1], ])

这种方式即使可以让列表中的小部件位于需要的位置,它也不是动态的。我需要帮助才能动态执行此操作。

【问题讨论】:

    标签: flutter flutter-listview


    【解决方案1】:

    使用扩展运算符

    Stack(
       children: <Widget>[
            SizedBox(
                width :MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height,
                child: Container( color: Colors.greenAccent)),
            ...widgetList,    // Note the three dots before the widgetList
    ])
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2020-05-09
      • 2012-02-06
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      • 2019-03-03
      相关资源
      最近更新 更多