|
| 1 | +/** |
| 2 | + * Author: Damodar Lohani |
| 3 | + * profile: https://github.com/lohanidamodar |
| 4 | + */ |
| 5 | + |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:flutter_ui_challenges/src/pages/bike/bike_details.dart'; |
| 8 | + |
| 9 | +List<String> places = [ |
| 10 | + "Nuwakot", |
| 11 | + "Dhaulagiri", |
| 12 | + "Rara", |
| 13 | + "Muktinath", |
| 14 | + "Pashupatinath" |
| 15 | +]; |
| 16 | + |
| 17 | +class AnimatedListOnePage extends StatefulWidget { |
| 18 | + static final String path = "lib/src/pages/animations/anim5.dart"; |
| 19 | + @override |
| 20 | + _AnimatedListOnePageState createState() => _AnimatedListOnePageState(); |
| 21 | +} |
| 22 | + |
| 23 | +class _AnimatedListOnePageState extends State<AnimatedListOnePage> { |
| 24 | + List<String> items; |
| 25 | + GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>(); |
| 26 | + |
| 27 | + @override |
| 28 | + void initState() { |
| 29 | + items = ["Kathmandu", "Bhaktapur", "Pokhara", "Mount Everest"]; |
| 30 | + super.initState(); |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + Widget build(BuildContext context) { |
| 35 | + return Scaffold( |
| 36 | + appBar: AppBar( |
| 37 | + title: Text("Animated List One"), |
| 38 | + ), |
| 39 | + backgroundColor: Colors.grey.shade300, |
| 40 | + body: AnimatedList( |
| 41 | + key: _listKey, |
| 42 | + initialItemCount: items.length, |
| 43 | + itemBuilder: (context, index, anim) { |
| 44 | + return SlideTransition( |
| 45 | + position: Tween<Offset>(begin: Offset(1, 0), end: Offset.zero) |
| 46 | + .animate(anim), |
| 47 | + child: BorderedContainer( |
| 48 | + margin: const EdgeInsets.symmetric( |
| 49 | + vertical: 4.0, |
| 50 | + horizontal: 8.0, |
| 51 | + ), |
| 52 | + padding: const EdgeInsets.all(0), |
| 53 | + child: ListTile( |
| 54 | + title: Text(items[index]), |
| 55 | + trailing: IconButton( |
| 56 | + icon: Icon(Icons.clear), |
| 57 | + onPressed: () { |
| 58 | + _listKey.currentState.removeItem(index, |
| 59 | + (context, animation) { |
| 60 | + String removedItem = items.removeAt(index); |
| 61 | + return SizeTransition( |
| 62 | + sizeFactor: animation, |
| 63 | + axis: Axis.vertical, |
| 64 | + child: BorderedContainer( |
| 65 | + margin: const EdgeInsets.symmetric( |
| 66 | + vertical: 4.0, |
| 67 | + horizontal: 8.0, |
| 68 | + ), |
| 69 | + padding: const EdgeInsets.all(0), |
| 70 | + child: ListTile( |
| 71 | + title: Text(removedItem), |
| 72 | + )), |
| 73 | + ); |
| 74 | + }); |
| 75 | + setState(() {}); |
| 76 | + }, |
| 77 | + ), |
| 78 | + ), |
| 79 | + ), |
| 80 | + ); |
| 81 | + }, |
| 82 | + ), |
| 83 | + floatingActionButton: FloatingActionButton( |
| 84 | + child: Icon(Icons.add), |
| 85 | + onPressed: () { |
| 86 | + places.shuffle(); |
| 87 | + items.insert(items.length, places[0]); |
| 88 | + _listKey.currentState.insertItem(items.length - 1); |
| 89 | + setState(() {}); |
| 90 | + }, |
| 91 | + ), |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
0 commit comments