我正在尝试使用 Pop Scope 处理我的嵌套导航器。
因为在嵌套路由中,当我点击设备上的后退按钮时,应用程序将关闭。
onInvoked 方法已被弃用,所以我无法解决这个问题。也无法弄清楚实现。
这是我处理导航器的 main_screen.dart 文件:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:calistimapp/widgetlar/bottom_navigation.dart';
import 'package:calistimapp/providers/main_screen_provider.dart';
import 'package:calistimapp/screens/job_search.dart';
import 'package:calistimapp/screens/cuzdan.dart';
import 'package:calistimapp/screens/islerim.dart';
import 'package:calistimapp/screens/profil.dart';
import 'package:calistimapp/screens/kategori_sayfalari/komi_garson_screen.dart';
import 'package:calistimapp/screens/kategori_sayfalari/etkinlik_gorevli_screen.dart';
import 'package:calistimapp/screens/kategori_sayfalari/otel_gorevlisi_screen.dart';
import 'package:calistimapp/screens/kategori_sayfalari/diger_isler_screen.dart';
import 'package:calistimapp/screens/job_details_screen.dart';
import 'package:calistimapp/screens/my_job_details_screen.dart';
import 'package:calistimapp/screens/pending_applications_screen.dart';
import 'package:calistimapp/screens/other_accepted_jobs_screen.dart';
import 'package:calistimapp/screens/update_worker_screen.dart';
class MainScreen extends ConsumerStatefulWidget {
final String initialRoute;
const MainScreen({Key? key, required this.initialRoute}) : super(key: key);
@override
ConsumerState<MainScreen> createState() => _MainScreenState();
}
class _MainScreenState extends ConsumerState<MainScreen> {
late final PageController _pageController;
@override
void initState() {
super.initState();
_pageController = PageController();
WidgetsBinding.instance.addPostFrameCallback((_) {
final initialPage = _getInitialPage(widget.initialRoute);
ref.read(mainScreenProvider.notifier).setPage(initialPage);
_pageController.jumpToPage(initialPage);
});
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final currentIndex = ref.watch(mainScreenProvider);
return Scaffold(
body: PageView(
controller: _pageController,
physics: const NeverScrollableScrollPhysics(),
onPageChanged: (index) {
ref.read(mainScreenProvider.notifier).setPage(index);
},
children: [
Navigator(
onGenerateRoute: (settings) => _generateRoute(settings, const JobSearchScreen()),
),
Navigator(
onGenerateRoute: (settings) => _generateRoute(settings, const CuzdanScreen()),
),
Navigator(
onGenerateRoute: (settings) => _generateRoute(settings, const IslerimScreen()),
),
Navigator(
onGenerateRoute: (settings) => _generateRoute(settings, const ProfilScreen()),
),
],
),
bottomNavigationBar: CustomBottomNavigationBar(
currentIndex: currentIndex,
onTap: (index) {
_pageController.jumpToPage(index);
},
),
);
}
int _getInitialPage(String route) {
switch (route) {
case '/job_search':
return 0;
case '/cuzdan':
return 1;
case '/islerim':
return 2;
case '/profil':
return 3;
default:
return 0;
}
}
PageRouteBuilder _generateRoute(RouteSettings settings, Widget defaultPage) {
Widget page;
switch (settings.name) {
case '/komi_garson':
page = const KomiGarsonScreen();
break;
case '/etkinlik_gorevli':
page = const EtkinlikGorevliScreen();
break;
case '/otel_gorevlisi':
page = const OtelGorevlisiScreen();
break;
case '/diger_isler':
page = const DigerIslerScreen();
break;
case '/job_details':
final jobId = settings.arguments as String;
page = JobDetailsScreen(jobId: jobId);
case '/my_job_details':
final jobId = settings.arguments as String;
page = MyJobDetailsScreen(jobId: jobId);
case '/pending_applications':
page = const PendingApplicationsScreen();
break;
case '/other_accepted_jobs':
page = const OtherAcceptedJobsScreen();
break;
case '/update-worker':
page = const UpdateWorkerScreen();
break;
default:
page = defaultPage;
}
return PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) => page,
transitionsBuilder: (context, animation, secondaryAnimation, child) {
const begin = Offset(1.0, 0.0);
const end = Offset.zero;
const curve = Curves.easeInOut;
var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
var offsetAnimation = animation.drive(tween);
return SlideTransition(
position: offsetAnimation,
child: child,
);
},
transitionDuration: const Duration(milliseconds: 300),
);
}
}
我尝试使用后退按钮返回到之前的页面。
在嵌套的导航器/路线中,应用程序关闭而不是返回到上一个屏幕。
WillPopScope 小部件可以与上面的方法一起使用,以便在提供嵌套导航器的情况下正确处理 Flutter 应用程序中的后退按钮。它控制当您按下后退按钮时会发生什么。由于现在已弃用 onInvoked 方法,因此 WillPopScope 是实现此目的的正确方法。因此,您需要使用 WillPopScope 包装这些嵌套导航器并手动处理后退导航行为。