Checkbox 更新复选框并从颤振对话框中返回值
我正在尝试将一些城市列表添加到带有复选框的对话框中,以便我需要实现多个单击项目。下面是我想做的Checkbox 更新复选框并从颤振对话框中返回值,checkbox,dart,dialog,flutter,Checkbox,Dart,Dialog,Flutter,我正在尝试将一些城市列表添加到带有复选框的对话框中,以便我需要实现多个单击项目。下面是我想做的 on按下from按钮调用Rest服务,成功后我只显示一个对话框 void showCityDialog(BuildContext context) { SimpleDialog dialog = new SimpleDialog( title: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,
on按下
from按钮调用Rest服务,成功后我只显示一个对话框
void showCityDialog(BuildContext context) {
SimpleDialog dialog = new SimpleDialog(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(
"CITIES",
style: TextStyle(fontSize: 18.0, color: Colors.black),
textAlign: TextAlign.center,
),
new RaisedButton(
onPressed: () {print("clicked");},
color: Color(0xFFfab82b),
child: new Text(
"Done",
style: TextStyle(color: Colors.white),
),)],),
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(
constraints: BoxConstraints(maxHeight: 500.0),
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: cityData.length,
itemBuilder: (context, position) {
return new CheckboxListTile(
value: checkboxValueCity,
onChanged: (bool value) {
setState(() {
checkboxValueCity = value;
});
},
activeColor: Color(0xFFfab82b),
dense: true,
title: Text(
cityData[position].city_name,
style: TextStyle(fontSize: 16.0, color: Colors.black),
),);},),),],)],);
showDialog(
context: context,
builder: (BuildContext context) {
return dialog;
});
}
void showCityDialog(构建上下文){
SimpleDialog=新建SimpleDialog(
标题:世界其他地区(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
新文本(
“城市”,
样式:TextStyle(字体大小:18.0,颜色:Colors.black),
textAlign:textAlign.center,
),
新升起的按钮(
按下:(){打印(“单击”);},
颜色:颜色(0xFFfab82b),
儿童:新文本(
“完成”,
样式:TextStyle(颜色:Colors.white),
),)],),
儿童:[
纵队(
mainAxisAlignment:mainAxisAlignment.start,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
新容器(
约束:框约束(最大高度:500.0),
子项:ListView.builder(
滚动方向:轴垂直,
itemCount:cityData.length,
itemBuilder:(上下文、位置){
返回新的CheckboxListTile(
值:checkboxValueCity,
一旦更改:(布尔值){
设置状态(){
checkboxValueCity=值;
});
},
activeColor:Color(0xFFfab82b),
是的,
标题:正文(
城市数据[位置]。城市名称,
样式:TextStyle(字体大小:16.0,颜色:Colors.black),
),);},),),],)],);
显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
返回对话框;
});
}
checkboxValueCity
是类中的布尔变量,单击chekboxListItem时,我需要将复选框值更新为选中和未选中。同时,需要将该项添加/删除到该类中的列表中
但在我的代码中,复选框并不是每次点击都引用,而是当我关闭该复选框并再次打开它时,复选框被选中。那么,如何从互动程序中获得多次单击,以及如何从对话框中返回列表?您的对话框需要是一个
StatefulWidget
()。跟踪选择状态的成员变量需要位于对话框类中。您可以使用回调来使用选定城市的列表更新父类中的成员变量。在SimpleDialog
或AlertDialog
中使用ListView.builder似乎也存在一些问题(在Flatter Github中搜索问题),因此我使用了一个普通的对话框
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Checkbox Dialog Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Checkbox Dialog Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool checkboxValueCity = false;
List<String> allCities = ['Alpha', 'Beta', 'Gamma'];
List<String> selectedCities = [];
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return _MyDialog(
cities: allCities,
selectedCities: selectedCities,
onSelectedCitiesListChanged: (cities) {
selectedCities = cities;
print(selectedCities);
});
});
}),
);
}
}
class _MyDialog extends StatefulWidget {
_MyDialog({
this.cities,
this.selectedCities,
this.onSelectedCitiesListChanged,
});
final List<String> cities;
final List<String> selectedCities;
final ValueChanged<List<String>> onSelectedCitiesListChanged;
@override
_MyDialogState createState() => _MyDialogState();
}
class _MyDialogState extends State<_MyDialog> {
List<String> _tempSelectedCities = [];
@override
void initState() {
_tempSelectedCities = widget.selectedCities;
super.initState();
}
@override
Widget build(BuildContext context) {
return Dialog(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'CITIES',
style: TextStyle(fontSize: 18.0, color: Colors.black),
textAlign: TextAlign.center,
),
RaisedButton(
onPressed: () {
Navigator.pop(context);
},
color: Color(0xFFfab82b),
child: Text(
'Done',
style: TextStyle(color: Colors.white),
),
),
],
),
Expanded(
child: ListView.builder(
itemCount: widget.cities.length,
itemBuilder: (BuildContext context, int index) {
final cityName = widget.cities[index];
return Container(
child: CheckboxListTile(
title: Text(cityName),
value: _tempSelectedCities.contains(cityName),
onChanged: (bool value) {
if (value) {
if (!_tempSelectedCities.contains(cityName)) {
setState(() {
_tempSelectedCities.add(cityName);
});
}
} else {
if (_tempSelectedCities.contains(cityName)) {
setState(() {
_tempSelectedCities.removeWhere(
(String city) => city == cityName);
});
}
}
widget
.onSelectedCitiesListChanged(_tempSelectedCities);
}),
);
}),
),
],
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振复选框对话框演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振复选框对话框演示”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
bool checkboxValueCity=false;
列出所有城市=['Alpha','Beta','Gamma'];
列出所选城市=[];
@凌驾
小部件构建(构建上下文){
返回脚手架(
浮动操作按钮:浮动操作按钮(
子:图标(Icons.add),
已按下:(){
显示对话框(
上下文:上下文,
生成器:(上下文){
返回我的对话框(
城市:所有城市,
选定城市:选定城市,
OnSelectedCities列表已更改:(城市){
选定城市=城市;
打印(选定城市);
});
});
}),
);
}
}
类_MyDialog扩展StatefulWidget{
_我的对话({
这个城市,
这是我选择的城市,
此.onSelectedCitiesList已更改,
});
最终名单城市;
最终选定城市名单;
所选城市的最终价值发生变化列表发生变化;
@凌驾
_MyDialogState createState()=>\u MyDialogState();
}
类_MyDialogState扩展了状态{
列表_tempSelectedCities=[];
@凌驾
void initState(){
_tempSelectedCities=widget.selectedCities;
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回对话框(
子:列(
儿童:[
划船(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
正文(
"城市",,
样式:TextStyle(字体大小:18.0,颜色:Colors.black),
textAlign:textAlign.center,
),
升起的按钮(
已按下:(){
Navigator.pop(上下文);
},
颜色:颜色(0xFFfab82b),
子:文本(
“完成”,
样式:TextStyle(颜色:Colors.white),
),
),
],
),
扩大(
子项:ListView.builder(
itemCount:widget.cities.length,
itemBuilder:(构建上下文,int索引){
final cityName=widget.cities[index];
返回容器(
子:CheckboxListTile(
标题:文本(城市名称),
值:_tempSelectedCities.contains(cityName),
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Checkbox Dialog Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Checkbox Dialog Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool checkboxValueCity = false;
List<String> allCities = ['Alpha', 'Beta', 'Gamma'];
List<String> selectedCities = [];
List<String> selectedCitiesTemp = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("App Bar"),
),
body: Center(
child: Column(
children: <Widget>[
_list(),
RaisedButton(
child: Text("Update From TMP List"),
onPressed: () {
setState(() {
selectedCities = selectedCitiesTemp;
});
},
)
],
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return _MyDialog(
cities: allCities,
selectedCities: selectedCities,
onSelectedCitiesListChanged: (cities) {
setState(() {
selectedCitiesTemp = cities;
});
},
);
});
}),
);
}
Widget _list() {
List<Widget> list = [];
for(String item in selectedCities) {
list.add(ListTile(
title: Text(item),
));
}
return Column(
children: list
);
}
}
class _MyDialog extends StatefulWidget {
_MyDialog({
this.cities,
this.selectedCities,
this.onSelectedCitiesListChanged,
});
final List<String> cities;
final List<String> selectedCities;
final ValueChanged<List<String>> onSelectedCitiesListChanged;
@override
_MyDialogState createState() => _MyDialogState();
}
class _MyDialogState extends State<_MyDialog> {
List<String> _tempSelectedCities = [];
@override
void initState() {
_tempSelectedCities = widget.selectedCities;
super.initState();
}
@override
Widget build(BuildContext context) {
return Dialog(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'CITIES',
style: TextStyle(fontSize: 18.0, color: Colors.black),
textAlign: TextAlign.center,
),
],
),
Expanded(
child: ListView.builder(
itemCount: widget.cities.length,
itemBuilder: (BuildContext context, int index) {
final cityName = widget.cities[index];
return Container(
child: CheckboxListTile(
title: Text(cityName),
value: _tempSelectedCities.contains(cityName),
onChanged: (bool value) {
if (value) {
if (!_tempSelectedCities.contains(cityName)) {
setState(() {
_tempSelectedCities.add(cityName);
});
}
} else {
if (_tempSelectedCities.contains(cityName)) {
setState(() {
_tempSelectedCities.removeWhere(
(String city) => city == cityName);
});
}
}
widget.onSelectedCitiesListChanged(_tempSelectedCities);
}),
);
}),
),
],
),
);
}
}
void _showDialog() {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder( // StatefulBuilder
builder: (context, setState) {
return AlertDialog(
actions: <Widget>[
Container(
width: 400,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"Student Attendence",
style: TextStyle(fontSize: 20),
),
SizedBox(
height: 5,
),
Container(
height: 2,
color: Colors.black,
),
SizedBox(
height: 15,
),
CheckboxListTile(
value: user1,
title: Text("user1"),
onChanged: (value){
setState(() {
user1=value;
});
},
),
Divider(
height: 10,
),
CheckboxListTile(
value: user2,
title: Text("user2"),
onChanged: (value){
setState(() {
user2=value;
});
},
),
Divider(
height: 10,
),
CheckboxListTile(
value: user3,
title: Text("user3"),
onChanged: (value){
setState(() {
user3=value;
});
},
),
Divider(
height: 10,
),
CheckboxListTile(
value: user4,
title: Text("user4"),
onChanged: (value){
setState(() {
user4=value;
});
},
),
Divider(
height: 10,
),
SizedBox(
height: 5,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Material(
elevation: 5.0,
color: Colors.blue[900],
child: MaterialButton(
padding: EdgeInsets.fromLTRB(
10.0, 5.0, 10.0, 5.0),
onPressed: () {},
child: Text("Save",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 15,
)),
),
),
Material(
elevation: 5.0,
color: Colors.blue[900],
child: MaterialButton(
padding: EdgeInsets.fromLTRB(
10.0, 5.0, 10.0, 5.0),
onPressed: () {
setState(() {
Navigator.of(context).pop();
});
},
child: Text("Cancel",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 15,
)),
),
),
Material(
elevation: 5.0,
color: Colors.blue[900],
child: MaterialButton(
padding: EdgeInsets.fromLTRB(
10.0, 5.0, 10.0, 5.0),
onPressed: () {},
child: Text("Select All",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 15,
)),
),
),
],
)
],
))
],
);
},
);
},
);
}