qlevar_router icon indicating copy to clipboard operation
qlevar_router copied to clipboard

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

Open SchabanBo opened this issue 2 years ago • 0 comments

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))),
      );
}

SchabanBo avatar Jul 02 '22 15:07 SchabanBo