yjbanov/tasteful


An different take on widgets with state

License: MIT

Language: Dart


package:tasteful

This Flutter package provides a new kind of stateful widget, TastefulWidget. It solves a subset of problems that the StatefulWidget a little more elegantly:

  • TastefulWidget does not require a second State class. Instead it can operate on an already existing class that can represent your widget's state.
  • Unlike State your state class can (and generally should) be immutable.

Example

The following example implements the classic counter widget. It uses data provided with the widget (greeting) as well as state (counter represented by int) to build a UI.

class Counter extends TastefulWidget<int> {
  Counter(this.greeting);

  final String greeting;

  int createInitialState() => 0;

  @override
  Widget build(TastefulBuildContext context) {
    return Column(children: [
      Text('$greeting, ${context.state}!'),
      GestureDetector(
        onTap: () {
          context.state = context.state + 1;
        },
        child: Text('Increment'),
      ),
    ]);
  }
}

When to use TastefulWidget

  • When you prefer your state objects to be immutable and that's sufficient to express you app's UI logic.
  • When a class already exists that fully represents your widget's state, and creating a second wrapper class would be wasteful.

TastefulWidget is not a StatefulWidget replacement

StatefulWidget supports more complex scenarios via a richer lifecycle API, such as didChangeDependencies, didUpdateWidget, and dispose.

Project Statistics

Sourcerank 3
Repository Size 2.93 KB
Stars 0
Forks 0
Watchers 0
Open issues 0
Dependencies 2
Contributors 1
Tags 0
Created
Last updated
Last pushed

Top Contributors See all

Yegor

Packages Referencing this Repo

tasteful
a different take on stateful widgets.
Latest release 0.0.1 - Published

Something wrong with this page? Make a suggestion

Last synced: 2019-05-30 00:59:47 UTC

Login to resync this repository