Created
September 16, 2024 11:30
-
-
Save omegasoft7/7fe1c005dd3a24fdfafa1a9d56efa766 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:async'; | |
import 'package:flutter/material.dart'; | |
class AnimatedListExample extends StatefulWidget { | |
@override | |
_AnimatedListExampleState createState() => _AnimatedListExampleState(); | |
} | |
class _AnimatedListExampleState extends State<AnimatedListExample> { | |
final List<String> _items = []; | |
final StreamController<List<String>> _streamController = StreamController<List<String>>(); | |
final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>(); | |
@override | |
void initState() { | |
super.initState(); | |
Stream.periodic(Duration(seconds: 2), (count) => 'Item ${count + 1}') | |
.take(10) | |
.listen((item) { | |
_items.add(item); | |
_streamController.add(_items); | |
_listKey.currentState?.insertItem(_items.length - 1); | |
}); | |
} | |
@override | |
void dispose() { | |
_streamController.close(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Animated List Example'), | |
), | |
body: StreamBuilder<List<String>>( | |
stream: _streamController.stream, | |
builder: (context, snapshot) { | |
if (!snapshot.hasData) { | |
return Center(child: CircularProgressIndicator()); | |
} | |
return AnimatedList( | |
key: _listKey, | |
initialItemCount: snapshot.data!.length, | |
itemBuilder: (context, index, animation) { | |
return FadeTransition( | |
opacity: animation, | |
child: ListTile( | |
title: Text(snapshot.data![index]), | |
), | |
); | |
}, | |
); | |
}, | |
), | |
); | |
} | |
} | |
void main() => runApp(MaterialApp(home: AnimatedListExample())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment