Flutter模块化封装插件get_modular_wrappers的使用
Flutter模块化封装插件get_modular_wrappers的使用
使用
修改 main
函数
首先,在项目的 main
函数中,我们需要使用 GetAppWrapper
来初始化应用。
import 'package:get_modular_wrappers/get_modular_wrappers.dart';
void main() {
runApp(
GetAppWrapper(
initialRoute: AppRoutes.root,
unknownPageRoute: AppRoutes.notFound,
coreModule: CoreModule(),
),
);
}
创建 core_module.dart
文件
接下来,创建一个名为 core_module.dart
的文件,并定义一个继承自 GetModule
的类 CoreModule
。在这个类中,我们定义了应用的核心模块和路由。
import 'package:get_modular_wrappers/get_modular_wrappers.dart';
import 'modules/home_module';
class CoreModule extends GetModule {
@override
List<GetBind> get binds => [];
@override
List<GetRoute> get routes => [
GetModuleRoute(
module: HomeModule(),
),
//..更多模块
];
}
创建 home_module.dart
文件
然后,创建一个名为 home_module.dart
的文件,用于定义应用的具体模块和页面。
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_modular_wrappers/get_modular_wrappers.dart';
import '../app_routes.dart';
class HomeRoutes {
static const root = '/';
}
class HomeModule extends GetModule {
@override
List<GetRoute> get routes => [
HomeRoute(),
];
}
class HomeRoute extends GetRoute {
HomeRoute();
@override
GetPage get asGetPage => GetPage(
name: AppRoutes.root,
page: () => const HomePage(),
);
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text('Home'),
),
);
}
}
创建 app_routes.dart
文件
最后,创建一个名为 app_routes.dart
的文件,用于映射应用的路由。
import 'modules/home_module.dart';
class AppRoutes {
static const root = HomeRoutes.root;
//static const login = LoginPage.routeName;
static const notFound = '/not-found';
}
更多关于Flutter模块化封装插件get_modular_wrappers的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复