easy 1.1.2+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 78

Easy #

An easy Flutter state management that allows minimal rebuild of widgets and improves performance.

##HOW TO USE?

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

dependencies:
  easy: 

Import it:

import 'easy_bloc.dart';

Add EasyManager and your state class to Main:

example:

void main() {
  runApp(EasyManager(
    bloc: [
      Easy<AppBloc>(
        builder: (context) => AppBloc(),
      ),
    ],
    child: MyApp(),
  ));
}

Create your state class and extends EasyBloc overriding touch method:

class AppBloc extends EasyBloc {
  int counter = 0;

  @override
  void touch() {
    counter++;
    super.touch();
  }
}

With Easy, any variable you put into your EasyBloc class will be accessible through the Widget Store. And to call any function or method within your EasyBloc class you just need to call dart "EasyState.of<T>(context, listen: false)" with the method.

You can call dart "EasyState.of<T>(context, listen: false)" in building your widget like this:

Example:

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //
    final bloc = EasyState.of<AppBloc>(context, listen: false);
    
    return Scaffold(
      floatingActionButton: FloatingActionButton(
      //Run the "touch" method and everything in it in your EasyBloc class will be instantly executed.
          child: Icon(Icons.add), onPressed: () => bloc.touch()),
      body: Center(
      // Get the value of any variable within your EasyBloc class using the "Store" widget.
        child: Store<AppBloc>(
            builder: (context, value, child) => Text(
                  "${value.counter}",
                  style: TextStyle(fontSize: 50),
                )),
      ),
    );
  }
}

And that's all, when you click on the FloatingActionButton the value will be incremented automatically. Only the Text will be redone, all other widgets will remain intact. This allows for considerable performance gain when you have heavy widgets (such as videos, gifs, maps) in the tree.

In addition to improving performance, clearing code that will separate the business logic part of the UI (following the BLoC standard), EasyProvider works with Singletons, so you can call the Widget Store from anywhere in your code, even on another screen, being able to read and change it from anywhere without any boilerplate code!

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bloc = EasyState.of<AppBloc>(context, listen: false);
    print("rebuild");
    return Scaffold(
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add), onPressed: () => bloc.touch()),
      body: SafeArea(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => SecondRoute()),
                  );
                },
                child: Text('Go to Next Screen!'),
              ),
              Store<AppBloc>(
                  builder: (context, value, child) => Text(
                        "${value.counter}",
                        style: TextStyle(fontSize: 50),
                      )),
            ],
          ),
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bloc = EasyState.of<AppBloc>(context, listen: false);
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        heroTag: "increment second route",
        onPressed: () => bloc.touch(),
      ),
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Store<AppBloc>(
                builder: (context, counter, child) => Text(
                      "${counter.counter}",
                      style: TextStyle(fontSize: 50),
                    )),
            RaisedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Go back!'),
            ),
          ],
        ),
      ),
    );
  }
}

You can amazingly increment the counter number of both screens! That is all? No. You in addition to performing functions you can inject values from anywhere in your code.

This is made possible by the injection methods incorporated in Easy. Easy natively accepts Strings, int, Map, List, bool, double and dynamic for in Simply override any inject method on your EasyBloc.

Example with int:

class AppBloc extends EasyBloc {
  int counter = 0;

  @override
  void injectInt(int integer) {
    counter = integer;
    super.injectInt(integer);
  }

  @override
  void touch() {
    counter++;
    super.touch();
  }
}

Now, inject any value via parameter, and the counter will be updated automatically.

Note that if you click increment, it will increment the value already injected. This would not be possible for example, natively with Streams, unless you used RxDart's BehaviourSubject.

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bloc = EasyState.of<AppBloc>(context, listen: false);
    print("rebuild");
    return Scaffold(
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add), onPressed: () => bloc.touch()),
      body: SafeArea(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RaisedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => SecondRoute()),
                  );
                },
                child: Text('Go to Next Screen!'),
              ),
              Store<AppBloc>(
                  builder: (context, value, child) => Text(
                        "${value.counter}",
                        style: TextStyle(fontSize: 50),
                      )),
              RaisedButton(
                onPressed: () {
                  bloc.injectInt(333);
                },
                child: Text('inject 333 on counter'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Didn't like my comment above? Keep calm, I also love Streams, inclusive, you can use Streams with this package if you prefer.

class AppBloc extends EasyBloc {
  int counter = 0;

  final _counterController = StreamController<int>();

  // can work with rxDart like:
  // final _counterController = BehaviouSubject<int>();

  get counterOut => _counterController.stream;

  @override
  void touch() {
    counter++;
    _counterController.sink.add(counter);
    super.touch();
  }

  @override
  void dispose() {
    _counterController.close();
    super.dispose();
  }
}

class MainPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bloc = EasyState.of<AppBloc>(context, listen: false);
    print("rebuild");
    return Scaffold(
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add), onPressed: () => bloc.touch()),
      body: SafeArea(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              StreamBuilder<int>(
                  initialData: 0,
                  stream: bloc.counterOut,
                  builder: (context, snapshot) {
                    return Text(
                      "${snapshot.data}",
                      style: TextStyle(fontSize: 50),
                    );
                  }),
            ],
          ),
        ),
      ),
    );
  }
}

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

1.0.0 #

  • Initial release from Easy

1.0.1 #

  • Add example and clean

1.0.2 #

  • Update readme files

1.1.0 #

  • Add module widget

1.1.1 #

  • Removed unnecessary dependencies

1.1.2 #

  • Changed documentation

1.1.2+2 #

  • fix bugs

example/readme.md

Easy #

An easy Flutter state management that allows minimal rebuild of widgets and improves performance.

This library is completely interoperable with Provider, Scooped_model and Bloc_pattern, allowing you to use multiple state managers.

Getting Started #

Add EasyManager and your state class to Main:

example:

import 'package:easy/easy.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(BlocProvider(
    blocs: [Bloc((i) => AppBloc())],
    child: MyApp(),
  ));
}

Create a logic class and extends EasyBloc:

class AppBloc extends EasyBloc {
  int counter = 0;

  @override
  void touch() {
    counter++;
    super.touch();
  }
}

User EasyState to locate your logic class and Store for access to all your variables and methods of your EasyBloc class.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bloc = EasyState.of<AppBloc>(context, listen: false);
    return MaterialApp(
        title: 'EasyState',
        home: Scaffold(
          floatingActionButton: FloatingActionButton(
              child: Icon(Icons.add), onPressed: () => bloc.touch()),
          body: Center(
            child: Store<AppBloc>(
              builder: (context, value, child) => Text(
                "${value.counter}",
                style: TextStyle(fontSize: 50),
              ),
            ),
          ),
        ));
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  easy: ^1.1.2+2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:easy/easy.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
58
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
78
Learn more about scoring.

We analyzed this package on Dec 10, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.13.1+4
  • Flutter: 1.12.13+hotfix.2

Health suggestions

Fix lib/src/easy_bloc.dart. (-1.49 points)

Analysis of lib/src/easy_bloc.dart reported 3 hints:

line 108 col 19: 'inheritFromWidgetOfExactType' is deprecated and shouldn't be used. Use dependOnInheritedWidgetOfExactType instead. This feature was deprecated after v1.12.1..

line 109 col 19: 'ancestorInheritedElementForWidgetOfExactType' is deprecated and shouldn't be used. Use getElementForInheritedWidgetOfExactType instead. This feature was deprecated after v1.12.1..

line 554 col 18: 'inheritFromElement' is deprecated and shouldn't be used. Use dependOnInheritedElement instead. This feature was deprecated after v1.12.1..

Format lib/src/bloc.dart.

Run flutter format to format lib/src/bloc.dart.

Format lib/src/bloc_provider_error.dart.

Run flutter format to format lib/src/bloc_provider_error.dart.

Format lib/src/dependency.dart.

Run flutter format to format lib/src/dependency.dart.

Format lib/src/view.dart.

Run flutter format to format lib/src/view.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test