1

I want to put a drawer like this in my Flutter app: just like https://m3.material.io/develop/flutter

I'm using NavigationRail and it's said that a menu button can be added to open a navigation drawer. Does any knows how to add the menu button and the drawer? menu button of NavigationRail

thanks.

Kelvin
  • 11
  • 1

1 Answers1

0

It's a bit hard to use a regular Scaffold Drawer without the regular scaffold controls, as far as I can tell.

I came up with a solution for your problem, if I understood it correctly. Looks a lot like the spec site, needs a bit of styling.

Took the example from the NavigationRail documentation and added a Visibility widget. Now clicking on the destinations, you can show and hide their child widgets(drawer). No drawer animation though.

enter image description here

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
          colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true),
      home: const NavRailExample(),
    );
  }
}

class NavRailExample extends StatefulWidget {
  const NavRailExample({super.key});

  @override
  State<NavRailExample> createState() => _NavRailExampleState();
}

class _NavRailExampleState extends State<NavRailExample> {
  int _selectedIndex = 0;
  NavigationRailLabelType labelType = NavigationRailLabelType.all;
  bool showLeading = false;
  bool showTrailing = false;
  double groupAligment = -1.0;
  bool _isClosed = false;

  Widget _getWidget(int index) {
    switch (index) {
      case 1:
        return GestureDetector(
          child: const Text('Tap!'),
          onTap: () => setState(() {
            _isClosed = true;
          }),
        );
      case 2:
        return const Text('empty');
      default:
        return ListView(
          children: const [
            ExpansionTile(
              title: Text('whatev'),
              children: [Text('1'), Text('2')],
            ),
            ListTile(
              title: Text('adfafdafaf'),
            )
          ],
        );
    }
  }

  Widget _getPage(int index) {
    switch (index) {
      case 1:
        return const Center(child: Text('sheeesh'));
      case 2:
        return const Center(child: Text('empty'));
      default:
        return const Center(child: Text('yolo'),);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Row(
          children: <Widget>[
            NavigationRail(
              selectedIndex: _selectedIndex,
              groupAlignment: groupAligment,
              onDestinationSelected: (int index) {
                setState(() {
                  _isClosed = (_selectedIndex == index || _isClosed)
                      ? !_isClosed
                      : _isClosed;
                  _selectedIndex = index;
                });
              },
              labelType: labelType,
              leading: showLeading
                  ? FloatingActionButton(
                      elevation: 0,
                      onPressed: () {
                        // Add your onPressed code here!
                      },
                      child: const Icon(Icons.add),
                    )
                  : const SizedBox(),
              trailing: showTrailing
                  ? IconButton(
                      onPressed: () {
                        // Add your onPressed code here!
                      },
                      icon: const Icon(Icons.more_horiz_rounded),
                    )
                  : const SizedBox(),
              destinations: const <NavigationRailDestination>[
                NavigationRailDestination(
                  icon: Icon(Icons.favorite_border),
                  selectedIcon: Icon(Icons.favorite),
                  label: Text('First'),
                ),
                NavigationRailDestination(
                  icon: Icon(Icons.bookmark_border),
                  selectedIcon: Icon(Icons.book),
                  label: Text('Second'),
                ),
                NavigationRailDestination(
                  icon: Icon(Icons.star_border),
                  selectedIcon: Icon(Icons.star),
                  label: Text('Third'),
                ),
              ],
            ),
            Visibility(
              maintainState: false,
              visible: !_isClosed,
              child: Row(
                children: [
                  const VerticalDivider(thickness: 1, width: 1),
                  SizedBox(
                    height: double.infinity,
                    width: 200,
                    child: _getWidget(_selectedIndex),
                  )
                ],
              ),
            ),
            const VerticalDivider(thickness: 1, width: 1),
            // This is the main content.
            Expanded(
              child: _getPage(_selectedIndex),
            ),
          ],
        ),
      ),
    );
  }
}

halfer
  • 19,824
  • 17
  • 99
  • 186
Csaba Mihaly
  • 149
  • 10
  • Thanks. I found out that it uses material3 Navigation Rail and Navigation Drawer, then keep Navigation Drawer open. However, material3 Navigation Drawer is not available for flutter. – Kelvin Jan 22 '23 at 04:56
  • Your solution is really a good alternative. – Kelvin Jan 22 '23 at 04:57