1

so I have the following routes defined in my main.dart file:

routes: { 
        '/sign-in': (context) => BlocProvider( 
              lazy: false, 
              create: (_) => AuthCubit(), 
              child: const LandingPage(), 
            ), 
        '/home': (context) => const HomeLandingPage(), 
        '/sign-up': (context) => const SignUpLandingPage(), 
        '/language-selection': (context) => const SelectionLanguageScreen(), 
        '/camera-page': (context) => CameraPage(), 
        '/web-add-page': (context) => const WebAddPage() 
      }, 

The problem is that some of them are just child widgets of a parent screen. e.g: '/web-add-page' is inside '/home' The following are the child views of /home :

final screens  = [  //screens is a List<Widget> 
      const WebAddPage(), 
      const WebUpdateProducts(), 
      const WebUpdateCategories(), 
      const WebUpdateStores(), 
      const WebUpdateUsers() 
    ]; 

Big picture problem is that I CAN'T use Navigator.Push() because I'm not navigating to a new screen, I'm just replacing the content of the '/home' with a child widget (treated as a screen) which allows me to keep the navbar and left column menu, but not updating the ourl route in the browser. When I enter / Home, url look like: locahost:4200/home, after I open the add View it should update to .../home/web-add-page or whatever but I can't find the way to do it.

Here's the entire file:


class WebHomeView extends StatefulWidget {
  const WebHomeView({Key? key}) : super(key: key);

  @override
  State<WebHomeView> createState() => _WebHomeViewState();
}

class _WebHomeViewState extends State<WebHomeView> {
  final screens = [
    const MainMenuWebPage(),
    const WebTickets(),
    const WebMovements(),
    const WebOrders()
  ];

  int _screenIndex = 0;
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => HomeBloc(),
      child: BlocConsumer<HomeBloc, HomeState>(
        builder: (context, state) {
          return Scaffold(
              appBar: AppBar(
                backgroundColor: Theme.of(context).primaryColor,
                title: const Text('Mercaditos Project',
                    style: TextStyle(fontSize: 20)),
                actions: [
                  JustTheTooltip(
                    tailLength: 0,
                    preferredDirection: AxisDirection.down,
                    content: const Padding(
                      padding: EdgeInsets.all(5.0),
                      child: Text('Log Out'),
                    ),
                    child: IconButton(
                        onPressed: () {
                          FirebaseAuth.instance.signOut();
                        },
                        icon: const Icon(
                          Icons.output,
                          size: 30,
                        )),
                  ),
                  JustTheTooltip(
                    tailLength: 0,
                    preferredDirection: AxisDirection.down,
                    content: const Padding(
                      padding: EdgeInsets.all(5.0),
                      child: Text('Main Menu'),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.only(top: 10, bottom: 10),
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                            elevation: 0,
                            shape: const CircleBorder(),
                            primary: state.option == AppBarOptions.mainMenu
                                ? Colors.grey.shade800
                                : Theme.of(context).primaryColor),
                        child: Icon(Icons.house,
                            color: Theme.of(context).backgroundColor),
                        onPressed: () {
                          setState(() {
                            _screenIndex = 0;
                          });
                        },
                      ),
                    ),
                  ),
                  JustTheTooltip(
                    tailLength: 0,
                    preferredDirection: AxisDirection.down,
                    content: const Padding(
                      padding: EdgeInsets.all(5.0),
                      child: Text('Tickets'),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.only(top: 10, bottom: 10),
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                            elevation: 0,
                            shape: const CircleBorder(),
                            primary: state.option == AppBarOptions.tickets
                                ? Colors.grey.shade800
                                : Theme.of(context).primaryColor),
                        child: Icon(Icons.receipt,
                            color: Theme.of(context).backgroundColor),
                        onPressed: () {
                          setState(() {
                            _screenIndex = 1;
                          });
                        },
                      ),
                    ),
                  ),
                  JustTheTooltip(
                    tailLength: 0,
                    preferredDirection: AxisDirection.down,
                    content: const Padding(
                      padding: EdgeInsets.all(5.0),
                      child: Text('Movements'),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.only(top: 10, bottom: 10),
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                            elevation: 0,
                            shape: const CircleBorder(),
                            primary: state.option == AppBarOptions.movements
                                ? Colors.grey.shade800
                                : Theme.of(context).primaryColor),
                        child: Icon(Icons.print_outlined,
                            color: Theme.of(context).backgroundColor),
                        onPressed: () {
                          setState(() {
                            _screenIndex = 2;
                          });
                        },
                      ),
                    ),
                  ),
                  JustTheTooltip(
                    tailLength: 0,
                    preferredDirection: AxisDirection.down,
                    content: const Padding(
                      padding: EdgeInsets.all(5.0),
                      child: Text('Orders'),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.only(top: 10, bottom: 10),
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                            elevation: 0,
                            shape: const CircleBorder(),
                            primary: state.option == AppBarOptions.orders
                                ? Colors.grey.shade800
                                : Theme.of(context).primaryColor),
                        child: Icon(Icons.assignment,
                            color: Theme.of(context).backgroundColor),
                        onPressed: () {
                          setState(() {
                            _screenIndex = 3;
                          });
                        },
                      ),
                    ),
                  ),
                  context
                              .watch<AuthCubit>()
                              .state
                              .currentUSer
                              ?.photoUrl
                              ?.isEmpty ==
                          null
                      ? const Padding(
                          padding: EdgeInsets.all(8.0),
                          child: CircleAvatar(
                              foregroundImage:
                                  AssetImage("assets/userIcon.png")),
                        )
                      : Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: CircleAvatar(
                              foregroundImage: NetworkImage(
                                  '${context.watch<AuthCubit>().state.currentUSer?.photoUrl}')),
                        ),
                ],
              ),
              body: AnimatedSwitcher(
                  transitionBuilder:
                      (Widget child, Animation<double> animation) {
                    final inAnimation = Tween<Offset>(
                            begin: Offset(1.0, 0.0), end: Offset(0.0, 0.0))
                        .animate(animation);
                    final outAnimation = Tween<Offset>(
                            begin: Offset(-1.0, 0.0), end: Offset(0.0, 0.0))
                        .animate(animation);

                    if (child.key == ValueKey(screens[state.screenIndex])) {
                      return ClipRect(
                        child: SlideTransition(
                          position: inAnimation,
                          child: child,
                        ),
                      );
                    } else {
                      return ClipRect(
                        child: SlideTransition(
                          position: outAnimation,
                          child: child,
                        ),
                      );
                    }
                  },
                  duration: Duration(milliseconds: 100),
                  child: screens[_screenIndex]));  // Need to update url here somehow according to routes defined in my main.dart`
 
        },
        listener: (context, state) {},
      ),
    );
  }
}

this is the same stucture that

this is similar to the structure that im working, i pressed the buttons to change the widget (that i use as a screen) that is inside the blue square without making a new navigation to another screen, just changing the widget but i need to try to implement the routes because i need to update the url pad when im changing the widgets

F7085
  • 49
  • 3

1 Answers1

0

When pushing to a new page with a named route defined, use Navigator.pushNamed(context, 'name_of_route_here');.

In your case, what I'll have is this:

 initialRoute: '/home',
      routes: {
        '/home': (context) => const MyHomePage(title: 'Flutter Demo Home Page'),
        '/home/second': (context) => const MyHomePage(title: "s",),
      },

If you noticed, I have the second named route as /home/... that allows the URL to be displayed with the prefix /home.

To manage multiple routes, you can have list of strings:

final screens  = [  //screens is a List<Strings> 
      const '/home/web-add-page', 
      const '/home/web-update-products', 
      const '/home/web-update-cat', 
      const '/home/web-update-stores', 
      const '/home/web-update-users' 
    ];

Then you can till use Navigator.pushNamed(context, screens[i]);

Oreofe Solarin
  • 286
  • 5
  • 13
  • the problem is that i'm not usign Navigator.pushNamed to make the navegations, i use an array of widgets that i treat as screens to change them, but i need to implement the routes but at the same time try to use the array of screens – F7085 Nov 28 '22 at 20:22
  • @F7085 Check my updated answer/ – Oreofe Solarin Nov 29 '22 at 02:21
  • @OreofeSolarin I just came across the same issue, the post mentions that `Navigator.PushNamed()` can't be used since there is no navigation to a new screen, it's the same screen but there is a widget inside it that represents a screen, but it's not a new scaffold. If this is the case your answer does not seems to suffice the issue – Retrosec6 Nov 30 '22 at 20:50
  • @OreofeSolarin i added an explanation of the structure that im working at the end of the question's body, if you can check it to understand a little more, thanks – F7085 Nov 30 '22 at 21:13