Skip to content

Instantly share code, notes, and snippets.

@omegasoft7
Last active September 16, 2024 11:29
Show Gist options
  • Save omegasoft7/74378d4c3b4bf2bf59d3b172809e565a to your computer and use it in GitHub Desktop.
Save omegasoft7/74378d4c3b4bf2bf59d3b172809e565a to your computer and use it in GitHub Desktop.
flutter challenge 01
import 'dart:async';
import 'package:flutter/material.dart';
class AnimatedListExample extends StatefulWidget {
@override
_AnimatedListExampleState createState() => _AnimatedListExampleState();
}
class _AnimatedListExampleState extends State<AnimatedListExample> with SingleTickerProviderStateMixin {
final List<String> _items = [];
final StreamController<List<String>> _streamController = StreamController<List<String>>();
Timer? _timer;
late AnimationController _animationController;
@override
void initState() {
super.initState();
_animationController = AnimationController(
duration: const Duration(seconds: 1),
vsync: this,
);
_timer = Timer.periodic(Duration(seconds: 2), (timer) {
_items.add('Item ${_items.length + 1}');
_streamController.add(_items);
});
}
@override
void dispose() {
_timer?.cancel();
_streamController.close();
_animationController.dispose();
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 ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
_animationController.forward(from: 0.0);
return FadeTransition(
opacity: _animationController,
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