apn_state 3.0.0 copy "apn_state: ^3.0.0" to clipboard
apn_state: ^3.0.0 copied to clipboard

An Appnormal flutter package, providing state management based on Flutter Provider and Event Bus for state-to-state communication.

example/lib/main.dart

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

void main() {
  // Run the app
  runApp(MyApp());
}

/// Make a class that holds your state variables
class CounterState extends BaseState<String> {
  int counter = 0;

  @override
  String? convertError(e) {
    print(e);
    return 'Something went wrong';
  }
}

/// Update your state by using events
class IncrementCounterEvent extends BaseStateEvent<CounterState> {
  @override
  Future<void> handle() async {
    state.counter++;
    state.notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      builder: (context, child) {
        return BaseConsumerView<CounterState>(
          create: () => CounterState(),
          builder: (context, state, child) => Scaffold(
            appBar: AppBar(
              title: Text('State example'),
            ),
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    'You have pushed the button this many times:',
                  ),
                  Text(
                    '${state.counter}',
                    style: Theme.of(context).textTheme.headline4,
                  ),
                ],
              ),
            ),
            floatingActionButton: FloatingActionButton(
              onPressed: () => state.dispatch(IncrementCounterEvent()),
              child: Icon(Icons.add),
            ),
          ),
        );
      },
    );
  }
}
1
likes
120
pub points
29%
popularity

Publisher

verified publisherappnormal.com

An Appnormal flutter package, providing state management based on Flutter Provider and Event Bus for state-to-state communication.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, provider

More

Packages that depend on apn_state