【问题标题】:Flutter listview button to create extra children颤动列表视图按钮以创建额外的孩子
【发布时间】:2020-04-13 04:54:45
【问题描述】:

我正在处理的颤振应用程序中有一个列表视图,我希望能够根据按下按钮的时间向列表视图添加额外的项目。该按钮应位于所有项目下方。每次按下按钮时,我都想添加一个额外的容器。理想情况下,它将是一个小部件。我不知道该怎么做。这是我的代码:

body: ListView(
          children: <Widget>[
            Container(   //this is the container I would like to add another of when a button is pressed
              height: 200,
              child: optionsChoices(),
            ), //end container
            Container(
              height: 200,
              child: optionsChoices(),
            ),
            Container(
              height: 200,
              child: optionsChoices(),
            ),
            Container(
              height: 200,
              child: optionsChoices(),
            ),
          ]
        )

谢谢!

【问题讨论】:

    标签: flutter dart flutter-listview


    【解决方案1】:

    请改用ListView.builder(),并使用包含容器小部件的列表以及setState() 来管理列表的状态。

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int x = 60;
      List<Widget> a = [
          Container(
            height: 200,
            child: Text('Test'),
          )
        ];
      void _d() {
        setState(() {
          a.add(Container(
                        height: 200,
                        child: Text('Test'),
                      ));
        });
      }
    
      Widget build(context) {
    
        return Scaffold(
            appBar: AppBar(),
            body: Column(
              children: <Widget>[
                FlatButton(
                    onPressed: () {
                      _d();
    
                    },
                    child: Text('Press here to add item')),
                Expanded(
                  child: ListView.builder(
                      itemCount: a.length,
                      itemBuilder: (context, index) {
                        return a[index];
                      }),
                ),
              ],
            ));
      }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用ListView.builder() 从您的列表视图中生成项目。将一个对象或值存储到List 类型的变量中并将其传递给列表视图。

      这是一个完整的示例来源:

      import 'package:flutter/material.dart';
      
      class Demo extends StatefulWidget {
        @override
        _DemoState createState() => _DemoState();
      }
      
      class _DemoState extends State<Demo> {
        List<int> items = [];
      
        @override
        void initState() {
          items = List.generate(3, (i) {
            // add some dummy items on activity start
            return i;
          });
          super.initState();
        }
      
        Widget listViewItem({int index}) {
          // widget layout for listview items
          return Container(
              height: 200,
              child:
                  Text("$index") // just for the demo, you can pass optionsChoices()
              );
        }
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
              appBar: AppBar(
                title: Text("DEMO"),
              ),
              body: ListView.builder(
                itemCount: items.length,
                itemBuilder: (context, i) {
                  return listViewItem(index: i); // item layout
                },
              ),
              floatingActionButton: FloatingActionButton(
                  child: Icon(Icons.add),
                  onPressed: () {
                    setState(() {
                      // add another item to the list
                      items.add(items.length);
                    });
                  }));
        }
      }
      

      【讨论】:

      • 应该可以。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 2016-05-15
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      相关资源
      最近更新 更多