Cómo eliminar ListView en Flutter
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Slidable Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Slidable Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final List<_HomeItem> items = List.generate(
5,
(i) => _HomeItem(
i,
'Tile n°$i',
),
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: _buildList(context),
),
);
}
Widget _buildList(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return Tile(items[index], _deleteItem);
},
itemCount: items.length,
);
}
void _deleteItem(_HomeItem item) {
setState(() {
print(context);
print("remove: $item");
print("Number of items before: ${items.length}");
items.remove(item);
print("Number of items after delete: ${items.length}");
});
}
}
class Tile extends StatefulWidget {
final _HomeItem item;
final Function delete;
Tile(this.item, this.delete);
@override
State<StatefulWidget> createState() => _TileState(item, delete);
}
class _TileState extends State<Tile> {
final _HomeItem item;
final Function delete;
_TileState(this.item, this.delete);
@override
Widget build(BuildContext context) {
return ListTile(
key: ValueKey(item.index),
title: Text("${item.title}"),
subtitle: Text("${item.index}"),
onTap: () => delete(item),
);
}
}
class _HomeItem {
const _HomeItem(
this.index,
this.title,
);
final int index;
final String title;
}
Sparkling Skimmer