ERNI-Academy / starterkit-mobile-application-flutter

ERNI Academy mobile boilerplate to start a cross-platform Flutter mobile application
MIT License
9 stars 2 forks source link

State management improvement #20

Closed dustincatap closed 1 year ago

dustincatap commented 1 year ago

Explain in 1-2 sentences. Say what this is about.

The blueprint currently uses MVVM as its state management. It uses ValueListenable combined with ValueListenableBuilder which are available out-of-the-box in Flutter.

class HomeViewModel {
  final ValueNotifier<int> _count = ValueNotifier(0);

  ValueListenable<int> get count => _count;

  void increment() => _count.value++;
}

class HomeView extends StatelessWidget {
  final HomeViewModel _viewModel = HomeViewModel();

  const HomeView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home')),
      body: ValueListenableBuilder<int>(
        valueListenable: _viewModel.count,
        builder: (context, count, child) {
          return Text('Count value: $count');
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _viewModel.increment,
        child: const Icon(Icons.add),
      ),
    );
  }
}

These classes are simple and good enough to be used instead of adding 3rd party plugins.

Motivation Goals

It is known fact that Flutter has multiple state management options. Eventually, as a developer, you will be asking "Which am I going to use"?

In scope

TODO

Out of scope

TODO

Risks / unknowns

TODO

Examples

No response

dustincatap commented 1 year ago

Closing this. Will stick to the current state management of the project.