flutter - 如何在flutter中将全选和取消全选按钮添加到以下代码中?
问题描述
我是初学者,仍在学习颤振,无法添加全选/取消全选按钮有人能帮帮我吗?我有一个项目列表,我想将全选/取消全选按钮添加到以下代码中,当单击“选定按钮”下方的全选按钮时应该可见。
这是我的模型课
class ToyCarListModel{
String toyimages, toyname, toymodel;
bool isSelected;
ToyCarListModel(this.toyimages, this.toyname, this.toymodel, this.isSelected);
}
这是主屏幕代码
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:listview_image_text/carlistmodel.dart';
class ListToysUsingModel extends StatefulWidget {
@override
_ListToysUsingModelState createState() => _ListToysUsingModelState();
}
class _ListToysUsingModelState extends State<ListToysUsingModel> {
List<ToyCarListModel> toylist =[
ToyCarListModel("assets/pikachu.png", "flutter1", "model 1", false),
ToyCarListModel("assets/pikachu.png", "flutter2", "model 2", false),
ToyCarListModel("assets/pikachu.png", "flutter3", "model 3", false),
ToyCarListModel("assets/pikachu.png", "flutter4", "model 4", false),
ToyCarListModel("assets/pikachu.png", "flutter5", "model 5", false),
ToyCarListModel("assets/pikachu.png", "flutter6", "model 6", false),
];
List<ToyCarListModel> selectedcars = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List view with images using model'),
),
body: Container(
child: Column(
children: [
Container(
child: TextButton(
onPressed: () {},
child: Text('All Select'),
),
),
Expanded(child: ListView.builder(
itemCount: toylist.length,
itemBuilder: (BuildContext context, int index){
return toysItem(
toylist[index].toyimages,
toylist[index].toyname,
toylist[index].toymodel,
toylist[index].isSelected,
index,
);
}),
),
selectedcars.length > 0 ? Padding(
padding: EdgeInsets.symmetric(
horizontal: 25,
vertical: 10,
),
child: SizedBox(
width: double.infinity,
child: RaisedButton(
color: Colors.black,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Selected',style: TextStyle(color: Colors.white),),
Icon(Icons.done,color: Colors.white,),
],
),
onPressed: () {
print("List Lenght: ${selectedcars.length}");
},
),
),
): Padding(
padding: EdgeInsets.symmetric(
horizontal: 25,
vertical: 10,
),
child: SizedBox(
width: double.infinity,
child: RaisedButton(
color: Color(0xFFBFBFC0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Selected',style: TextStyle(color: Colors.white30),),
Icon(Icons.done,color: Colors.white30,),
],
),
onPressed: () {
print("List Lenght: ${selectedcars.length}");
},
),
),
),
],
),
),
);
}
Widget toysItem( String toyimages, toyname, toymodel, bool isSelected, int index){
return Container(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Image.asset(toyimages),
GestureDetector(
child: Container(
child: isSelected
? Icon(
Icons.check_circle,
color: Colors.black,
)
: Icon(
Icons.radio_button_unchecked_outlined,
color: Colors.grey,
),
),
onTap: (){
setState(() {
toylist[index].isSelected = !toylist[index].isSelected;
if (toylist[index].isSelected == true) {
selectedcars.add(ToyCarListModel(toyimages, toyname,toymodel, true));
} else if (toylist[index].isSelected == false) {
selectedcars
.removeWhere((element) => element.toyname == toylist[index].toyname);
}
});
},
),
],
),
Text(toyname),
Text(toymodel),
Divider(),
],
),
);
}
}
解决方案
这里的代码选择
child: TextButton(
onPressed: () {
setState (() {
for (var i = 0; i<toylist.length ; i++)
toylist[i]. isSelected = true;
selectedcars.addAll(toylist);
});
},
child: Text('All Select'),
),
这里是取消全选的代码
child: TextButton(
onPressed: () {
setState (() {
for (var i = 0; i<toylist.length ; i++)
toylist[i]. isSelected = false;
});
},
child: Text('Deselect All'),
),
推荐阅读
- amazon-sagemaker - 创建训练作业后,如何根据新数据重新训练 sagemaker 模型
- python - 如何运行 x 数量的线程并等待线程完成
- javascript - 如何从javascript中的另一个模块更改变量值
- c++ - 如果视频发送器关闭,则显示无信号图像
- sql - 使用子查询 H2 进行 SQL 更新
- redis - Symfony Messenger:重试延迟不适用于 Redis 传输
- python - PyGObject:将 Gtk.ListBoxRow 连接到它自己的函数?
- git - 修复删除的合并
- fonts - 有39个字体文件可以吗?
- javascript - 函数在 for 循环中执行多少次?