Flutter MVP 快速开发
练习做的项目,来做一些学习笔记,具体框架的使用方法就不做记录,网上很多,这里只做 MVP 框架搭建,框架借鉴了其他大佬写的一些代码,然后按照自己又做了些调整
基础配置
先来看下依赖的库都有哪些,也不一定全部都用到了 ,看自己项目需要吧
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
# 路由
fluro: ^1.5.1
# 简单数据存储
shared_preferences: ^0.5.3+4
# 依赖注入
get_it: ^3.0.1
# 吐司
fluttertoast: ^3.1.3
# 尺寸适配
flutter_screenutil: ^0.6.0
# 二维码条形码扫描
flutter_qr_reader: ^1.0.3
# 权限申请
permission_handler: ^3.0.0
# 网络请求
dio: ^2.0.1
# 手机网络状态监听
connectivity: ^0.4.3+7
# 消息总线
event_bus: ^1.1.0
# 刷新 加载
pull_to_refresh: ^1.5.4
# Rx
rxdart: ^0.21.0
在入口函数 main() 中进行部分框架的初始化:
void main() {
Router router = Router();
Routes.configureRoutes(router);
Application.router = router;
Application.setupLocator();
runApp(MyApp());
}
- 1、2、3 行是用来配置项目中路由的(fluro 框架)
- Application.setupLocator() 初始化 get_it 依赖注入框架,相当于 Android 中 dagger2
- 其中在 Application 中还做了屏幕适配配置
看下 Application 中的代码吧:
class Application {
static Router router;
static GlobalKey<NavigatorState> globalKey = GlobalKey();
static SharedPreferences sp;
static double screenWidth;
static double screenHeight;
static d