一个Flutter的MVVM(Model-View-ViewModel)实现, 它使用基于属性(property)的数据绑定在视图模型(ViewModel)与视图(View)之间建立关联,并通过视图模型(ViewModel)驱动视图(View)变化。以此简化Flutter开发过程中繁重的状态管理与widget树控制。
import'package:flutter/widgets.dart';import'package:mvvm/mvvm.dart';import'dart:async';//defineViewModelclassDemo1ViewModelextendsViewModel{Demo1ViewModel(){//definebindablepropertyproperty<String>("time",initial:"");//timerstart();}start(){Timer.periodic(constDuration(seconds:1),(_){varnow=DateTime.now();//callsetValuesetValue<String>("time","${now.hour}:${now.minute}:${now.second}");});}}//defineViewclassDemo1extendsView<Demo1ViewModel>{Demo1():super(Demo1ViewModel());@overrideWidgetbuildCore(BuildContextcontext){returnContainer(margin:EdgeInsets.symmetric(vertical:100),padding:EdgeInsets.all(40),//bindingchild:$.watchFor("time",builder:$.builder1((t)=>Text("$t",textDirection:TextDirection.ltr))));}}//runvoidmain()=>runApp(Demo1());点击空白处退出提示
评论