flutter 在_CustomTabViewState中找不到路线RouteSettings(“Map屏幕”,空)的生成器

vof42yt1  于 2022-12-05  发布在  Flutter
关注(0)|答案(2)|浏览(96)

构建一个多自动售货机杂货店应用程序。代码运行得很好,但是当我添加persistent_bottom_nav_bar插件并使用PersistentTabView作为底部导航器时,我的控制台中出现Could not find a generator for route RouteSettings("map-screen", null) in the _CustomTabViewState错误。
每次单击按钮时都会出现此错误。
同样,当我单击“退出”按钮时,我得到此错误Could not find a generator for route RouteSettings("welcome-screen", null) in the _CustomTabViewState.
下面是我的main.dart代码

import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:multi_vending_grocery_app/providers/auth_provider.dart';
import 'package:multi_vending_grocery_app/providers/location_provider.dart';
import 'package:multi_vending_grocery_app/providers/store_provider.dart';
import 'package:multi_vending_grocery_app/screens/home_screen.dart';
import 'package:multi_vending_grocery_app/screens/landing_screen.dart';
import 'package:multi_vending_grocery_app/screens/login_screen.dart';
import 'package:multi_vending_grocery_app/screens/main_screen.dart';
import 'package:multi_vending_grocery_app/screens/map_screen.dart';
import 'package:multi_vending_grocery_app/screens/register_screen.dart';
import 'package:multi_vending_grocery_app/screens/splash_screen.dart';
import 'package:multi_vending_grocery_app/screens/welcome_screen.dart';
import 'package:provider/provider.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (_) => AuthProvider(),
        ),
        ChangeNotifierProvider(
          create: (_) => LocationProvider(),
        ),
        ChangeNotifierProvider(
          create: (_) => StoreProvider(),
        ),
      ],
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: SplashScreen.id,
      routes: {
        SplashScreen.id: (context) => const SplashScreen(),
        HomeScreen.id: (context) => const HomeScreen(),
        WelcomeScreen.id: (context) => const WelcomeScreen(),
        MapScreen.id: (context) => const MapScreen(),
        LoginScreen.id: (context) => const LoginScreen(),
        RegistrationScreen.id: (context) => const RegistrationScreen(),
        LandingScreen.id: (context) => const LandingScreen(),
        MainScreen.id: (context) => const MainScreen(),
      },
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.deepPurple, fontFamily: 'Lato'),
    );
  }
}

下面是我的profile_screen.dart代码

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:multi_vending_grocery_app/screens/welcome_screen.dart';

class ProfileScreen extends StatelessWidget {
  const ProfileScreen({Key? key}) : super(key: key);
  static const String id = 'profile-screen';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
      children: [
        const Center(
          child: Text("Profile Section"),
        ),
        ElevatedButton(onPressed: () {
          FirebaseAuth.instance.signOut();
          Navigator.pushReplacementNamed(context, WelcomeScreen.id);
        }, child: const Text("Sign-Out"))
      ],
    ));
  }
}

下面是我的welcome_screen.dart代码

import 'package:flutter/material.dart';
import 'package:multi_vending_grocery_app/providers/auth_provider.dart';
import 'package:multi_vending_grocery_app/providers/location_provider.dart';
import 'package:multi_vending_grocery_app/screens/map_screen.dart';
import 'package:multi_vending_grocery_app/screens/on_board_screen.dart';
import 'package:provider/provider.dart';

class WelcomeScreen extends StatefulWidget {
  const WelcomeScreen({Key? key}) : super(key: key);
  static const String id = 'welcome-screen';

  @override
  State<WelcomeScreen> createState() => _WelcomeScreenState();
}

class _WelcomeScreenState extends State<WelcomeScreen> {
  @override
  Widget build(BuildContext context) {
    final auth = Provider.of<AuthProvider>(context);
    final Size size = MediaQuery.of(context).size;
    TextEditingController _phoneNumberController = TextEditingController();
    final locationData = Provider.of<LocationProvider>(context, listen: false);
    bool _validPhoneNumber = false;

    void showBottomSheet(context) {
      showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return StatefulBuilder(
            builder: (BuildContext context, StateSetter stateSetter) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Padding(
                    padding: EdgeInsets.only(top: 20.0, left: 20),
                    child: Text(
                      "Login",
                      style:
                          TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                    ),
                  ),
                  const Padding(
                    padding: EdgeInsets.only(top: 5.0, left: 20),
                    child: Text(
                      "Enter Your Phone Number",
                      style: TextStyle(fontSize: 18, color: Colors.grey),
                    ),
                  ),
                  Padding(
                    padding:
                        const EdgeInsets.only(top: 8.0, left: 20, right: 20),
                    child: TextField(
                      controller: _phoneNumberController,
                      maxLength: 10,
                      style: const TextStyle(fontSize: 20),
                      keyboardType: TextInputType.phone,
                      decoration: const InputDecoration(
                        prefix: Text(
                          "+91  ",
                          style: TextStyle(fontSize: 20),
                        ),
                        labelText: "Enter Your Phone Number",
                      ),
                      autofocus: true,
                      onChanged: (value) {
                        if (value.length == 10) {
                          stateSetter(() {
                            _validPhoneNumber = true;
                          });
                        } else {
                          stateSetter(
                            () {
                              _validPhoneNumber = false;
                            },
                          );
                        }
                      },
                    ),
                  ),
                  Row(
                    children: [
                      Expanded(
                        child: SizedBox(
                          height: size.height / 18,
                          child: Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 20),
                            child: AbsorbPointer(
                              absorbing: _validPhoneNumber ? false : true,
                              child: ElevatedButton(
                                style: ButtonStyle(
                                    backgroundColor: _validPhoneNumber
                                        ? MaterialStateProperty.all(
                                            Colors.deepPurple)
                                        : MaterialStateProperty.all(
                                            Colors.grey)),
                                onPressed: () {
                                  stateSetter(() {
                                    auth.isLoading = true;
                                  });
                                  String number =
                                      '+91${_phoneNumberController.text}';
                                  auth.verifyPhoneNumber(
                                    context: context,
                                    number: number,
                                    // latitude: null,
                                    // longitude: null,
                                    // address: null
                                  );
                                  // auth.isLoading = false;
                                },
                                child: auth.isLoading
                                    ? const Center(
                                        child: CircularProgressIndicator(
                                          valueColor:
                                              AlwaysStoppedAnimation<Color>(
                                                  Colors.white),
                                        ),
                                      )
                                    : Text(
                                        _validPhoneNumber
                                            ? "Continue"
                                            : "Enter Phone Number",
                                        style: const TextStyle(
                                          fontSize: 20,
                                        ),
                                      ),
                              ),
                            ),
                          ),
                        ),
                      )
                    ],
                  )
                ],
              );
            },
          );
        },
      );
    }

    return Container(
      color: Colors.deepPurpleAccent,
      child: SafeArea(
        child: Scaffold(
          body: SizedBox(
            child: Stack(
              children: [
                Align(
                  alignment: Alignment.topRight,
                  child: Container(
                    height: size.height / 10,
                    width: size.width / 3.5,
                    alignment: Alignment.center,
                    decoration: const BoxDecoration(
                      color: Color.fromRGBO(178, 182, 231, 1.0),
                      borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(1000),
                      ),
                    ),
                    child: InkWell(
                      onTap: () {},
                      child: Icon(
                        Icons.arrow_forward,
                        size: size.width / 12,
                      ),
                    ),
                  ),
                ),
                Column(
                  children: [
                    const Expanded(child: OnBoardScreen()),
                    const Text(
                      "Ready to Order from your Nearest Shop?",
                      style:
                          TextStyle(fontSize: 18, fontWeight: FontWeight.w300),
                    ),
                    SizedBox(
                      height: size.height / 45,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Text("New User?",style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold),),
                        SizedBox(height: 50,child: Image.asset('assets/forward_arrow.gif')),
                        TextButton(
                          style: ButtonStyle(
                              elevation: MaterialStateProperty.all(1),
                              backgroundColor: MaterialStateProperty.all(
                                const Color.fromRGBO(178, 182, 231, 1.0),
                              )),
                          onPressed: () async {
                            setState(() {
                              locationData.isLoading = true;
                            });
                            await locationData.getMyCurrentPosition();
                            if (locationData.permissionAllowed) {
                              Navigator.pushReplacementNamed(context, MapScreen.id);
                              setState(() {
                                locationData.isLoading = false;
                              });
                            } else {
                              setState(() {
                                locationData.isLoading = false;
                              });
                            }
                          },
                          child: locationData.isLoading
                              ? const Center(
                            child: CircularProgressIndicator(
                              valueColor:
                              AlwaysStoppedAnimation<Color>(Colors.white),
                            ),
                          )
                              : const Text(
                            "Set Location",
                            style: TextStyle(
                              color: Colors.black,
                              fontSize: 16,
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: size.height / 45,
                    ),
                    TextButton(
                      onPressed: () {
                        setState(() {
                          auth.screen = "Login";
                        });
                        showBottomSheet(context);
                      },
                      child: RichText(
                        text: const TextSpan(
                          text: "Already a customer?",
                          children: [
                            TextSpan(
                                text: " Login",
                                style: TextStyle(
                                    fontSize: 17,
                                    color: Colors.deepPurpleAccent,
                                    fontWeight: FontWeight.w500))
                          ],
                          style: TextStyle(fontSize: 15, color: Colors.black87),
                        ),
                      ),
                    ),
                    SizedBox(
                      height: size.height / 30,
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
9lowa7mx

9lowa7mx1#

您必须为特定PersistentBottomNavBarItem显式提供路由设置,才能从该页面导航。
例如:在您的PersistentTabView中,您有一个导航到HomeScreen()的“主页”选项卡。现在您要从HomeScreen()导航到ProfileScreen(),则必须在PersistentBottomNavBarItem中提供一个路由设置。

PersistentBottomNavBarItem(
    icon: Icon(Icons.home),
    title: "Home",
    activeColorPrimary: primaryColor,
    inactiveColorPrimary: Colors.grey,
    inactiveColorSecondary: Colors.purple,
    routeAndNavigatorSettings: RouteAndNavigatorSettings(
      routes: {
       "/profile": (context)=> ProfileScreen(),
      },
    )
  ),

现在,您可以从HomeScreen()调用**Navigator.pushNamed(context,“/profile”)**来导航到ProfileScreen()。

mitkmikd

mitkmikd2#

是的@Sizajul Islam说的对
您可以浏览任何您正在使用的导航栏。它必须具有类似routesonGenerateRoutes的属性。为路线创建一个单独的类,然后在导航中调用它们。
对于CupertinoTabBar****返回CupertinoTabView(路径:appRoutes,//或onGenerateRoutes(生成路由):***
与上述类似,在导航中设置路线:

PersistentBottomNavBarItem(
..............,
    routeAndNavigatorSettings: RouteAndNavigatorSettings(
      routes: {},
//Or
      onGenerateRoutes: ....

相关问题