multi_animated_builder 0.1.0
multi_animated_builder: ^0.1.0 copied to clipboard
An AnimatedBuilder that takes a list of Listenable and rebuilds when any of them fire.
import 'package:flutter/material.dart';
import 'package:multi_animated_builder/multi_animated_builder.dart';
class Counter extends ChangeNotifier {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
}
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final counter1 = Counter();
final counter2 = Counter();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: MultiAnimatedBuilder(
animations: [counter1, counter2],
builder: (context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the buttons this many times:',
),
Text(
'${counter1.count} + ${counter2.count}',
style: Theme.of(context).textTheme.headlineMedium,
),
],
);
},
),
),
floatingActionButton: Row(
children: [
FloatingActionButton(
onPressed: counter1.increment,
tooltip: 'Increment 1',
child: const Icon(Icons.add),
),
FloatingActionButton(
onPressed: counter2.increment,
tooltip: 'Increment 2',
child: const Icon(Icons.add),
),
],
),
);
}
}