flutter mvvm框架(基于provider)

gitHub地址flutter mvvm框架(基于provider)_第1张图片

 

 

 

 

 

 

 

 

 

上图为Android 中的mvvm

MVVM模式分为Model,View,ViewModel 三个部分

(1).Model:数据层,包含数据实体和对数据实体的操作
(2).View:界面层,对应于Activity,XML,View,负责数据显示以及用户交互。
(3).ViewModel:关联层,将Model和View进行绑定,Model或者View更改时,实时刷新对方

flutter mvvm框架(基于provider)_第2张图片

flutter 也是类似

State的封装

 

abstract class BaseState
    extends State {
  late M viewModel;
  late String pageName;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return build(context);
  }

  Widget initWiew() {
    return Consumer(builder: (BuildContext, provie, _) {
      return viewModel.isLoading ? LoadingWidget() : buildView();
    });
  }

  Widget buildView();

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    print("didChangeDependencies: ${pageName}界面");
  }

  @override
  void didUpdateWidget(covariant T oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    print("didUpdateWidget: ${pageName}界面");
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    print("deactivate:${pageName}界面");
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    print("dispose 离开${pageName}界面");
    print("销毁${pageName}界面");
  }
}

viewModel封装

/**
 *
 * @class name:
 * @class describe
 *@class name:
 * @author XUIELIANG
 * @time 
 * @change
 * @chang time
 * @class describe
 */
import 'package:flutter/widgets.dart';

abstract class BaseViewModel with ChangeNotifier {
  BuildContext context;

  BaseViewModel(this.context);

  bool _isLoading = false;

  bool get isLoading => _isLoading;

  set isLoading(bool isLoading) {
    if (_isLoading != isLoading) {
      _isLoading = isLoading;
    }
    notifyListeners();
  }

  ///刷新数据
  @protected
  Future refreshData({bool isShowLoading = true});
}

 

 

 

 

 

你可能感兴趣的:(flutter,Dart)