<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Flutter MVVMFlutter 的 Model-View-ViewModel 實現(xiàn)

          聯(lián)合創(chuàng)作 · 2023-09-30 04:37

          一個 Flutter 的 MVVM(Model-View-ViewModel) 實現(xiàn), 它使用基于屬性 (property) 的數(shù)據(jù)綁定在視圖模型 (ViewModel) 與視圖 (View) 之間建立關(guān)聯(lián),并通過視圖模型 (ViewModel) 驅(qū)動視圖 (View) 變化。以此簡化 Flutter 開發(fā)過程中繁重的狀態(tài)管理與 widget 樹控制。

          import 'package:flutter/widgets.dart';
          import 'package:mvvm/mvvm.dart';
          import 'dart:async';
          
          // define ViewModel
          class Demo1ViewModel extends ViewModel {
            Demo1ViewModel() {
              // define bindable property
              property<String>("time", initial: "");
              // timer
              start();
            }
          
            start() {
                Timer.periodic(const Duration(seconds: 1), (_) {
                  var now = DateTime.now();
                  // call setValue
                  setValue<String>("time", "${now.hour}:${now.minute}:${now.second}");
                });
            }
          }
          
          // define View
          class Demo1 extends View<Demo1ViewModel> {
            Demo1() : super(Demo1ViewModel());
          
            @override
            Widget buildCore(BuildContext context) {
              return Container(
                  margin: EdgeInsets.symmetric(vertical: 100),
                  padding: EdgeInsets.all(40),
          
                  // binding
                  child: $.watchFor("time", 
                      builder: $.builder1((t) => 
                        Text("$t", textDirection: TextDirection.ltr))));
            }
          }
          
          // run
          void main() => runApp(Demo1());

          瀏覽 18
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲一区二区自拍 | 啪啪视频免费网站 | 嫩草午夜少妇在线影视 | 人人搞人人摸 | 91精品XXX |