Flutter 列表视图按钮创建额外 children
Flutter listview button to create extra children
我正在处理的 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(),
),
]
)
谢谢!
您可以使用 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);
});
}));
}
}
改为使用 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];
}),
),
],
));
}
}
我正在处理的 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(),
),
]
)
谢谢!
您可以使用 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);
});
}));
}
}
改为使用 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];
}),
),
],
));
}
}