Reputation: 187
I'm trying to implement a swipe to dismiss list view as the child of a column so I can add a row of buttons below it but nothing is appearing on the screen when this renders and I am getting no errors.
final goals = List<String>.generate(20,(i) => 'item ${i+1}');
class AddButton extends StatelessWidget {
final formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Column(children: <Widget>[
ListView.builder(
itemBuilder: (context,int index){
return new Dismissible(
key: new Key(goals[1]),
onDismissed: (direction){
goals.removeAt(index);
Scaffold.of(context).showSnackBar(SnackBar(
content: new Text("Item Removed")
));
},
child: new ListTile(
title: new Text("${goals[index]}"),
)
);
},
itemCount: goals.length,
)
],
),
),
);
}
}
Reference for what I'm trying to implement: https://flutter.dev/docs/cookbook/gestures/dismissible
Upvotes: 0
Views: 260
Reputation: 6161
I've created a variable to reference the current goal being built. Then, I've created a key from the item and inside the onDismissed
function, I've added the required setState
method. This example requires the use of a StatefulWidget
, not a StatelessWidget
.
NOTE: As of Dart 2.0, the new
keyword is optional.
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Column(children: <Widget>[
ListView.builder(
itemBuilder: (context,int index){
var item = goals[index];
return Dismissible(
key: Key(item),
onDismissed: (direction){
setState(() {
goals.removeAt(index);
});
Scaffold.of(context).showSnackBar(SnackBar(
content: new Text("Item Removed")
));
},
child: ListTile(title: Text("$item"))
);
},
itemCount: goals.length,
)],
),
),
);
}
Upvotes: 1