Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter will get deleted, if a new navigator got created with the initial path #68

Open
SchabanBo opened this issue Jul 2, 2022 · 0 comments

Comments

@SchabanBo
Copy link
Owner

Code to reproduce

import 'dart:math';

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

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

class MyApp extends StatelessWidget {
  static List<String> tabs = [
    "Home Page",
    "Store Page",
    "Settings Page",
  ];
  final routes = [
    QRoute(path: '/login', builder: () => LoginScreen()),
    QRoute.withChild(
        path: '/home/:id',
        builderChild: (c) => HomePage(c),
        children: [
          QRoute(
              name: tabs[0],
              path: '/',
              builder: () => Tab('Home', Colors.blueGrey.shade900)),
          QRoute(
              name: tabs[1],
              path: '/store',
              builder: () => Tab('Store', Colors.blueGrey.shade700)),
          QRoute(
              name: tabs[2],
              path: '/settings',
              builder: () => Tab('Settings', Colors.blueGrey.shade500)),
        ]),
  ];

  @override
  Widget build(BuildContext context) => MaterialApp.router(
        routeInformationParser: const QRouteInformationParser(),
        routerDelegate: QRouterDelegate(routes, initPath: '/login'),
        theme: ThemeData.dark(),
      );
}

class LoginScreen extends StatefulWidget {
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text('Login'),
          centerTitle: true,
        ),
        body: Column(
          children: [
            TextButton(
                onPressed: () =>
                    QR.navigator.replaceAll('/home/${Random().nextInt(100)}'),
                child: const Text('Login')),
          ],
        ),
      );
}

class HomePage extends StatefulWidget {
  final QRouter router;
  const HomePage(this.router);
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    // We need to add listener here so the bottomNavigationBar
    // get updated (the selected tab) when we navigate to new page
    widget.router.navigator.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text('My App'),
          centerTitle: true,
        ),
        body: widget.router,
        bottomNavigationBar: BottomNavigationBar(
          items: const [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: 'home'),
            BottomNavigationBarItem(icon: Icon(Icons.store), label: 'store'),
            BottomNavigationBarItem(
                icon: Icon(Icons.settings), label: 'Settings')
          ],
          currentIndex: MyApp.tabs
              .indexWhere((element) => element == widget.router.routeName),
          onTap: (v) => QR.toName(MyApp.tabs[v]),
        ),
      );
}

class Tab extends StatelessWidget {
  final String name;
  final Color color;
  const Tab(this.name, this.color);
  @override
  Widget build(BuildContext context) => Container(
        color: color,
        child: Center(
            child: Text('$name  ${QR.params['id']!}',
                style: const TextStyle(fontSize: 20))),
      );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant