jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.39k stars 1.63k forks source link

GetX 5: initialRoute parameter of GetMaterialApp() is ignored, if a route with name '/' is registered #3196

Open thomasgi1 opened 2 months ago

thomasgi1 commented 2 months ago

Describe the bug When you register a page with name '/', then the initialRoute parameter of the GetMaterialApp() constructor is ignored. Furthermore no navigation to another page than the '/' page is possible.

**Reproduction code

example:

Change the example app in getx repository as follows:

import 'package:flutter/material.dart';
import 'package:get/get.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      initialRoute: '/second',
      getPages: [
        GetPage(
            participatesInRootNavigator: true,
            name: '/',
            page: () => const First()),
        GetPage(
          name: '/second',
          page: () => const Second(),
          transition: Transition.downToUp,
        ),
        GetPage(
          name: '/third',
          page: () => const Third(),
        ),
        GetPage(
          name: '/fourth',
          page: () => const Fourth(),
        ),
      ],
      debugShowCheckedModeBanner: false,
    );
  }
}

class FirstController extends GetxController {
  @override
  void onClose() {
    print('on close first');
    super.onClose();
  }
}

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

  @override
  Widget build(BuildContext context) {
    print('First rebuild');
    Get.put(FirstController());
    return Scaffold(
      appBar: AppBar(
        title: const Text('page one'),
        leading: IconButton(
          icon: const Icon(Icons.more),
          onPressed: () {
            Get.snackbar(
              'title',
              "message",
              mainButton:
                  TextButton(onPressed: () {}, child: const Text('button')),
              isDismissible: true,
              duration: Duration(seconds: 5),
              snackbarStatus: (status) => print(status),
            );
            // print('THEME CHANGED');
            // Get.changeTheme(
            //     Get.isDarkMode ? ThemeData.light() : ThemeData.dark());
          },
        ),
      ),
      body: Center(
        child: SizedBox(
          height: 300,
          width: 300,
          child: ElevatedButton(
            onPressed: () {
              Get.toNamed('/second?id=123');
            },
            child: const Text('next screen'),
          ),
        ),
      ),
    );
  }
}

class SecondController extends GetxController {
  final textEdit = TextEditingController();
  @override
  void onClose() {
    print('on close second');
    textEdit.dispose();
    super.onClose();
  }
}

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

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(SecondController());
    print('second rebuild');
    return Scaffold(
      appBar: AppBar(
        title: Text('page two ${Get.parameters["id"]}'),
      ),
      body: Center(
        child: Column(
          children: [
            Expanded(
                child: TextField(
              controller: controller.textEdit,
            )),
            SizedBox(
              height: 300,
              width: 300,
              child: ElevatedButton(
                onPressed: () {
                  Get.toNamed('/third');
                },
                child: const Text('next screen'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.red,
      appBar: AppBar(
        title: const Text('page three'),
      ),
      body: Center(
        child: SizedBox(
          height: 300,
          width: 300,
          child: ElevatedButton(
            onPressed: () {
              Get.offNamedUntil('/fourth', (route) {
                return Get.currentRoute == '/first';
              });
            },
            child: const Text('go to first screen'),
          ),
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.red,
      appBar: AppBar(
        title: const Text('page four'),
      ),
      body: Center(
        child: SizedBox(
          height: 300,
          width: 300,
          child: ElevatedButton(
            onPressed: () {
              Get.back();
            },
            child: const Text('go to first screen'),
          ),
        ),
      ),
    );
  }
}

To Reproduce Steps to reproduce the behavior:

  1. Run the app in debug mode
  2. Page two should be possible, but page one is shown
  3. Click on 'next screen' button
  4. No page change happens

Expected behavior The initial route should be respected even if one of the registered pages has the route '/'.

Flutter Version: 3.24.1

Getx Version: git commit 5db4322c417cf7de10077c79dfa5cc6c5259a465

Describe on which device you found the bug: MacOS