cubix 0.1.4+5 copy "cubix: ^0.1.4+5" to clipboard
cubix: ^0.1.4+5 copied to clipboard

outdated

A predictable state management library that based on BLoc library. Easy to use and maintenance.

example/main.dart

// create a cubix
import 'package:cubix/cubix.dart';
import 'package:cubix/cubix_widgets.dart';
import 'package:flutter/material.dart';

class CounterCubix extends Cubix<int> {
  CounterCubix() : super(0);

  // method base action
  void increment() => state++;

  @override
  onDispatch(ActionBase action) {
    // log an action dispatching
    // cannot track method base actions (increment)
    // ignore: avoid_print
    print(action.runtimeType.toString());
  }
}

// class base action
// using SyncAction for sync code
class DecrementAction extends SyncAction<void, int> {
  final int step;

  DecrementAction([this.step = 1]);

  @override
  body() {
    // update state
    state -= step;
  }
}

class DecrementAsyncAction extends AsyncAction<void, int> {
  final int step;

  DecrementAsyncAction([this.step = 1]);

  @override
  body() async {
    // delay in 2 seconds
    await Future.delayed(const Duration(seconds: 2));
    // perform increment by dispatch another action
    dispatch(DecrementAction(step));
  }
}

void main() => runApp(const App());

class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  build(context) {
    return CubixProvider(
      // build widget from CounterCubix
      // the builder receives BuildContext and Cubix objects
      child: CounterCubix.new.build(
        (context, cubix) {
          return Column(
            children: [
              // getting state of cubix
              Text(cubix.state.toString()),
              ElevatedButton(
                // resolve cubix from context object
                onPressed: context.cubix(CounterCubix.new).increment,
                child: const Text('Increment'),
              ),
              ElevatedButton(
                // dispatch class base action
                onPressed: () => cubix.dispatch(DecrementAction()),
                child: const Text('Decrement'),
              ),
              ElevatedButton(
                // dispatch class base action with args
                onPressed: () => cubix.dispatch(DecrementAsyncAction(2)),
                child: const Text('Decrement Async'),
              )
            ],
          );
        },
      ),
    );
  }
}
0
likes
0
pub points
3%
popularity

Publisher

unverified uploader

A predictable state management library that based on BLoc library. Easy to use and maintenance.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

collection, flutter, flutter_bloc

More

Packages that depend on cubix