Flutter : Android Studio #6 Using a drawer menu to navigate page

ProfilePage.dart

import 'package:flutter/material.dart';
import 'package:flutter03/widgets/menu.dart';

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

  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        drawer: Menu(),
        appBar: AppBar(
          title: Text('ProfilePage'),
        )
    );
  }
}

SettingPage.dart

import 'package:flutter/material.dart';
import 'package:flutter03/widgets/menu.dart';

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

  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        drawer: Menu(),
        appBar: AppBar(
          title: Text('SettingPage'),
        )
    );
  }
}

main.dart

 routes: {
        '/' : (context) => HomePage(),
        '/profile' : (context) => ProfilePage(),
        '/setting' : (context) => SettingPage(),
      },

menu.dart

      onTap: () {
              Navigator.of(context, rootNavigator: true).pushNamedAndRemoveUntil('/', (route) => false);
            },
       ListTile(
            leading: Icon(Icons.home),
            title: Text('Home'),
            onTap: () {
              Navigator.of(context, rootNavigator: true).pushNamedAndRemoveUntil('/', (route) => false);
            },
          ),
          ListTile(
            leading: Icon(Icons.account_circle),
            title: Text('Profile'),
            onTap: () {
              Navigator.of(context, rootNavigator: true).pushNamedAndRemoveUntil('/profile', (route) => false);
            },
          ),
          ListTile(
            leading: Icon(Icons.settings),
            title: Text('Settings'),
            onTap: () {
              Navigator.of(context, rootNavigator: true).pushNamedAndRemoveUntil('/setting', (route) => false);
            },
...
selected: ModalRoute.of(context)!.settings.name == '/' ? true : false ,

...
 selected: ModalRoute.of(context)!.settings.name == '/profile' ? true : false ,

...
selected: ModalRoute.of(context)!.settings.name == '/setting' ? true : false ,
(Visited 11 times, 1 visits today)
Spread the love
Published
Categorized as Flutter