Created
September 16, 2024 11:31
-
-
Save omegasoft7/e0954730955b1b8bb5a57e57d7c1f24f 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>>(); | |
@override | |
void initState() { | |
super.initState(); | |
Stream.periodic(Duration(seconds: 2), (count) => 'Item ${count + 1}') | |
.take(10) | |
.listen((item) { | |
_items.add(item); | |
_streamController.add(_items); | |
}); | |
} | |
@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( | |
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