global_state 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 48

Global State #

Global State is an automatic Widget refresh library for Flutter. Inspired by react-recollect.

Global State is a very simple and easy to use state management that causes Widgets to always show the current value in the store. It is specially useful for Stateless Widgets, causing them to be rebuild when values on the store change

The example below shows how easy it is to create an interactive app using just StatelessWidgets.

Example - How to use #

-class Clicker extends StatelessWidget {
+class Clicker extends StatelessWidgetGS {

  @override
-Widget build(BuildContext context) {
+Widget buildGS(BuildContext context) {
    return Scaffold(
      body: Center(
+          child: Text(store['clicks'].toString())
        ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
+        onPressed: () => store['clicks']++    // change 'clicks' from anywhere in the app and the widget will get updated
      ),
    );
  }
}

store is an ObservedMap instance that comes bundled in with the library. Own 'stores' can be created if preferred: Map myStore = ObservedMap();.

When a value in the 'store' changes, GlobalState will only rebuild the Widgets that used that value.

More #

Global State is a wrapper library over Floop. It does exactly the same with a different name.

Check the github repository for more details, examples and up to date information:

https://github.com/icatalud/floop

[0.1.1] - 21/07/2019

Typo fixes in documentation.

[0.1.0] - 21/07/2019

Publishes first version Global State.

example/example.dart

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

/// This example exists to satisfy dart publishing requirements.
/// More complete examples are available at the root of the project on Github.
/// https://github.com/icatalud/floop

void main() {
  store['clicks'] = 0;
  runApp(MaterialApp(
      title: 'Clicker',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Clicker()));
}

class Clicker extends StatelessWidgetGS {
  @override
  Widget buildGS(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(
          store['clicks'].toString(),
          style: TextStyle(
            color: Colors.red,
            fontSize: 100,
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add), onPressed: () => store['clicks']++),
    );
  }
}

// The following are alternative implementations.

class ClickerStateful extends StatefulWidget {
  @override
  State<ClickerStateful> createState() => ClickerState();
}

class ClickerState extends StateGS<ClickerStateful> {
  @override
  Widget buildGS(BuildContext context) {
    return Scaffold(
      body: Center(
          child: Text(store['clicks'].toString(),
              style: TextStyle(
                color: Colors.red,
                fontSize: 100,
              ))),
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add), onPressed: () => store['clicks']++),
    );
  }
}

// Simplest example.

class SimpleClicker extends StatelessWidgetGS {
  @override
  Widget buildGS(BuildContext context) {
    return Scaffold(
      body: Center(child: Text(store['clicks'].toString())),
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add), onPressed: () => store['clicks']++),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  global_state: ^0.1.1

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:global_state/global_state.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
48
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (floop).

Dependencies

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