mvvm 0.3.0 mvvm: ^0.3.0 copied to clipboard
A Flutter MVVM. It uses property-based data binding to establish a connection between the ViewModel and the View, and drives the View changes through the ViewModel.
A Flutter MVVM (Model-View-ViewModel) implementation. It uses property-based data binding to establish a connection between the ViewModel and the View, and drives the View changes through the ViewModel.
一个 Flutter 的 MVVM(Model-View-ViewModel) 实现。 它使用基于属性 (property) 的数据绑定在视图模型 (ViewModel) 与视图 (View) 之间建立关联,并通过视图模型 (ViewModel) 驱动视图 (View) 变化。
#
import 'package:flutter/widgets.dart';
import 'package:mvvm/mvvm.dart';
import 'dart:async';
// ViewModel
class Demo1ViewModel extends ViewModel {
Demo1ViewModel() {
registryProperty(#time, BindableProperty.$value(initial: DateTime.now()));
start();
}
start() {
Timer.periodic(const Duration(seconds: 1),
(_) => setValue<DateTime>(#time, DateTime.now()));
}
}
// View
class Demo1View extends View<Demo1ViewModel> {
Demo1View() : super(Demo1ViewModel());
format(DateTime dt) => "${dt.hour}:${dt.minute}:${dt.second}";
@override
Widget build(BuildContext context) {
return Center(
// binding
child: $.watchFor<DateTime>(#time,
builder: (context, time, child) =>
Text(format(time), textDirection: TextDirection.ltr)));
}
}
// run
void main() => runApp(Demo1View());
APIs #
ViewContext ($.*) #
Properties
Methods
- watch
- watchFor
- watchAny
- watchAnyFor
- watchAnyForMap
- merge
- mergeMap
- $cond
- $condFor
- $if
- $ifFor
- $switch
- $switchFor
- builder0
- builder1
- builder2
- b0
- b1
- b2
override
ViewModel #
Methods
- registryProperty
- getProperty
- requireProperty
- getPropertyOf
- requirePropertyOf
- getProperties
- propertyValue
- propertyAdaptive
- propertyAsync
- requireValue
- getValue
- setValue
- setValues
- updateValue
- notify
override
View #
Properties
Methods
override