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() {
propertyValue<String>(#time, initial: "");
start();
}
start() {
Timer.periodic(const Duration(seconds: 1), (_) {
var now = DateTime.now();
setValue<String>(#time, "${now.hour}:${now.minute}:${now.second}");
});
}
}
// View
class Demo1View extends View<Demo1ViewModel> {
Demo1View() : super(Demo1ViewModel());
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: 100),
padding: EdgeInsets.all(40),
// binding
child: $.watchFor<String>(#time,
builder:
$.builder1((t) => Text(t, textDirection: TextDirection.ltr))));
}
}
// run
void main() => runApp(Demo1View());
- watch
- watchFor
- watchAny
- watchAnyFor
- $cond
- $condFor
- $if
- $ifFor
- $switch
- $switchFor
- adapt
- builder0
- builder1
- builder2
override
override
override