Skip to content

folaoluwafemi/vanilla_state

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A very simple way to manage immutable state in flutter without having too much dependencies

Features

Has the following

  • State management: VanillaNotifier
  • State listener widgets: VanillaListener, VanillaBuilder
  • VanillaNotifier dependency injection widget: InheritedVanilla
  • Helper utils for making immutable state management easier and faster: EqualityChecker, StateWithStatus, VanillaUtilsMixin

Getting started

dependencies:
  vanilla_state: ^1.0.0

or

  flutter pub add vanilla_state
import 'package:vanilla_state/vanilla_state.dart';

Usage

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

void main() {
  runApp(const MyApp());
}

class CounterNotifier extends VanillaNotifier<int> {
  CounterNotifier() : super(0);

  void increment() {
    state = state + 1;
  }

  void decrement() {
    state = state - 1;
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: InheritedVanilla<CounterNotifier>(
        createNotifier: () => CounterNotifier(),
        child: const MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme
            .of(context)
            .colorScheme
            .inversePrimary,
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              'You have pushed the button this many times:',
            ),
            VanillaBuilder<CounterNotifier, int>(
              builder: (context, state) {
                return Text(
                  '$state',
                  style: Theme
                      .of(context)
                      .textTheme
                      .headlineMedium,
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: context
            .read<CounterNotifier>()
            .increment,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Additional information

To create issues, prs or otherwise contribute in anyway check out the package repository home

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published