我是一个初学者,还在学习颤动,无法添加全选/全选按钮,有人能帮我吗?我有一个项目列表,我想将Select All/Deselect All按钮添加到以下代码中,当单击下面的Select all按钮时,应该可以看到"Selected Button“。
这是我的模型类
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(),
],
),
);
}
}
发布于 2021-08-23 05:49:05
下面是select代码
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'),
),
https://stackoverflow.com/questions/68887749
复制相似问题