Flutter Mvp框架

基础view页

abstact class BasePage extends StatefulWidget{}

BaseProvider内容提供者

typedef void onStartLoadPage(bool refresh);
typedef void onSuccessLoadPage(dynamic data);
typedef void onFailedLoadPage(String msg);
typedef void onDoneLoadPage();
abstract class BaseProvide,<V extends BaseService> extends ChangeNotifier{
	OnstartLoadPage onStartLoadPage;
	OnSuccessLoadPage onSuccessLoadPage;
	OnFailedLoadPage onFailLoadPage;
	OnDoneLoadPage onDoneLoadPage;
	bool isShowLoading;
	bool isEmptyError;
	CompositeSubscription compositeSubscription=CompositeSubscription();
	V remoteService;
	BaseProvide({
		this.onStartLoadPage,
		this.onSuccessLoadPage,
		this.onFailLoadPage;
		this.onDoneLoadPage,
		this.isShowLoading:true,
		this.isEmptyError:false,
	});
	addSubscription(StreamSubcription subscription){
		compositeSubscription.add(subscription);
	}
	void initRemoteService();
	@override
	void dispose(){
		super.dispose();
		compositionSubcription.dispose();
	}
}

数据请求Service

abstract class BaseService {
}

数据状态

abstract class BaseState <V extends BasePage,P extends BaseProvide> extends State<V>{
	P p;
	@override
	void initState(){
		super.initState();
		initProvide();
		initViewData();
	}
	@override
  	void dispose() {
	    	super.dispose();
	    	if (provide != null) {
	      	provide.dispose();
   		}
  	 }
	
	@override
	Widget build(BuildContext context) {
	    return ChangeNotifierProvider.value(
	      value: provide,
	      child: Scaffold(
	          appBar: buildAppBar(),
	          body: Consumer<P>(
	            builder: (context, _provide, _) {
	              return _buildContentView();
	            },
	          )),
	     );
  	}
	void initProvide();
	void initViewData();
	Widget buildContent();
	
	Widger _buildContentView(){
	if (provide.isShowLoading) {
  		return LoadingView.initLoading(false);
	}
    if (provide.isEmptyError) {
      return LoadingView.initLoading(true); 
    }
	 return buildContent();
	}
}

实例化

class AdvertPage extends BasePage {
  @override
  State<StatefulWidget> createState() {
    return _AdvertState();
  }
}
class _AdvertState extends BaseState<AdvertPage,AdvertProvide> {
	@override
	void initState() {
	   super.initState();
	   FlutterSplashScreen.hide();
	}
  @override
  void initProvide() {
  	///采用withCallBack(...)组合方法
    provide = new AdvertProvide.withCallBack(_onStartLoadPage,
        _onSuccessLoadPage, _onFailedLoadPage, _onDoneLoadPage);
    provide.initRemoteService();
    _refreshController = new RefreshController();
  }

  @override
  AppBar buildAppBar() {
    return new AppBar(
      title: new Text("test"),
    );
  }

  @override
  Widget buildContent() {
    return new Column(
      children: <Widget>[
        new RaisedButton(
            onPressed: () {
              provide.titleText = "you now me";
            },
            child: new Text('${provide.titleText}')),
        new Expanded(
            child: MultiListView(
          items: provide.listItems,
          refreshController: this._refreshController,
          onHeaderRefreshCallback: _onHeaderRefresh,
          onFooterRefreshCallback: _onFootLoadMore,
        ))
      ],
    );
  }

  @override
  void initViewData() {
    provide.getItems();
  }

  _onHeaderRefresh() {
    provide.refreshItems();
  }

  _onFootLoadMore() {
    _refreshController.sendBack(false, RefreshStatus.noMore);
  }

  _onStartLoadPage(bool refresh) {
    provide.isShowLoading = !refresh;
  }

  _onSuccessLoadPage(dynamic result) {
    provide.listItems = result;
    provide.isShowLoading = false;
  }

  _onFailedLoadPage(String msg) {
    provide.isEmptyError = true;
  }

  _onDoneLoadPage() {
    _refreshController.sendBack(true, RefreshStatus.idle);
  }
}

import 'dart:developer';
import 'package:cityhelper/base/BaseProvide.dart';
import 'package:cityhelper/data/MultiListItem.dart';
import 'package:cityhelper/pages/advert/AdvertService.dart';

class AdvertProvide extends BaseProvide<AdvertService> {
  AdvertProvide();

  AdvertProvide.withCallBack(
      OnStartLoadPage onStartLoadPage,
      OnSuccessLoadPage onSuccessLoadPage,
      OnFailedLoadPage onFailedLoadPage,
      OnDoneLoadPage onDoneLoadPage)
      : super(
            onStartLoadPage: onStartLoadPage,
            onSuccessLoadPage: onSuccessLoadPage,
            onFailedLoadPage: onFailedLoadPage,
            onDoneLoadPage: onDoneLoadPage);

  List<ListItem> _listItems = [];

  List<ListItem> get listItems => _listItems;

  set listItems(List<ListItem> arr) {
    _listItems = arr;
    notifyListeners();
  }

  String _titleText = "welcome to the new world";

  String get titleText => _titleText;

  set titleText(String value) {
    _titleText = value;
    notifyListeners();
  }

  @override
  void initRemoteService() {
    remoteService = new AdvertService();
  }

  void getItems() {
    var s = remoteService.getItems().doOnListen(() {
      log("doOnListen");
      if (onStartLoadPage != null) onStartLoadPage(false);
    }).doOnData((result) {
      log("doOnData");
      if (onSuccessLoadPage != null) onSuccessLoadPage(result);
    }).doOnError((e, stacktrace) {
      log("doOnError");
      if (onFailedLoadPage != null) onFailedLoadPage(e.toString());
    }).doOnDone(() {
      log("doOnDone");
      if (onDoneLoadPage != null) onDoneLoadPage();
    }).listen((data) {
      log("listen");
    });
    addSubscription(s);
  }

  void refreshItems() {
    var s = remoteService.refreshItems().doOnListen(() {
      if (onStartLoadPage != null) onStartLoadPage(true);
    }).doOnData((result) {
      log("doOnData");
      if (onSuccessLoadPage != null) onSuccessLoadPage(result);
    }).doOnError((e, stacktrace) {
      log("doOnError");
      if (onFailedLoadPage != null) onFailedLoadPage(e.toString());
    }).doOnDone(() {
      log("doOnDone");
      if (onDoneLoadPage != null) onDoneLoadPage();
    }).listen((data) {
      log("listen");
    });
    addSubscription(s);
  }
}

///AdvertService
import 'package:cityhelper/base/BaseService.dart';
import 'package:cityhelper/data/MultiListItem.dart';
import 'package:rxdart/rxdart.dart';

class AdvertService extends BaseService {
  Observable<List<ListItem>> getItems() {
    final items = new List<ListItem>.generate(
      20,
      (i) => i % 6 == 0
          ? new HeadingItem("Heading $i")
          : new MessageItem("Sender $i", "Message body $i"),
    );
    return Observable.just(items).delay(new Duration(seconds: 3));
  }

  Observable<List<ListItem>> refreshItems() {
    final items = new List<ListItem>.generate(
      20,
      (i) => i % 6 == 0
          ? new HeadingItem("Heading 1$i")
          : new MessageItem("Sender 1$i", "Message body 1$i"),
    );
    return Observable.just(items).delay(new Duration(seconds: 3));
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值