mvvm_bloc

Flutter MVVM Architecture with BLoC Pattern

Getting Started

ViewModel


class MyPageViewModel extends ViewModel {
  @override
  String get name => 'my_page';

  final LifeCycleOwner? parent;

  ServiceLogic({
    Key? key,
    required Widget Function(ViewModel) builder,
    this.parent,
  }) : super(key: key, builder: builder);

  factory MyPageViewModel.build({LifeCycleOwner? parent}) {
    return MyPageViewModel(
      parent: parent,
      builder: (vm) => MyPageView(vm as MyPageViewModel),
    );
  }

  late final LiveData<int> $counter = LivaData(0).owner(this);

  increment(){
    $counter.value++;
  }

  decrement(){
    $counter.value--;
  }
}

View

class MyPageView extends app.View<MyPageViewModel> {
  const MyPageView(
      MyPageViewModel logic, {
        Key? key,
      }) : super(logic, key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text(
          'You have pushed the button this many times:',
        ),
        $watch(vm.$counter, builder: (_, value) {
          return Text(
            '$value',
          );
        }),
        RaisedButton(
          child: Text('+'),
          onPressed: () => vm.increment(),
        ),
        RaisedButton(
          child: Text('-'),
          onPressed: () => vm.decrement(),
        ),
      ],
    );
  }
}