Site icon Flutter Packages | Pub dev Packages – Flutter Mobile App World

Flutter state management library inspired with MVC pattern

A super-powerful flutter state management library inspired with MVC pattern with very flexible dependency injection.



Features

Core Concepts

Preview

In this image the process was like this:

And magic happens! All the inputs were retained and not just that but also including the page where you left off. Navigation history is also persisted which means pressing the system back button will navigate you to the correct previous page.

Dark Mode

This theming is done manually using momentum.

Source Code for this Example App

This example app shows how powerful momentum is.

Quick Start

You only have to install one package and momentum doesn’t have any peer dependencies.

Create

To get started, flutter create an app. Name it however you want.

Installing

  1. Add this to your package’s pubspec.yaml file:dependencies: momentum: ^1.1.6It is not recommended to use the one from GitHub because the changes there are subject to breaking changes on future pushes to the repository.
  2. You can install this package from the command-line:flutter pub getAlternatively, your editor might support flutter pub get.
  3. Now in your Dart code, you can use:import ‘package:momentum/momentum.dart’;You only have to import this one file alone and you’ll be able to use all momentum API.

Counter App Example

Copy this example counter app code and run it:

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

void main() {
  runApp(
    Momentum(
      controllers: [CounterController()],
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Momentum State Management',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: HomeWidget(),
    );
  }
}

class CounterController extends MomentumController<CounterModel> {
  @override
  CounterModel init() {
    return CounterModel(
      this,
      value: 0,
    );
  }

  void increment() {
    var value = model.value; // grab the current value
    model.update(value: value + 1); // update state (rebuild widgets)
    print(model.value); // new or updated value
  }
}

class CounterModel extends MomentumModel<CounterController> {
  CounterModel(
    CounterController controller, {
    this.value,
  }) : super(controller);

  final int value;

  @override
  void update({
    int value,
  }) {
    CounterModel(
      controller,
      value: value ?? this.value,
    ).updateMomentum();
  }
}

class HomeWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Momentum Counter'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            MomentumBuilder(
              controllers: [CounterController],
              builder: (context, snapshot) {
                var counter = snapshot<CounterModel>();
                return Text(
                  '${counter.value}',
                  style: Theme.of(context).textTheme.headline4,
                );
              },
            ),
          ],
        ),
      ),
      // we don't need to rebuild the increment button, we can skip the MomentumBuilder
      floatingActionButton: FloatingActionButton(
        onPressed: Momentum.controller<CounterController>(context).increment,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Please Note

Download Flutter state management library source code on GitHub

https://github.com/xamantra/momentum

Exit mobile version