初始定位后如何导航到ShellRoute?

问题描述 投票:0回答:1

shellRoute效果很好,但是我需要在选择语言之后选择第一页,因为他们选择了neeed返回用户到shellRoute(或“//home”)。因此,如果您有一个想法,我如何打开它或其他方式。谢谢! 在我的主角。

GoRouter router = GoRouter( initialLocation: '/firstPage', routes: [ ShellRoute( navigatorKey: GlobalKey<NavigatorState>(), // Ensures nested navigation works builder: (context, state, child) { debugPrint("Building ShellRoute with path: ${state.uri}"); int index = getIndexFromPath(state.uri.path); return CurveBar(initialIndex: index); }, routes: [ GoRoute( path: '/home', builder: (context, state) => isDesktop ? WebHomePage() : HomePage(), ), GoRoute( path: '/favorites', builder: (context, state) => FavoritesPage(), ), GoRoute( path: '/search', builder: (context, state) => const SearchPage(), ), GoRoute( path: '/profile', builder: (context, state) => const ProfilePage(), ), GoRoute( path: '/cart', builder: (context, state) => const CartPage(), ), ], ), GoRoute( path: '/firstPage', builder: (context, state) => FirstPage(), ),
在那里我的第一页以及我如何尝试路线

context.go('/home');

,所以它不起作用,我不知道为什么:

class FirstPage extends StatefulWidget { const FirstPage({super.key}); @override State<FirstPage> createState() => _FirstPageState(); } class _FirstPageState extends State<FirstPage> { final TextEditingController _languageController = TextEditingController(); late Logic _logic; // Save reference to Logic String languageImage = ''; // Listen to language changes void _listenLanguageChanges() { print('FirstPage - _listenLanguageChanges'); _logic.addListener(_onLanguageChanged); _updateLanguageImage(_logic.currentLanguage); // Initialize image on load } @override void didChangeDependencies() { print('FirstPage - didChangeDependencies'); super.didChangeDependencies(); // Ensure _logic is used only after initialization. _logic = context.read<Logic>(); // Initialize it here _listenLanguageChanges(); // Listen for language changes } @override void dispose() { print('FirstPage - dispose'); _languageController.dispose(); _logic.removeListener(_onLanguageChanged); super.dispose(); } void _onLanguageChanged() { print('FirstPage - _onLanguageChanged'); if (mounted) { _updateLanguageImage(_logic.currentLanguage); } } void _updateLanguageImage(String language) { debugPrint('_updateLanguageImage'); if (mounted) { setState(() { if (language == 'English') { languageImage = 'assets/images/background/CFen.webp'; } else if (language == 'Руский') { languageImage = 'assets/images/background/CFru.webp'; } else if (language == 'Қазақша') { languageImage = 'assets/images/background/CFkz.webp'; } }); } } @override Widget build(BuildContext context) { print('FirstPage - build'); return Scaffold( body: Stack( alignment: Alignment.bottomCenter, children: [ createPage( image: languageImage, // Dynamically updated language image title: S.of(context).firstPageTitleOne, description: S.of(context).firstPageDescriptionOne, child: MyTextField( hintText: 'Выберите язык', language: true, icon: CarbonIcons.language, controller: _languageController, ), ), Positioned( bottom: 60, right: 30, child: FloatingActionButton( backgroundColor: Constants.primaryColor, onPressed: _onNextPressed, child: const Icon(Icons.arrow_forward_ios, color: Colors.white), ), ), ], ), ); } void _onNextPressed() async { debugPrint('FirstPage - _onNextPressed'); final selectedLanguage = _languageController.text; SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString('selectedLanguage', selectedLanguage); await prefs.setBool('onboardingComplete', true); debugPrint("Selected language: $selectedLanguage"); if (mounted) { _logic.setLanguage(selectedLanguage); debugPrint('Navigating to /home...'); Future.delayed(Duration(milliseconds: 100), () { if (mounted) { Navigator.push( context, MaterialPageRoute( builder: (context) => CurveBar(initialIndex: 0), ), ); } }); } } } // --- ... --- //
我尝试:

Navigator.push( context, MaterialPageRoute( builder: (context) => CurveBar(initialIndex: 0), ), );

GoRouter.of(context).go('/home'); // Navigate to the home page

并尝试过去的第一页到ShellRoute,然后尝试使用:

isLanguageSet ? '/firstPage' : '/home'
    

flutter web navigation gorouter
1个回答
0
投票
onPressed: () { GoRouter.of(context).go('/home'); //_onNextPressed(); },

主要:
GoRouter router = GoRouter(
  initialLocation: isLanguageSet
      ? '/home'
      : '/firstPage',
  routes: [
    GoRoute(
      path: '/firstPage',
      builder: (context, state) => FirstPage(),
    ),
    ShellRoute(
      navigatorKey: GlobalKey<NavigatorState>(),
      builder: (context, state, child) {
        debugPrint("Building ShellRoute with path: ${state.uri}");
        int index = getIndexFromPath(state.uri.path);
        return CurveBar(initialIndex: index);
      },
      routes: [
        GoRoute(
          path: '/home',
          builder: (context, state) => HomePage(),
        ),


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.