因为数据用的是Mysql,Mysql的bool值只有0和1,怎样有效的整合到以下代码 class MyWidget extends StatefulWidget { @override _MyWidgetState createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { List<Map<String, dynamic>> _data = [ {'title': '备注1', 'content': '这是备注1的内容', 'isVisible': false}, {'title': '备注2', 'content': '这是备注2的内容', 'isVisible': false}, {'title': '备注3', 'content': '这是备注3的内容', 'isVisible': false}, ]; @override Widget build(BuildContext context) { return Container( margin: EdgeInsets.symmetric(vertical: 30,horizontal: 10), child: Column( children: _data.map((item) { int index = _data.indexOf(item); return Column( children: [ ListTile( title: Text(item['title']), trailing: IconButton( icon: Icon(item['isVisible'] ? Icons.arrow_drop_up : Icons.arrow_drop_down), onPressed: () { setState(() { _data[index]['isVisible'] = !_data[index]['isVisible']; }); }, ), ), Visibility( visible: item['isVisible'], child: Text(item['content']), ), ], ); }).toList(), ), ); } }
时间: 2024-02-14 11:17:42 浏览: 57
可以将bool类型的值转换为int类型的值,例如将false转换为0,将true转换为1,在代码中使用int类型的值即可,代码如下:
```
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
List<Map<String, dynamic>> _data = [
{'title': '备注1', 'content': '这是备注1的内容', 'isVisible': 0},
{'title': '备注2', 'content': '这是备注2的内容', 'isVisible': 0},
{'title': '备注3', 'content': '这是备注3的内容', 'isVisible': 0},
];
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: 30, horizontal: 10),
child: Column(
children: _data.map((item) {
int index = _data.indexOf(item);
return Column(
children: [
ListTile(
title: Text(item['title']),
trailing: IconButton(
icon: Icon(item['isVisible'] == 1
? Icons.arrow_drop_up
: Icons.arrow_drop_down),
onPressed: () {
setState(() {
_data[index]['isVisible'] = _data[index]['isVisible'] == 1 ? 0 : 1;
});
},
),
),
Visibility(
visible: item['isVisible'] == 1,
child: Text(item['content']),
),
],
);
}).toList(),
),
);
}
}
```
阅读全文