inherited_builder 1.0.1+8

  • Readme
  • Changelog
  • Example
  • Installing
  • 71

Inherited Builder #

Autogenerated state management and dependency inhection with inherited widgets

lesnitsky.dev GitHub stars Twitter Follow

ToC #

Motivation #

InheritedWidget is a powerful concept which allows you to inject values and pass them down the widget tree as well as subscribe to updates, but it requires a lot of boilerplate

This package allows you to get advantage of this awesome concept w/o boilerplate, it will be autogenerated for you

App Reference Architecture #

Check out app reference architecture

Example app #

"flutter create" app built with inherited_builder

Installation #

dev_dependencies:
  build_runner: <version>
  inherited_builder: ^1.0.0

Usage #

Define your model #

app_state.dart

import 'package:flutter/material.dart';
import 'package:inherited_builder/annotations.dart';

part 'app_state.g.dart'

@Inherited()
class AppState {
    final int count;

    const AppState({ this.count });

    String toString() {
        return 'Count is $count';
    }
}

Execute build_runner #

flutter packages pub run build_runner build --delete-conflicting-outputs

This will generate AppStateBuilder and AppStateProvider classes

Place Builder in your widget tree #

import 'package:flutter/material.dart';

class ParentWidget extends StatelessWidget {
  final Widget child;

  const ParentWidget({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    const initialValue = 0;

    return AppStateBuilder(
      count: initialValue,
      child: child,
    );
  }
}

Access values using Provider #

class ChildWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appStateProvider = AppStateProvider.of(context);

    return Scaffold(
      body: Center(child: Text(appStateProvider.count.toString())),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          final count = appStateProvider.count;
          appStateProvider.setcount(count + 1);
        },
      ),
    );
  }
}

Access instance of your model using Provider #

class ChildWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appStateProvider = AppStateProvider.of(context);
+   final appState = appStateProvider.model;

    return Scaffold(
-     body: Center(child: Text(appStateProvider.count.toString())),
+     body: Center(child: Text(appState.toString())),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          final count = appStateProvider.count;
          appStateProvider.setcount(count + 1);
        },
      ),
    );
  }
}

React to changes #

You can use didChangeDepencies() to react to changes of your model (e.g. appStateProvider.setCount(newCount))

Example:

import 'package:flutter/material.dart';

class StatePersistance extends StatefulWidget {
  final Widget child;

  const StatePersistance({Key key, this.child}) : super(key: key);

  @override
  _StatePersistanceState createState() => _StatePersistanceState();
}

class _StatePersistanceState extends State<StatePersistance> {
  @override
  Widget build(BuildContext context) {
    return widget.child;
  }

  @override
  void didChangeDependencies() async {
    final appStateProvider = AppStateProvider.of(context);

    final currentState = appStateProvider.model;
    final prevState = appStateProvider.oldModel;

    if (currentState.count != prevState.count) {
        persist(currentState);
    }

    super.didChangeDependencies();
  }
}

Define actions on your model #

requires dart >2.7.0

import 'package:flutter/material.dart';
import 'package:inherited_builder/annotations.dart';

@Inherited()
class AppState {
    final int count;

    const AppState({ this.count });

    String toString() {
        return 'Count is $count';
    }
}

extension CounterActions on AppStateProvider {
    increment() {
        this.setCount(this.count + 1);
    }
}

// somewhere in the widget tree:

final appStateProvider = AppStateProvider.of(context);
appStateProvider.increment();

License #

MIT

lesnitsky.dev GitHub stars Twitter Follow

[1.0.1+8] - 2020-02-01 #

  • upgrade analyzer version (fixes incompatibility with json_serializable)

[1.0.0+2] - 2020-01-27 #

  • fix readme

[1.0.0+1] - 2020-01-27 #

  • initial release

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:inherited_builder/annotations.dart';

part 'main.g.dart';

void main() => runApp(MyApp());

@Inherited()
class AppState {
  final int count;

  AppState({this.count});
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AppStateBuilder(
      count: 0,
      child: MaterialApp(
        title: 'Inherited Builder demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(title: 'Inherited Builder demo'),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  const MyHomePage({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final appStateProvider = AppStateProvider.of(context);
    final count = appStateProvider.count;

    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$count',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          appStateProvider.setCount(appStateProvider.count + 1);
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  inherited_builder: ^1.0.1+8

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:inherited_builder/annotations.dart';
import 'package:inherited_builder/builder.dart';
import 'package:inherited_builder/generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
50
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

We analyzed this package on Jul 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • inherited_builder that is a package requiring null.

Package not compatible with SDK flutter

Because it is not compatible with any of the supported runtimes: flutter-native, flutter-web

Package not compatible with runtime flutter-native on Android

Because:

  • package:inherited_builder/builder.dart that imports:
  • package:inherited_builder/generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on Linux

Because:

  • package:inherited_builder/builder.dart that imports:
  • package:inherited_builder/generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on Windows

Because:

  • package:inherited_builder/builder.dart that imports:
  • package:inherited_builder/generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on iOS

Because:

  • package:inherited_builder/builder.dart that imports:
  • package:inherited_builder/generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on macOS

Because:

  • package:inherited_builder/builder.dart that imports:
  • package:inherited_builder/generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-web on Web

Because:

  • package:inherited_builder/builder.dart that imports:
  • package:inherited_builder/generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/utils.dart that imports:
  • package:build/build.dart that imports:
  • package:build/src/generate/run_post_process_builder.dart that imports:
  • package:build/src/builder/post_process_builder.dart that imports:
  • package:build/src/builder/builder.dart that imports:
  • package:build/src/builder/build_step.dart that imports:
  • package:build/src/asset/reader.dart that imports:
  • package:glob/glob.dart that imports:
  • package:glob/src/list_tree.dart that imports:
  • package:glob/src/io.dart that imports:
  • package:glob/src/io_export.dart that imports:
  • dart:io

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
analyzer ^0.39.0 0.39.13
build >=0.12.6 <2.0.0 1.3.0
build_config >=0.2.6 <0.5.0 0.4.2
code_builder ^3.2.0 3.4.0
dart_style ^1.3.3 1.3.6
flutter 0.0.0
source_gen ^0.9.4+5 0.9.6
Transitive dependencies
_fe_analyzer_shared 5.0.0
args 1.6.0
async 2.4.2
built_collection 4.3.2
built_value 7.1.0
charcode 1.1.3
checked_yaml 1.0.2
collection 1.14.12 1.14.13
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
fixnum 0.10.11
glob 1.2.0
html 0.14.0+3
js 0.6.2
json_annotation 3.0.1
logging 0.11.4
matcher 0.12.9
meta 1.1.8 1.2.2
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
path 1.7.0
pedantic 1.9.0 1.9.2
pub_semver 1.4.4
pubspec_parse 0.1.5
quiver 2.1.3
sky_engine 0.0.99
source_span 1.7.0
stack_trace 1.9.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
watcher 0.9.7+15
yaml 2.2.1
Dev dependencies
flutter_test