1

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

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

谢谢!

4

2 回答 2

2

改为使用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];
                  }),
            ),
          ],
        ));
  }
}
于 2019-12-20T17:54:44.107 回答
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);
              });
            }));
  }
}
于 2019-12-20T17:45:18.180 回答