簡體   English   中英

我的 flutter 屏幕在熱重載時一直返回到我之前的屏幕

[英]my flutter screen keeps going back to my previous screen on hot reload

我有一個歡迎頁面,我可以在其中單擊一個按鈕和 go 到我的登錄屏幕,我使用Get.toNamed來導航我的屏幕

//Click button on welcome.dart
InkWell(
  onTap: () => goto("login"),
  child: ....,
)

//goto function for navigating
goto(String path, {dynamic args}) {
  Get.toNamed('/$path', arguments: args);
}

我的獲取頁面

const welcome = '/welcome';
const login = '/login';

List<GetPage<dynamic>> getPages = [
  pages(welcome, const Welcome()),
  pages(login, const Login()),
];

GetPage pages(String route, dynamic screen,
    {dynamic args, bool dialog = false, bool opaque = false}) {
  return GetPage(
      name: route,
      page: () => screen,
      transition: Transition.leftToRightWithFade,
      fullscreenDialog: dialog,
      opaque: opaque);
}

現在在我的登錄頁面中使用flutter_hooks而不是 statelesswidget

class Login extends HookWidget {
  const Login({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final emailController = useTextEditingController();
    final passwordController = useTextEditingController();

    return SafeArea(
      child: Scaffold(
          body: ListView(
        padding: const EdgeInsets.all(15),
        children: [
          const SizedBox(height: 50),
          const CustomText(
            "Welcome Back",
            size: 30,
            font: "FlexR",
          ),
          const SizedBox(height: 20),
          InputField(
            controller: emailController,
            label: "Email Address",
            suffixIcon: IconlyLight.message,
          ),

          //...,

主要.dart

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<ThemeData> themes = [
    ThemeManager.lightTheme,
    ThemeManager.lightBlueTheme,
    ThemeManager.lightPurpleTheme,
    ThemeManager.lightGreenTheme,
    ThemeManager.lightOrangeTheme,
    ThemeManager.lightYellowTheme,
    ThemeManager.darkTheme,
    ThemeManager.darkBlueTheme,
    ThemeManager.darkPurpleTheme,
    ThemeManager.darkGreenTheme,
    ThemeManager.darkOrangeTheme,
    ThemeManager.darkYellowTheme,
  ];

  @override
  void initState() {
    init();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter',
        theme: themes[0],
        initialRoute: "/",
        unknownRoute: GetPage(name: "/404", page: () => Container()),
        getPages: getPages,
        home: const Welcome());
  }
}

我的歡迎頁面是一個無狀態小部件,因為我沒有在其中使用任何掛鈎,但現在每當我單擊 go 登錄頁面並進行更改並決定熱重新加載它以查看更改時,它會立即將我帶回我的歡迎頁面頁面,我不知道是什么原因造成的,因為一切都是正確的,或者我錯過了什么。 請問我該如何解決這個問題,如果您還有更多代碼或解釋,請告訴我。

好的,我發現它出了什么問題而不是使用

InkWell(
  onTap: () => goto("login"), //from here
  child: ....,
)

//goto function for navigating
goto(String path, {dynamic args}) {
  Get.toNamed('/$path', arguments: args); //from here
}

我像正常方式一樣使用它,即

InkWell(
  onTap: () => goto("/login"), //change here
  child: ....,
)

//goto function for navigating
goto(String path, {dynamic args}) {
  Get.toNamed(path, arguments: args); //change here
}

我仍然不知道它們之間有什么區別,但是就是這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM