multi_listenable_builder 1.0.0 multi_listenable_builder: ^1.0.0 copied to clipboard
A widget builder whose content stays synced with one or more Listenables.
import 'package:flutter/material.dart';
import 'package:multi_listenable_builder/multi_listenable_builder.dart';
void main() {
runApp(const MyApp());
}
class CounterModel extends ChangeNotifier {
int _count = 0;
void increment() {
_count++;
notifyListeners();
}
int get count => _count;
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({super.key});
// create model
final CounterModel _counterModel = CounterModel();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('My Home Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
MultiListenableBuilder(
// supply the instance of `ChangeNotifier` model,
// whether you get it from the build context or anywhere
notifiers: [_counterModel],
// this builder function will be executed,
// once the `ChangeNotifier` model is updated
builder: (BuildContext context, _) {
return Text(
'${_counterModel.count}',
style: Theme.of(context).textTheme.headlineMedium,
);
},
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _counterModel.increment,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}