|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// Flutter code sample for [NavigationDrawer] . |
| 6 | + |
| 7 | +// Builds an adaptive navigation widget layout. When the screen width is less than |
| 8 | +// 450, A [NavigationBar] will be displayed. Otherwise, a [NavigationRail] will be |
| 9 | +// displayed on the left side, and also a button to open the [NavigationDrawer]. |
| 10 | +// All of these navigation widgets are built from an indentical list of data. |
| 11 | + |
| 12 | +import 'package:flutter/material.dart'; |
| 13 | + |
| 14 | +class ExampleDestination { |
| 15 | + const ExampleDestination(this.label, this.icon, this.selectedIcon); |
| 16 | + |
| 17 | + final String label; |
| 18 | + final Widget icon; |
| 19 | + final Widget selectedIcon; |
| 20 | +} |
| 21 | + |
| 22 | +const List<ExampleDestination> destinations = <ExampleDestination>[ |
| 23 | + ExampleDestination('page 0', Icon(Icons.widgets_outlined), Icon(Icons.widgets)), |
| 24 | + ExampleDestination('page 1', Icon(Icons.format_paint_outlined), Icon(Icons.format_paint)), |
| 25 | + ExampleDestination('page 2', Icon(Icons.text_snippet_outlined), Icon(Icons.text_snippet)), |
| 26 | + ExampleDestination('page 3', Icon(Icons.invert_colors_on_outlined), Icon(Icons.opacity)), |
| 27 | +]; |
| 28 | + |
| 29 | +void main() { |
| 30 | + runApp( |
| 31 | + MaterialApp( |
| 32 | + title: 'NavigationDrawer Example', |
| 33 | + debugShowCheckedModeBanner: false, |
| 34 | + theme: ThemeData(useMaterial3: true), |
| 35 | + home: const NavigationDrawerExample(), |
| 36 | + ), |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +class NavigationDrawerExample extends StatefulWidget { |
| 41 | + const NavigationDrawerExample({super.key}); |
| 42 | + |
| 43 | + @override |
| 44 | + State<NavigationDrawerExample> createState() => _NavigationDrawerExampleState(); |
| 45 | +} |
| 46 | + |
| 47 | +class _NavigationDrawerExampleState extends State<NavigationDrawerExample> { |
| 48 | + final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>(); |
| 49 | + |
| 50 | + int screenIndex = 0; |
| 51 | + late bool showNavigationDrawer; |
| 52 | + |
| 53 | + void handleScreenChanged(int selectedScreen) { |
| 54 | + setState(() { |
| 55 | + screenIndex = selectedScreen; |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + void openDrawer() { |
| 60 | + scaffoldKey.currentState!.openEndDrawer(); |
| 61 | + } |
| 62 | + |
| 63 | + Widget buildBottomBarScaffold(){ |
| 64 | + return Scaffold( |
| 65 | + body: Center( |
| 66 | + child: Column( |
| 67 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, |
| 68 | + children: <Widget>[ |
| 69 | + Text('Page Index = $screenIndex'), |
| 70 | + ], |
| 71 | + ), |
| 72 | + ), |
| 73 | + bottomNavigationBar: NavigationBar( |
| 74 | + selectedIndex: screenIndex, |
| 75 | + onDestinationSelected: (int index) { |
| 76 | + setState(() { |
| 77 | + screenIndex = index; |
| 78 | + }); |
| 79 | + }, |
| 80 | + destinations: destinations |
| 81 | + .map((ExampleDestination destination) { |
| 82 | + return NavigationDestination( |
| 83 | + label: destination.label, |
| 84 | + icon: destination.icon, |
| 85 | + selectedIcon: destination.selectedIcon, |
| 86 | + tooltip: destination.label, |
| 87 | + ); |
| 88 | + }) |
| 89 | + .toList(), |
| 90 | + ), |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + Widget buildDrawerScaffold(BuildContext context){ |
| 95 | + return Scaffold( |
| 96 | + key: scaffoldKey, |
| 97 | + body: SafeArea( |
| 98 | + bottom: false, |
| 99 | + top: false, |
| 100 | + child: Row( |
| 101 | + children: <Widget>[ |
| 102 | + Padding( |
| 103 | + padding: const EdgeInsets.symmetric(horizontal: 5), |
| 104 | + child: NavigationRail( |
| 105 | + minWidth: 50, |
| 106 | + destinations: destinations |
| 107 | + .map((ExampleDestination destination) { |
| 108 | + return NavigationRailDestination( |
| 109 | + label: Text(destination.label), |
| 110 | + icon: destination.icon, |
| 111 | + selectedIcon: destination.selectedIcon, |
| 112 | + ); |
| 113 | + }) |
| 114 | + .toList(), |
| 115 | + selectedIndex: screenIndex, |
| 116 | + useIndicator: true, |
| 117 | + onDestinationSelected: (int index) { |
| 118 | + setState(() { |
| 119 | + screenIndex = index; |
| 120 | + }); |
| 121 | + }, |
| 122 | + ), |
| 123 | + ), |
| 124 | + const VerticalDivider(thickness: 1, width: 1), |
| 125 | + Expanded( |
| 126 | + child: Column( |
| 127 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, |
| 128 | + children: <Widget>[ |
| 129 | + Text('Page Index = $screenIndex'), |
| 130 | + ElevatedButton( |
| 131 | + onPressed: openDrawer, |
| 132 | + child: const Text('Open Drawer'), |
| 133 | + ), |
| 134 | + ], |
| 135 | + ), |
| 136 | + ), |
| 137 | + ], |
| 138 | + ), |
| 139 | + ), |
| 140 | + endDrawer: NavigationDrawer( |
| 141 | + onDestinationSelected: handleScreenChanged, |
| 142 | + selectedIndex: screenIndex, |
| 143 | + children: <Widget>[ |
| 144 | + Padding( |
| 145 | + padding: const EdgeInsets.fromLTRB(28, 16, 16, 10), |
| 146 | + child: Text( |
| 147 | + 'Header', |
| 148 | + style: Theme.of(context).textTheme.titleSmall, |
| 149 | + ), |
| 150 | + ), |
| 151 | + ...destinations |
| 152 | + .map((ExampleDestination destination) { |
| 153 | + return NavigationDrawerDestination( |
| 154 | + label: Text(destination.label), |
| 155 | + icon: destination.icon, |
| 156 | + selectedIcon: destination.selectedIcon, |
| 157 | + ); |
| 158 | + }), |
| 159 | + const Padding( |
| 160 | + padding: EdgeInsets.fromLTRB(28, 16, 28, 10), |
| 161 | + child: Divider(), |
| 162 | + ), |
| 163 | + ], |
| 164 | + ), |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + @override |
| 169 | + void didChangeDependencies() { |
| 170 | + super.didChangeDependencies(); |
| 171 | + showNavigationDrawer = MediaQuery.of(context).size.width >= 450; |
| 172 | + } |
| 173 | + |
| 174 | + @override |
| 175 | + Widget build(BuildContext context) { |
| 176 | + return showNavigationDrawer ? buildDrawerScaffold(context) : buildBottomBarScaffold(); |
| 177 | + } |
| 178 | +} |
0 commit comments