該項目爲flutter的mvp的基礎架構,主要封裝了mvp基礎類,使用dio進行網絡框架封裝,使用fluro進行路由跳轉;android
思想和我封裝android的mvp的思想基本一致,可是因爲dart和kotlin的語法差異,可能有些地方仍是不太同樣,若是想了解我原生的mvp封裝能夠參考文章blog.csdn.net/villa_mou/a…git
contractgithub
abstract class SecondView extends IView<SecondPresenter>{
void showSucc(Article article);
}
abstract class SecondPresenter extends IPresenter<SecondView>{
Future loadContacts();
}
複製代碼
presenter網絡
class SecondPresenterImp extends BasePresenterKt<SecondView>
implements SecondPresenter {
@override
Future loadContacts() async {
}
}
複製代碼
view架構
class SecondWidget extends BaseMvpWidget {
String name;
String title;
SecondWidget(this.name,this.title);
@override
BaseMvpWidgetState<ITopPresenter, BaseMvpWidget> getMvpState() {
return SecondPage(name,title);
}
}
class SecondPage extends BaseMvpWidgetState<SecondPresenter, SecondWidget>
implements SecondView {
String name;
String title;
SecondPage(this.name,this.title);
@override
SecondPresenter mPresenter = SecondPresenterImp();
@override
void initData() {
mPresenter.loadContacts();
}
@override
void showSucc(Article article) {
toast(article.data[0].name);
}
@override
void initView() {
}
@override
Widget buildWidget(BuildContext context) {
var widget = new ListView(
padding: new EdgeInsets.symmetric(vertical: 8.0),
children: [
Text("aaaaaaaaaaaaaaa"),
Text("傳遞過來的參數{$name$title}"),
]
);
return widget;
}
}
複製代碼
網絡框架使用dio,這個玩意有頁面銷燬的時候取消網絡請求的cancelToken,比較符合以前原生開發的業務場景,具體使用框架
HttpGo.getInstance()
.get(UrlConstants.ARTICLE
, ShowLoadingIntercept(mView)
, (data) {
var article =Article.fromJson(data);
mView.showSucc(article);
}
, errorCallBack: () {
mView.setErrorWidgetVisible(true);
});
複製代碼
ShowLoadingIntercept(mView)爲是否顯示loading彈窗,loading彈窗封裝在基類的,也就是原生的baseActivity,經過ItopView進行調用的,而且基類封裝了空佈局,錯誤佈局,loading佈局,只須要在presenter中經過mview調用便可; 這裏基類的封裝參考了另一個項目async
fluro簡化了Flutter的路由開發,也是目前Flutter生態中最成熟的路由框架。ide
首先須要配置路由路徑佈局
class Routes {
static String root = "/";
static String second = "/second";
static void configureRoutes(Router router) {
//router.notFoundHandler=Handler()
router.define(root, handler: homeHandler);
router.define(second, handler: secondHandler);
}
}
複製代碼
而後配置handler(帶參和不帶參)ui
var homeHandler = Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> parameters) {
return HomeTabWidget();
});
var secondHandler = Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> parameters) {
String name = parameters["name"].first;
String title = parameters["title"].first;
return new SecondWidget(name, title);
});
複製代碼
最後路由跳轉
String route = '/second?name=${Uri.encodeComponent("張三")}&title=${Uri.encodeComponent("你好")}';
Application.router.navigateTo(context, route,transition: TransitionType.fadeIn);
複製代碼
鄙人能力有限,暫時只能封裝成這樣,若是有小夥伴有好的建議,歡迎一塊兒分享討論;
參考項目: