Last active
December 30, 2020 15:20
-
-
Save Morthor/1fffbe68a0945305a7f70432cbb9d82a to your computer and use it in GitHub Desktop.
AnimatedList, SizeTransition & Dismissible
This file contains 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
// Created for use in this Medium article: https://morthor.medium.com/flutter-animatedlist-sizetransition-dismissible-dce49c11200d | |
import 'dart:math'; | |
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: 'AnimatedList', | |
theme: ThemeData( | |
primarySwatch: Colors.orange, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
GlobalKey<AnimatedListState> animatedListKey = GlobalKey<AnimatedListState>(); | |
List<String> list = new List<String>(); | |
var rng = new Random(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('AnimatedList'), | |
centerTitle: true, | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () => addItem(), | |
), | |
body: Center( | |
child: AnimatedList( | |
key: animatedListKey, | |
initialItemCount: list.length, | |
itemBuilder: (context, index, animation){ | |
return SizeTransition( | |
sizeFactor: animation, | |
child: Dismissible( | |
key: Key('${list[index].hashCode}'), | |
onDismissed: (direction) => removeItem(list[index], index), | |
background: Container(color: Colors.red[700]), | |
direction: DismissDirection.startToEnd, | |
child: TextContainer(text: list[index]), | |
), | |
); | |
}, | |
), | |
), | |
); | |
} | |
void addItem(){ | |
list.add('item ${rng.nextInt(100)}'); | |
animatedListKey.currentState.insertItem(list.length-1); | |
} | |
void removeItem(String item, int index){ | |
animatedListKey.currentState.removeItem(index, (context, animation) { | |
return SizedBox(width: 0, height: 0); | |
}); | |
list.remove(item); | |
} | |
} | |
class TextContainer extends StatelessWidget { | |
final String text; | |
TextContainer({ | |
@required this.text, | |
}); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
alignment: Alignment.center, | |
padding: EdgeInsets.all(8), | |
child: Text(text), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment