基础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));
}
}