Advertisement
Guest User

Flutter Nav Drawer Not Working

a guest
Dec 29th, 2022
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.75 KB | Source Code | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. import 'package:routemaster/routemaster.dart';
  4.  
  5.  
  6. class App extends StatelessWidget {
  7.   const App({super.key});
  8.  
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return MaterialApp.router(
  12.       routerDelegate: RoutemasterDelegate(routesBuilder: (context) => routes),
  13.       routeInformationParser: const RoutemasterParser(),
  14.     );
  15.   }
  16. }
  17.  
  18.  
  19. final routes = RouteMap(
  20.   routes: {
  21.     '/':(route) => const MaterialPage(
  22.       key: ValueKey('first'),
  23.       child: firstPage,
  24.     ),
  25.     '/second':(route) => const MaterialPage(
  26.       key: ValueKey('second'),
  27.       child: secondPage,
  28.     ),
  29.   },
  30. );
  31.  
  32.  
  33. const firstPage = AppPage(
  34.   title: Text('First'),
  35.   body: Text('First'),
  36.   color: Colors.purple
  37. );
  38.  
  39.  
  40. const secondPage = AppPage(
  41.   title: Text('Second'),
  42.   body: Text('Second'),
  43.   color: Colors.green
  44. );
  45.  
  46.  
  47. class AppPage extends StatelessWidget {
  48.   final Text title;
  49.   final Text body;
  50.   final Color color;
  51.  
  52.   const AppPage({
  53.     super.key,
  54.     required this.title,
  55.     required this.body,
  56.     required this.color
  57.   });
  58.  
  59.   @override
  60.   Widget build (BuildContext context) {
  61.     return Scaffold(
  62.       drawer: Drawer(
  63.         child: ListView(
  64.           children: [
  65.             ListTile(
  66.               title: const Text('First'),
  67.               onTap: () => Routemaster.of(context).push('/'),
  68.             ),
  69.             ListTile(
  70.               title: const Text('Second'),
  71.               onTap: () => Routemaster.of(context).push('/second'),
  72.             ),
  73.           ],
  74.         ),
  75.       ),
  76.       appBar: AppBar(
  77.         title: title,
  78.       ),
  79.       body: Container(
  80.         decoration: BoxDecoration(color: color),
  81.         child: Center(child: body),
  82.       ),
  83.     );
  84.   }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement