|
| 1 | +/// |
| 2 | +/// Created by NieBin on 2019/6/9 |
| 3 | +/// Github: https://github.com/nb312 |
| 4 | + |
| 5 | +/// |
| 6 | +import "package:flutter/material.dart"; |
| 7 | +import 'package:flutter_widgets/const/_const.dart'; |
| 8 | + |
| 9 | +class AnimListPage extends StatefulWidget { |
| 10 | + @override |
| 11 | + AnimListState createState() => AnimListState(); |
| 12 | +} |
| 13 | + |
| 14 | +List<String> list = [ |
| 15 | + "Hello", |
| 16 | + "World", |
| 17 | + "AAAA", |
| 18 | + "BBBB", |
| 19 | + "CCCC", |
| 20 | + "DDDDD", |
| 21 | + "EEEE", |
| 22 | + "FFFF" |
| 23 | +]; |
| 24 | + |
| 25 | +class AnimListState extends State<AnimListPage> |
| 26 | + with SingleTickerProviderStateMixin { |
| 27 | + final _key = GlobalKey<AnimatedListState>(); |
| 28 | + |
| 29 | + Widget _itemBuilder(context, index, anim) => index == 0 |
| 30 | + ? FloatingActionButton( |
| 31 | + child: Text("Click"), |
| 32 | + onPressed: () { |
| 33 | + if (list.length > 20) { |
| 34 | + var s = list.removeAt(1); |
| 35 | + _key.currentState.removeItem( |
| 36 | + 1, |
| 37 | + (context, anim) => ScaleTransition( |
| 38 | + scale: anim, |
| 39 | + child: Text(list[index]), |
| 40 | + ), |
| 41 | + ); |
| 42 | + } else { |
| 43 | + list.insert(1, "Hello"); |
| 44 | + _key.currentState.insertItem(1); |
| 45 | + } |
| 46 | + }, |
| 47 | + ) |
| 48 | + : Container( |
| 49 | + constraints: BoxConstraints.expand(height: 40), |
| 50 | + child: ScaleTransition( |
| 51 | + scale: anim, |
| 52 | + child: Text(list[index]), |
| 53 | + )); |
| 54 | + |
| 55 | + @override |
| 56 | + void initState() { |
| 57 | + super.initState(); |
| 58 | + } |
| 59 | + |
| 60 | + @override |
| 61 | + Widget build(BuildContext context) { |
| 62 | + return Scaffold( |
| 63 | + appBar: AppBar( |
| 64 | + title: Text("Hello World"), |
| 65 | + ), |
| 66 | + body: AnimatedList( |
| 67 | + key: _key, |
| 68 | + initialItemCount: list.length, |
| 69 | + itemBuilder: _itemBuilder, |
| 70 | + )); |
| 71 | + } |
| 72 | +} |
0 commit comments