Flutter Listview按钮创建额外的孩子

时间:2019-12-20 17:32:39

标签: flutter dart flutter-listview

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

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(),
            ),
          ]
        )

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用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);
              });
            }));
  }
}

答案 1 :(得分:0)

改为使用ListView.builder(),然后使用包含容器小部件的List和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];
                  }),
            ),
          ],
        ));
  }
}