我只是使用命名路由导航到新页面,一旦触发toNamed,新屏幕会闪现,然后关闭控制台会显示“onDelete called”,用导航替换路由。
- main.dart
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
),
initialRoute: AppRoutes.splash,
getPages: Pages.getPages,
);
}
}
- 页面
abstract class Pages {
static List<GetPage> getPages = [
GetPage(name: AppRoutes.splash, page: () => SplashPage(), binding: SplashBinding()),
GetPage(name: AppRoutes.login, page: () => LoginPage(), binding: LoginBinding()),
GetPage(name: AppRoutes.home, page: () => HomePage(), binding: HomeBinding(), transition: Transition.fadeIn),
//issue in last one.
GetPage(name: AppRoutes.newTask, page: () => NewTaskPage(), binding: TaskBindings(), transition: Transition.fadeIn),
];
}
注意:导航到NewTaskPage()时,
- 绑定
class TaskBindings extends Bindings{
@override
void dependencies() {
Get.put<TaskController>(TaskController());
}
}
- 任务控制器
class TaskController extends GetxController{
@override
void onInit() {
//todo: fetch Task info.
super.onInit();
}
}
- 任务页面
class NewTaskPage extends GetView<TaskController> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red,
appBar: AppBar(
title: Text("NEW TASK"),
),
body: Center(child: Text("No Working"),),
);
}
}
- 导航功能
void navigate2NewTask() {
Get.toNamed(AppRoutes.newTask);
}
- 调用上述函数
class HomePage extends GetView<HomeController> {
final GlobalKey<ScaffoldState> _homeScfKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
//SEE HERE
onPressed: () => controller.navigate2NewTask(),
child: Icon(Icons.add),
),
key: _homeScfKey,
drawer: HomeDrawer(),
)
}
}
新任务页面闪烁后,控制台显示以下内容:
[GETX] GOING TO ROUTE /new-task
[GETX] Instance "TaskController" has been created
[GETX] Instance "TaskController" has been initialized
flutter: Splash navigation
[GETX] REPLACE ROUTE /new-task
[GETX] NEW ROUTE /app-home
flutter: Splash navigation
[GETX] "TaskController" onDelete() called
[GETX] "TaskController" deleted from memory
请帮忙。