我面临在多个ExpansionTile中添加带复选框的listview的问题。同样,必须动态选择一个复选框。
在此图像中,我创建了一个扩展图块,并在其中添加了带有复选框的列表图块,但是当我添加多个扩展图块时,都基于值被选中或未选中,但是我想选择用户想要的值,这意味着它应该是动态的
body: ExpansionTile(
title: Text("Expansion"),
children: _listViewData
.map((data) => CheckboxListTile(
title: Text(data),
value: _isChecked,
onChanged: (val) {
setState(() {
_isChecked = val;
});
},
))
.toList(),
));
答案 0 :(得分:2)
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var lovCountryServices = [
{
"services": [
{
"service_group_id": 2,
"service_category": "B",
"name": "Cat",
"id": 6
},
],
"name": "A",
"is_active": true,
"id": 2
},
{
"services": [
{
"service_group_id": 3,
"service_category": "B",
"name": "Air",
"id": 10
},
],
"name": "B",
"is_active": true,
"id": 3
}
];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: <Widget>[
ListView.builder(
shrinkWrap: true,
padding: const EdgeInsets.all(8.0),
itemCount: lovCountryServices.length,
itemBuilder: (BuildContext context, int index) {
var item = lovCountryServices[index];
return ExpansionTile(
title: Text("Expansion"),
children: <Widget>[
CheckboxListTile(
title: Text(item['name']),
value: item['is_active'],
onChanged: (val) {
setState(() {
item['is_active'] = val;
});
},
),
],
);
},
),
RaisedButton(
onPressed: () => print("sending to backend"),
child: Text("SEND"),
)
],
)),
);
}
}
编辑:如果您不想使用映射,则可以使用is_active属性来选中复选框,然后使用发送按钮将其发布到后端,以便将其保存到数据库中。