flutter_provider

Author: Petrus Nguyễn Thái Học

Pub Build Status codecov License: MIT Hits

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>

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(),
  ),
);

2. Consume

Provider.of<T>(context);
context.get<T>();
Consumer<T>(builder: (context, T value) { });
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()),
                ],
              ),
            ),
          );
        },
      ),
    );
  }
}

License

MIT License

Copyright (c) 2021 Petrus Nguyễn Thái Học

Libraries

flutter_provider
Light weight provider for flutter. [...]