A stateless stateful state management package that is not stateless.

Overview

Stateless

A stateless stateful state management package that is not stateless.

stateless coverage style: very good analysis License: MIT pub package


DISCLAIMER: Please do not use this package in production, it uses Dart Magic under the hood that could break whenever Flutter wants it to break.

Introduction

The goal of this package is to see if we can have state management without having to care about state management. The learning curve of Stateless should be at a minimal, knowledge developers have from known Flutter APIs should be transferable, like initState, dispose and build.

Getting Started

In your flutter project, add the following in your pubspec.yaml:

  dependencies:
    stateless: ^0.1.0

Usage

The classic Flutter Counter Widget, rewritten with Stateless:

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Stateless Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(title: 'Stateless Demo Home Page'),
    );
  }
}

// We define the interface that will describe our state data.
abstract class MyCounterState {
  late int counter;
}

// We extends from Stateless to create our stateless stateful widget.
class MyHomePage extends Stateless implements MyCounterState {
  MyHomePage({super.key, required this.title});

  final String title;

  @override
  void initState() {
    super.initState();
    // Initialize our state data.
    counter = 0;
  }

  void showSnackBar() {
    // We can access the BuildContext from anywhere in our widget.
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('The count is at: $counter')),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(title)),
      body: const Center(child: MyCounterText()),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(
            // Update the counter by just simply incrementing it.
            onPressed: () => counter++,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
          const SizedBox(height: 8),
          FloatingActionButton(
            onPressed: showSnackBar,
            tooltip: 'Show SnackBar',
            child: const Icon(Icons.lightbulb),
          ),
        ],
      ),
    );
  }
}

As you can see the API is barely any different from normal Flutter widgets, by extending from Stateless and implementing our state interface we can just update the interface properties and it will automatically know that it's state has changed and therefore triggers a rebuild.

Accessing state data in a child

Quite often you want to access state data from a parent in the tree, well Stateless is capable of doing that for you!

Lets re-imagine the above counter app into two parts, one is the Stateless widget and the second part is a normal StatelessWidget that displays the counter value.

class MyHomePage extends Stateless implements MyCounter {
  ... 

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(title)),
      // We build our custom text counter widget here.
      body: const Center(child: MyCounterText()),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(
            onPressed: () => counter++,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
          const SizedBox(height: 8),
          FloatingActionButton(
            onPressed: showSnackBar,
            tooltip: 'Show SnackBar',
            child: const Icon(Icons.lightbulb),
          ),
        ],
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    // We can then observe our MyCounter for state changes.
    final myCounter = context.observe<MyCounter>();

    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        const Text('You have pushed the button this many times:'),
        Text(
          '${myCounter.counter}',
          style: Theme.of(context).textTheme.headline4,
        ),
      ],
    );
  }
}

Our MyCounterText widget can simply observe the state of our MyCounter and read the current counter value from it.

Contributing

Interested in contributing? We love pull request! See the Contribution document for more information.

Comments
  • chore(deps): bump very_good_analysis from 3.0.1 to 3.0.2

    chore(deps): bump very_good_analysis from 3.0.1 to 3.0.2

    Bumps very_good_analysis from 3.0.1 to 3.0.2.

    Release notes

    Sourced from very_good_analysis's releases.

    v3.0.2

    • fix: remove deprecated invariant_booleans lint
    • fix: deprecate unawaited (use unawaited from dart:async instead).
    Changelog

    Sourced from very_good_analysis's changelog.

    3.0.2

    • fix: remove deprecated invariant_booleans lint
    • fix: deprecate unawaited (use unawaited from dart:async instead).
    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies dart 
    opened by dependabot[bot] 1
  • fix: `_findParent` is going through the whole widget tree

    fix: `_findParent` is going through the whole widget tree

    Description

    _findParent was going through the whole widget tree, turning it into O(N) while it should have been O(1).

    Type of Change

    • [ ] ✨ New feature (non-breaking change which adds functionality)
    • [x] πŸ› οΈ Bug fix (non-breaking change which fixes an issue)
    • [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] 🧹 Code refactor
    • [ ] βœ… Build configuration change
    • [ ] πŸ“ Documentation
    • [ ] πŸ—‘οΈ Chore
    opened by wolfenrain 1
  • chore(deps): bump very_good_analysis from 3.0.1 to 3.1.0

    chore(deps): bump very_good_analysis from 3.0.1 to 3.1.0

    Bumps very_good_analysis from 3.0.1 to 3.1.0.

    Release notes

    Sourced from very_good_analysis's releases.

    v3.1.0

    Thanks to @​lsaudon for the contribution πŸ’™

    v3.0.2

    • fix: remove deprecated invariant_booleans lint
    • fix: deprecate unawaited (use unawaited from dart:async instead).
    Changelog

    Sourced from very_good_analysis's changelog.

    3.1.0

    3.0.2

    • fix: remove deprecated invariant_booleans lint
    • fix: deprecate unawaited (use unawaited from dart:async instead).
    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies dart 
    opened by dependabot[bot] 0
  • chore: release v0.1.1

    chore: release v0.1.1

    Description

    Type of Change

    • [ ] ✨ New feature (non-breaking change which adds functionality)
    • [ ] πŸ› οΈ Bug fix (non-breaking change which fixes an issue)
    • [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] 🧹 Code refactor
    • [ ] βœ… Build configuration change
    • [ ] πŸ“ Documentation
    • [x] πŸ—‘οΈ Chore
    opened by wolfenrain 0
  • feat: support finding a `Stateless` by interface

    feat: support finding a `Stateless` by interface

    Description

    Currently you can't specify the implement interface as the generic of Stateless.of, this PR adds this feature at the cost of a more expensive look-up because we can't cache the result.

    Type of Change

    • [x] ✨ New feature (non-breaking change which adds functionality)
    • [ ] πŸ› οΈ Bug fix (non-breaking change which fixes an issue)
    • [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] 🧹 Code refactor
    • [ ] βœ… Build configuration change
    • [ ] πŸ“ Documentation
    • [ ] πŸ—‘οΈ Chore
    opened by wolfenrain 0
  • feat: make stateless inheritable

    feat: make stateless inheritable

    Description

    Type of Change

    • [x] ✨ New feature (non-breaking change which adds functionality)
    • [ ] πŸ› οΈ Bug fix (non-breaking change which fixes an issue)
    • [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change)
    • [ ] 🧹 Code refactor
    • [ ] βœ… Build configuration change
    • [ ] πŸ“ Documentation
    • [ ] πŸ—‘οΈ Chore
    opened by wolfenrain 0
Releases(v0.1.1)
Cross-platform flutter plugin for reading and writing NFC tags. Not maintained anymore - not looking for new maintainer, fork instead.

nfc_in_flutter NFC in Flutter is a plugin for reading and writing NFC tags in Flutter. It works on both Android and iOS with a simple stream interface

Andi Semler 113 Sep 28, 2022
State Persistence - Persist state across app launches. By default this library store state as a local JSON file called `data.json` in the applications data directory. Maintainer: @slightfoot

State Persistence Persist state across app launches. By default this library store state as a local JSON file called data.json in the applications dat

Flutter Community 70 Sep 28, 2022
An extension to the bloc state management library which lets you create State Machine using a declarative API

An extension to the bloc state management library which lets you create State Machine using a declarative API

null 25 Nov 28, 2022
A powerful state machine for MobX management, that can be used in almost any application state.

A powerful state machine for MobX management, which can be used in almost any application state. It has 3 states - loading, success, error - and is pe

Daniel Magri 8 Oct 31, 2022
Practice building basic animations in apps along with managing app state by BLoC State Management, Flutter Slider.

Practice building basic animations in apps along with managing app state by BLoC State Management including: Cubit & Animation Widget, Flutter Slider.

TAD 1 Jun 8, 2022
A Stateless Application of BookMyShow

book_my_show A Stateless Application of BookMyShow Lab: Link to EMI-Calculator REPO Getting Started This project is a starting point for a Flutter app

Deepanshu Goel 0 Mar 11, 2022
Shopify Tag and Product Management App using Flutter and Riverpod State Management

Myshopify App A Simple Flutter Application project to get List of Tags, Products and Product Details from shopify https://shopicruit.myshopify.com/adm

Idowu Tomiwa 5 Nov 12, 2022
An all-in-one Fllutter package for state management, reactive objects, animations, effects, timed widgets etc.

Frideos An all-in-one package for state management, streams and BLoC pattern, animations and timed widgets, effects. Contents 1. State management Gett

Francesco Mineo 188 Dec 23, 2022
Flutter getx template - A Flutter Template using GetX package for State management, routing and Dependency Injection

Flutter GetX Template (GetX, Dio, MVVM) This Flutter Template using GetX package

Tareq Islam 6 Aug 27, 2022
A quiz-app bundled with Provider package for state management.

quiz_app A quiz-app bundled with Provider package for state management. Implemented Features MVVM (Provider+ChangeNotifiers) state sharing and state m

null 3 Jul 26, 2022
Flutter package to hide out whole application or sections if you are not paid for the job done.

not_paid Worried about your payment? Using this package you can provide a non paid version of your app to the client that consistently starts fading o

hd-motion 11 Nov 30, 2021
Music player application for android. It's uses MVVM architecture and Provider & ValueNotifier state management.

music-player-flutter Flutter music player application which is my personal project published to play store. Project structures are as following,

null 30 Jul 10, 2022
Doctor Consultation App in Flutter containing splash screen on boarding screen Routing state management Dash board Bottom navigation Decorated Drawer and Doctors Screen in the last.

Online doctor Consultation App UI in Flutter Doctor Consultation App UI in Flutter Visit Website Features State Management Navigation Bar Responsive D

Habib ullah 14 Jan 1, 2023
A project built to learn Flutter state management

Tienda Tienda is a simple product store mobile application. Tienda is a flutter application which was solely built to learn App state and local state

Derrick Keteku 0 Nov 28, 2021
Flutter Control is complex library to maintain App and State management. Library merges multiple functionality under one hood. This approach helps to tidily bound separated logic into complex solution.

Flutter Control is complex library to maintain App and State management. Library merges multiple functionality under one hood. This approach helps to

Roman Hornak 23 Feb 23, 2022
Easy Form State Management using BLoC pattern

?? Dart and Flutter Package ?? Easy Form State Management using BLoC pattern ?? Wizard/stepper forms, asynchronous validation, dynamic and conditional fields, submission progress, serialization and more! ??

GiancarloCode 406 Jan 8, 2023
An easy Flutter state management library

EbloX An easy Flutter state management library.It is similar to Bloc, but it uses a lot of annotations and separates business logic from UI through th

arcticfox 9 Oct 29, 2022