【发布时间】:2021-08-07 12:49:54
【问题描述】:
每当我按下圆形RoundIcnBt 时我想在列表中添加元素并在再次按下时删除但它会添加重复的项目并且在再次按下按钮时不删除是否有任何其他方法可以做到这一点请建议我想要的是当用户按下按钮时,数据应该被存储到列表中,当用户再次按下相同的按钮项目时应该被删除:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mdi/mdi.dart';
class MyListEX extends StatefulWidget {
const MyListEX({Key key}) : super(key: key);
@override
_MyListEXState createState() => _MyListEXState();
}
class _MyListEXState extends State<MyListEX> {
List l = [];
var distl = [];
final List<int> rollNo = [
2016239001,
2016239002,
2016239003,
2016239004,
];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: 4,
itemBuilder: (context, index) {
return Column(
children: <Widget>[
SizedBox(
height: 10,
),
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [Colors.red, Colors.blue],
),
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.black,
offset: const Offset(3.0, 3.0),
blurRadius: 10,
spreadRadius: 2.0,
),
BoxShadow(
color: Colors.white,
offset: const Offset(0.0, 0.0),
blurRadius: 0.0,
spreadRadius: 0.0,
),
],
),
child: SizedBox(
height: 60,
child: ListTile(
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RoundIcnBt(
icon: Mdi.alphaPCircle,
onPressed: () {
setState(() {
l.add([
{
{rollNo[index]},
true
}
]);
print(distl = l.toSet().toList(growable: true));
});
},
),
],
),
leading: Icon(Icons.account_circle_outlined),
title: Text('${rollNo[index]}'),
),
),
),
],
);
},
);
}
}
【问题讨论】:
-
我添加了一个使用默认
IconButton的答案,请查看。