Skip to content

:octocat: Flutter generic provider using InheritedWidget. Flutter generic provider using InheritedWidget. An helper to easily exposes a value using InheritedWidget without having to write one.

License

Notifications You must be signed in to change notification settings

hoc081098/flutter_provider

Repository files navigation

flutter_provider

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) 2019-2021 Petrus Nguyễn Thái Học

About

:octocat: Flutter generic provider using InheritedWidget. Flutter generic provider using InheritedWidget. An helper to easily exposes a value using InheritedWidget without having to write one.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages