The Dismissible
Widget has an option for this, confirmDismiss
. It expects a Future<bool>
. You give it a Future
method that returns a true
or false
. So depending if your network operation fails, or doesn't, you can make the Dismissible
reset:
class Issue65842845 extends StatefulWidget {
@override
_Issue65842845State createState() => _Issue65842845State();
}
class _Issue65842845State extends State<Issue65842845> {
List<String> items = [
'item 1',
'item 2',
];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index){
return Dismissible(
background: Container(color: Colors.red),
key: Key(items[index].hashCode.toString()),
direction: DismissDirection.startToEnd,
confirmDismiss: (direction) => confirmDismiss(items[index]),
child: Container(
padding: EdgeInsets.all(6),
child: Text(items[index]),
height: 30,
alignment: Alignment.centerLeft,
),
);
},
);
}
Future<bool> confirmDismiss(String text) async {
if(text == 'item 1'){
// Operation was successful and item was removed from remote server
// Dismissible is removed
return true;
} else {
// Operation failed and Dismissible is reset
return false;
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…