GitXplorerGitXplorer
w

stateless

public
39 stars
2 forks
1 issues

Commits

List of commits on branch main.
Verified
8b09db29144b4a18eb8609e069d3df4cd19963b1

chore(deps): bump very_good_analysis from 3.0.1 to 3.1.0 (#7)

ddependabot[bot] committed 2 years ago
Verified
400409b7b80c5133cc2131cdf48033da03614a00

chore: release v0.1.1 (#5)

wwolfenrain committed 2 years ago
Verified
9708f4f4a99a92f72db36582f11f832fe8c0ff03

feat: support finding a `Stateless` by interface (#4)

wwolfenrain committed 2 years ago
Verified
314f000adf9502c43f2340637be0376bc9eca9b8

fix: `_findParent` is going through the whole widget tree (#3)

wwolfenrain committed 2 years ago
Verified
1fb234e28e0c9287376adea8f28422ad660baa76

feat: make stateless inheritable (#2)

wwolfenrain committed 2 years ago
Verified
f7f82cb4a9828f9ee02832defce4f19dc2552da8

refactor: rename the complex example

wwolfenrain committed 2 years ago

README

The README file for this repository.

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.