Created
September 30, 2022 03:50
-
-
Save corecode/4a2bf7c33c3eb1b0a69c702ed7531a2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'AnimatedList Test', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'AnimatedList Test'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final listKey = GlobalKey<AnimatedListState>(); | |
final List<ItemModel> items = []; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: AnimatedList( | |
key: listKey, | |
initialItemCount: items.length, | |
itemBuilder: (context, index, animation) => | |
_buildItem(items[index], animation), | |
), | |
floatingActionButton: Wrap( | |
children: [ | |
FloatingActionButton( | |
onPressed: _addItem, | |
tooltip: 'Add', | |
child: const Icon(Icons.add), | |
), | |
FloatingActionButton( | |
onPressed: _removeItem, | |
tooltip: 'Remove', | |
child: const Icon(Icons.remove), | |
) | |
], | |
), | |
); | |
} | |
void _addItem() { | |
setState(() { | |
final index = items.length; | |
items.add(ItemModel(Random().nextInt(1000))); | |
listKey.currentState!.insertItem(index); | |
print("add: $items"); | |
}); | |
} | |
void _removeItem() { | |
setState(() { | |
final item = items.removeAt(0); | |
listKey.currentState!.removeItem( | |
0, | |
(context, animation) => _buildItem(item, animation), | |
); | |
print("remove: $items"); | |
}); | |
} | |
Widget _buildItem(ItemModel item, Animation<double> animation) { | |
return SizeTransition( | |
key: ObjectKey(item), | |
sizeFactor: animation, | |
child: ListItem( | |
item: item, | |
), | |
); | |
} | |
} | |
class ListItem extends StatefulWidget { | |
final ItemModel item; | |
const ListItem({super.key, required this.item}); | |
@override | |
State<ListItem> createState() => _ListItemState(); | |
} | |
class _ListItemState extends State<ListItem> { | |
late int id2; | |
@override | |
void initState() { | |
super.initState(); | |
id2 = Random().nextInt(1000); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Card( | |
child: ListTile( | |
title: Text("${widget.item.id} - $id2"), | |
), | |
); | |
} | |
} | |
class ItemModel { | |
final int id; | |
ItemModel(this.id); | |
@override | |
String toString() { | |
return id.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment