mvu_flutter 1.0.1 copy "mvu_flutter: ^1.0.1" to clipboard
mvu_flutter: ^1.0.1 copied to clipboard

discontinued
outdated

State management and architecture library that allows to write Flutter Apps under MVU/The Elm Architecture approach.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:mvu_flutter/mvu_flutter.dart';
import 'package:flutter/foundation.dart';

part 'main.freezed.dart';

@freezed
class CounterModel extends Model with _$CounterModel {
  const factory CounterModel(int currentValue) = _CounterModel;
}

class IncrementMsg extends Msg<CounterModel> {
  final int amount;
  IncrementMsg({required this.amount});
}

class DecrementMsg extends Msg<CounterModel> {
  final int amount;
  DecrementMsg({required this.amount});
}


class UpdateCounter extends Update<CounterModel> {
  @override
  ProducedState<CounterModel> update(
      Msg<CounterModel> message, CounterModel model) {
  if (message is IncrementMsg) {
    return ProducedState(
        model.copyWith(currentValue: model.currentValue + message.amount));
  }
  if (message is DecrementMsg) {
    return ProducedState(
        model.copyWith(currentValue: model.currentValue - message.amount));
  }
  return ProducedState(model);
  }
}


class CounterView extends View<CounterModel> {
  @override
  Widget view(BuildContext context, CounterModel model) => Scaffold(
    body: Center(
        child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('Counter value: ${model.currentValue}'),
          ElevatedButton(
            onPressed: dispatchCallback(IncrementMsg(amount: 10)),
            child: Text("Increment"),
          ),
          ElevatedButton(
            onPressed: dispatchCallback(DecrementMsg(amount: 1)),
            child: Text("Decrement"),
          ),
        ],
      ),
    ),
  );
}

final counterProgram = () => Program(
      model: CounterModel(0),
      view: () => CounterView(),
      update: UpdateCounter(),
);


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MVU Demo',
      home: counterProgram(),
    );
  }
}

void main() => runApp(MyApp());
7
likes
140
points
32
downloads

Publisher

unverified uploader

Weekly Downloads

State management and architecture library that allows to write Flutter Apps under MVU/The Elm Architecture approach.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on mvu_flutter