【发布时间】:2021-01-14 15:15:46
【问题描述】:
我正在使用 flutter_slidable 包从我的应用程序中的列表中删除项目。但我想在列表视图之外创建一个按钮,可以触发这些项目以显示它们的可滑动项,但我不确定如何从提供的文档中执行此操作
https://pub.dev/packages/flutter_slidable
bool isClicked = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
brightness: Brightness.light,
leading: new IconButton(
icon: new Icon(Icons.arrow_back, color: Colors.black),
onPressed: () {
Navigator.of(context).pop();
},
),
backgroundColor: Colors.white,
title: const Text(
'User Cards',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.black, fontSize: 20.0),
),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: Row(
children: <Widget>[
GestureDetector(
onTap: () {
Provider.of<CardBrandProvider>(context, listen: false).clearCardBrand();
Provider.of<Data>(context, listen: false).selectedBrand = null;
Navigator.push(context, MaterialPageRoute(builder: (context) => AddCard()));
},
child: Row(
children: <Widget>[
Icon(
Icons.card_giftcard,
color: Colors.grey[600],
size: 28.0,
),
Icon(
Icons.add,
size: 20.0,
color: Colors.black45,
),
],
)),
],
),
)
],
elevation: 1.0,
centerTitle: true,
),
body: CustomScrollView(
slivers: [
Provider.of<CardProvider>(context, listen: false).cards.isNotEmpty ?
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.only(top: 20),
child: Padding(
padding: const EdgeInsets.only(left: 30.0, right: 26.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Cards',
style: TextStyle(
fontSize: 17.0,
fontWeight: FontWeight.w500,
fontFamily: 'Lato'),
),
GestureDetector(
onTap: (){
isClicked = true;
},
child: Text(
'Edit',
style: TextStyle(
fontSize: 17.0,
fontWeight: FontWeight.w500,
color: Color.fromRGBO(129, 2, 2, 1),
fontFamily: 'Lato'),
),
),
],
),
),
),
)
:
SliverToBoxAdapter(child: Container(),),
SliverToBoxAdapter(
child: SizedBox(
height: 25,
),
),
Container(
child: SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
var card = getCards(context)[index];
var id = card.Id;
return InkWell(
child: Padding(
padding: const EdgeInsets.only(top: 15, bottom: 15),
child: Column(
children: [
Container(
child: Slidable(
enabled: isClicked,
actionPane: SlidableDrawerActionPane(),
secondaryActions: <Widget>[
IconSlideAction(
caption: 'Delete',
color: Colors.red,
icon: Icons.delete,
onTap: () async {
setState(() {
showSpinner = true;
});
try{
print('$id');
setState(() {
Provider.of<Cards>(context, listen: false).removeIndividualCard(index);
});
} catch(e){
print(e);
setState(() {
showSpinner = true;
});
}
}),
],
child: ListTile(
title: Text('Username$index'),
trailing: Icon(Icons.person),
),
),
),
],
),
),
);
},
childCount: 5,
),
],
),
);
}
}
【问题讨论】:
-
您需要一个按钮来启用或禁用可滑动吗?
-
是的 - 启用它