Giter VIP home page Giter VIP logo

flutter_acceptable's Introduction

A flutter package acceptable provides AcceptableStatefulWidget which glues Providers of provider package and StatefulWidget.

Features

  • Watch changes of target objects in state objects provided by Provider.
  • Callback when watching object changes to convert the object to whatever state for UI.

Getting started

As this package is completely depends on provider package, you must import the latest version of provider first.

Provide whatever state objects with Provider or other related classes to descendants.

Then, build StatefulWidget using AcceptableStatefulWidget instead of StatefulWidget which represents whatever UI using data provided by those objects.

AcceptableStatefulWidget can accept that state object and detect its changes, and call apply function to convert the object to state for UI.

Usage

Thinking of implementing "Counter App" whose count is managed by CounterState which extends ValueNotifier, you can implement MultipleCounter which build Text displaying multiplied value of count like below.

class MultipleCounter extends AcceptableStatefulWidget {
  const MultipleCounter({Key? key}) : super(key: key);

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

class _MultipleCounterState
    extends AcceptableStatefulWidgetState<MultipleCounter> {

  @override
  void acceptProviders(Accept accept) {
    accept<CounterState, int>(
      watch: (state) => state.value,
      apply: (value) => _value = value * 2,
      perform: (value) {
        // call imperative methods such as showDialog()
      }
    );
  }

  late int _value;

  @override
  Widget build(BuildContext context) {
    return Text('$_value');
  }
}

Most important method is acceptProviders which is called right before build is called at the first time.

acceptProviders passes accept function as an argument. You can call accept with two arguments of function.

watch

watch returns what object to watch. In the example above, MultipleCounter observes state.value: state is an instance of CounterState.

This method is quite similar to context.select<Value, T>() of provider package.

apply

apply represents how to apply provided data, which is decided by calling watch, to the state of UI.

In the example above, value is doubled and assigned to _value field of State. Then _value is used in the build method.

As apply is called before build is called when observing value is changed, you don't need to convert provided value in build method.

perform

perform is called after rebuild is completed. Imperative methods, such as showDialog, Navigator.push, can be called here using current value of state objects.

Motivation

By using AcceptableStatefulWidget you don't have to

  • make state object which represents specific UI, which is typically called ViewModel.

and you can

  • manage state of the UI in State class which totally follows the basic idea of Flutter.
  • concentrate on providing data via Provider without considering "how it is used" in each UI.

flutter_acceptable's People

Contributors

chooyan-eng avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.