mvp的全称为Model-View-Presenter,Model提供数据,View负责显示,Controller/Presenter负责逻辑的处理。MVP与MVC有着一个重大的区别:在MVP中View并不直接使用Model,它们之间的通信是通过Presenter (MVC中的Controller)来进行的,所有的交互都发生在Presenter内部,
运用在Flutter中
步骤一:编写基类(base_presenter,base_state,base_view,lifecycle)
class BasePresenter<V extends BaseView>extends IPresenter{
V view;
CancelToken _cancelToken;
@override
void deactivatePresenter() {
}
@override
void disposePresenter() {
//请求取消
if(_cancelToken!=null){
if(_cancelToken.isCancelled){
_cancelToken.cancel();
}
}
}
@override
void initPresenter() {
}
}
abstract class BaseState<T extends StatefulWidget,V extends BasePresenter> extends State<T> implements BaseView{
V presenter;
V createPresenter();
BaseState() {
presenter = createPresenter();
presenter.view = this;
}
@override
BuildContext getContext() {
// TODO: implement getContext
return context;
}
bool _isShowDialog = false;
@override
void hideLoading() {
if (mounted && _isShowDialog){
_isShowDialog = false;
// NavigatorUtil.goBack(context);
}
}
@override
void showLoading() {
/// 避免重复弹出
if (mounted && !_isShowDialog){
_isShowDialog = true;
try{
showTransparentDialog(
context: context,
barrierDismissible: false,
builder:(_) {
return WillPopScope(
onWillPop: () async {
// 拦截到返回键,证明dialog被手动关闭
_isShowDialog = false;
return Future.value(true);
},
child: const ProgressDialog(hintText:"加载中..."),
);
}
);
}catch(e){
/// 异常原因主要是页面没有build完成就调用Progress。
print(e);
}
}
}
@override
void showToast(String msg) {
BotToast.showText(text: msg);
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
presenter?.disposePresenter();
}
@override
void deactivate() {
// TODO: implement deactivate
super.deactivate();
presenter?.deactivatePresenter();
}
@override
void initState() {
// TODO: implement initState
super.initState();
presenter?.initPresenter();
}
}
abstract class BaseView{
BuildContext getContext();
//显示加载loading
void showLoading();
//隐藏loading
void hideLoading();
//显示吐司
void showToast(String msg);
}
//describe:周期管理
abstract class LifeCycle{
void initPresenter();
void deactivatePresenter();
void disposePresenter();
}
步骤二:在代码中的运用 以某个页面为例
//我的页面
class MinPage extends StatefulWidget {
@override
MinPageState createState() => MinPageState();
}
//mvp
class MinPageState extends BaseState<MinPage,MinPresenter> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
body: SingleChildScrollView(child:
Column(
children: buildLists(),
)
,),
);
}
List<Widget> buildLists(){
List<Widget> listWidgets=[];
listWidgets.add(MinTopWidget());
listWidgets.add(ThemeUtils.isDark(context)?Sps.hPureDark16:Sps.hPure16);
listWidgets.add(_buildShop());
listWidgets.add(ThemeUtils.isDark(context)?Sps.hPureDark16:Sps.hPure16);
listWidgets.add(MineItemsWidget());
return listWidgets;
}
Widget _buildShop(){
return Container(
height: ScreenAdapter.height(170),
child:InkWell(
child: Row(
children: <Widget>[
Sps.buildWGaps(32.0),
Expanded(
child:
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("多多积分信用商城",style: TextStyle(fontSize: ScreenAdapter.setSize(32),color: Color(0xFFF89F31)),),
Text("超多好礼等你兑换~",style: TextStyle(fontSize: ScreenAdapter.setSize(28),color:ThemeUtils.isDark(context)?Colors.white:Color(0x611D1D1C)))
],
),
),
Image.asset("assets/images/icon_min_shop.png",height: ScreenAdapter.height(48),width: ScreenAdapter.width(48),),
Icon(Icons.keyboard_arrow_right,size: ScreenAdapter.setSize(50),color: Color(0xFFA5AAB3),),
Sps.buildWGaps(32.0),
],
),
onTap: (){
BotToast.showText(text: "积分");
},
),
);
}
// Widget test(){
// Column(
// children: <Widget>[
// InkWell(
// child: Container( alignment: Alignment.center,height: ScreenAdapter.height(100), child: Text("深色模式")),
// onTap: (){
// //0表示浅色,1表示深色,2表示跟随系统。
// ThemeMode themeMode = ThemeMode.dark ;
// /// 与上方等价,provider 4.1.0添加的拓展方法
// context.read<ThemeProvider>().setTheme(themeMode);
// },
// ),
// InkWell(
// child: Container( alignment: Alignment.center,height: ScreenAdapter.height(100), child: Text("浅色模式")),
// onTap: (){
// ThemeMode themeMode = ThemeMode.light ;
// /// 与上方等价,provider 4.1.0添加的拓展方法
// context.read<ThemeProvider>().setTheme(themeMode);
// },
// ),
// InkWell(
// child: Container( alignment: Alignment.center,height: ScreenAdapter.height(100), child: Text("跟随系统")),
// onTap: (){
// ThemeMode themeMode = ThemeMode.system ;
// /// 与上方等价,provider 4.1.0添加的拓展方法
// context.read<ThemeProvider>().setTheme(themeMode);
// },
// )
//
// ],
// )
// }
@override
MinPresenter createPresenter() {
// TODO: implement createPresenter
return MinPresenter();
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive =>true;
}
对应的MinPresenter
class MinPresenter extends BasePresenter<MinPageState >{
var platform = const MethodChannel('com.guizhoudcb.anshun_app/md5');
//放一些网络请求 数据库操作
noticeData(pageindex, pageSize) async {
RealRepository.getInstance().realdata(pageindex, pageSize,8).then((onValue) {
//view.hideLoading();
if (onValue.status == 200) {
view.setData(onValue.data.records);
} else{
Toast.show(onValue.msg);
}
}, onError: (e) {
view.hideLoading();
});
}
@override
void initPresenter() {
// TODO: implement initPresenter
super.initPresenter();
}
}