Last updated:
0 purchases
flutter provider
flutter_provider #
Author: Petrus Nguyễn Thái Học #
Flutter generic provider using InheritedWidget. An helper to easily exposes a value using InheritedWidget without having to write one.
Getting Started #
In your flutter project, add the dependency to your pubspec.yaml
dependencies:
...
flutter_provider: <latest_version>
copied to clipboard
Usage #
1. Provide #
final foo = Foo();
final bar1 = Bar1();
Providers(
providers: [
Provider<Bar1>.value(
bar1,
disposer: (v) => v.dispose(),
),
Provider<Bar2>.factory(
(context) => Bar2(),
disposer: (v) => v.dispose(),
),
],
child: Provider<Foo>.value(
foo,
disposer: (v) => v.dispose(),
child: const HomePage(),
),
);
copied to clipboard
2. Consume #
Provider.of<T>(context);
context.get<T>();
Consumer<T>(builder: (context, T value) { });
copied to clipboard
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter provider example'),
),
body: Consumer3<Foo, Bar1, Bar2>(
builder: (BuildContext context, Foo a, Bar1 b, Bar2 c) {
return Container(
constraints: BoxConstraints.expand(),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(a.foo()),
Text(b.bar1()),
Text(c.bar2()),
],
),
),
);
},
),
);
}
}
copied to clipboard
License #
MIT License
Copyright (c) 2021 Petrus Nguyễn Thái Học
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.