Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

surfstudio/flutter-surf-mwwm

Repository files navigation

Surf MWWM

Build Status Coverage Status Pub Version Pub Likes Pub popularity Flutter Platform

This package made by Surf.

Description

Reflection of widget in a single entity

Installation

Add surf_mwwm to your pubspec.yaml file:

dependencies:
  surf_mwwm: ^1.0.0

You can use both stable and dev versions of the package listed above in the badges bar.

Example

  1. Create WM class
class CounterWidgetModel extends WidgetModel {
  CounterWidgetModel(
    WidgetModelDependencies dependencies,
    this.navigator,
    this._key,
  ) : super(dependencies);

  final NavigatorState navigator;
  final GlobalKey<ScaffoldState> _key;

  StreamedState<int> counterState = StreamedState(0);

  final incrementAction = VoidAction();
  final showInit = StreamedAction<int>();

  @override
  void onLoad() {
    _listenToActions();
    super.onLoad();
  }

  void _listenToActions() {
    incrementAction.bind((_) {
      counterState.accept(counterState.value + 1);
    }).listenOn(
      this,
      onValue: (_) {},
    );

    showInit.bind((_) {
      ScaffoldMessenger.of(_key.currentContext!).showSnackBar(
        const SnackBar(
          content: Text('init'),
        ),
      );
    }).listenOn(this, onValue: (_) {});

    counterState.stream.where((c) => c.isEven).skip(1).listenOn(
      this,
      onValue: (c) {
        navigator.push(
          MaterialPageRoute<void>(
            builder: (ctx) => Scaffold(
              body: Column(
                children: [
                  TextField(
                    autofocus: true,
                    onChanged: (_) {},
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
  1. Create Screen
class CounterScreen extends MwwmWidget<CounterComponent> {
  CounterScreen({Key? key})
      : super(
          key: key,
          dependenciesBuilder: (context) =>
              CounterComponent(Navigator.of(context)),
          widgetStateBuilder: () => _CounterScreenState(),
          widgetModelBuilder: createCounterModel,
        );
}

class _CounterScreenState extends OldWidgetState<CounterWidgetModel> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: context.getComponent<CounterComponent>().scaffoldKey,
      appBar: AppBar(
        title: const Text('Counter Demo'),
      ),
      body: StreamBuilder(
        stream: wm.counterState.stream,
        initialData: 0,
        builder: (context, snapshot) {
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text('You have pushed the this many times:'),
                Text(
                  '${snapshot.data}',
                  style: Theme.of(context).textTheme.caption,
                ),
                TextField(
                  autofocus: true,
                  onChanged: (_) {},
                ),
              ],
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: wm.incrementAction,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
  1. Create Route
class CounterScreenRoute extends MaterialPageRoute<void> {
  CounterScreenRoute() : super(builder: (ctx) => CounterScreen());
}

Changelog

All notable changes to this project will be documented in this file.

Issues

For issues, file directly in the Issues section.

Contribute

If you would like to contribute to the package (e.g. by improving the documentation, solving a bug or adding a cool new feature), please review our contribution guide first and send us your pull request.

Your PRs are always welcome.

How to reach us

Please feel free to ask any questions about this package. Join our community chat on Telegram. We speak English and Russian.

Telegram

License

Apache License, Version 2.0

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published