rs5173 / ui_logic

A light state management lib.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A light state management lib.

Usage

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ui_logic/ui_logic.dart';

void main() {
  testWidgets('ui logic test', (WidgetTester tester) async {
    // init a logic
    final logic = Logic();
    // UIStateBuilder
    final builder = UIStateBuilder(
      logic: logic,
      states: const [UIStateA, UIStateB, UIStateC],
      builder: (context, state) {
        if (state is UIStateA) {
          // do sth
        } else if (state is UIStateB) {
          // do sth
        } else if (state is UIStateC) {
          // do sth
        }
        return const SizedBox();
      },
    );
    // emit a ui event
    logic.emit(UIEventA());
    // test
    await tester.pumpWidget(builder);
  });
}

class UIEventA extends UIEvent {}

class UIEventB extends UIEvent {}

class UIStateA extends UIState {}

class UIStateB extends UIState {}

class UIStateC extends UIState {}

class Logic extends UILogic {
  @override
  void handleUIEvents(UIEvent event, UIState? lastState) {
    if (event is UIEventA) {
      // do sth
    } else if (event is UIEventB) {
      // do sth
    }
    // reply a state to ui
    reply(event, UIStateA());
  }
}

About

A light state management lib.

License:MIT License


Languages

Language:Dart 98.0%Language:HTML 2.0%