@@ -28,7 +28,7 @@ class AdaptiveScaffoldDestination {
28
28
/// A widget that adapts to the current display size, displaying a [Drawer] ,
29
29
/// [NavigationRail] , or [BottomNavigationBar] . Navigation destinations are
30
30
/// defined in the [destinations] parameter.
31
- class AdaptiveNavigationScaffold extends StatefulWidget {
31
+ class AdaptiveNavigationScaffold extends StatelessWidget {
32
32
final Widget title;
33
33
final Widget body;
34
34
final int currentIndex;
@@ -37,7 +37,8 @@ class AdaptiveNavigationScaffold extends StatefulWidget {
37
37
final FloatingActionButton floatingActionButton;
38
38
final NavigationTypeResolver navigationTypeResolver;
39
39
40
- AdaptiveNavigationScaffold ({
40
+ const AdaptiveNavigationScaffold ({
41
+ Key key,
41
42
this .title,
42
43
this .body,
43
44
@required this .currentIndex,
@@ -46,15 +47,9 @@ class AdaptiveNavigationScaffold extends StatefulWidget {
46
47
this .floatingActionButton,
47
48
this .navigationTypeResolver,
48
49
}) : assert (currentIndex != null ),
49
- assert (destinations != null );
50
+ assert (destinations != null ),
51
+ super (key: key);
50
52
51
- @override
52
- _AdaptiveNavigationScaffoldState createState () =>
53
- _AdaptiveNavigationScaffoldState ();
54
- }
55
-
56
- class _AdaptiveNavigationScaffoldState
57
- extends State <AdaptiveNavigationScaffold > {
58
53
NavigationType _defaultNavigationTypeResolver (BuildContext context) {
59
54
if (_isLargeScreen (context)) {
60
55
return NavigationType .drawer;
@@ -68,52 +63,52 @@ class _AdaptiveNavigationScaffoldState
68
63
@override
69
64
Widget build (BuildContext context) {
70
65
final NavigationTypeResolver navigationTypeResolver =
71
- widget .navigationTypeResolver ?? _defaultNavigationTypeResolver;
66
+ this .navigationTypeResolver ?? _defaultNavigationTypeResolver;
72
67
switch (navigationTypeResolver (context)) {
73
68
case NavigationType .bottomNavigation:
74
69
// Show a Scaffold with a BottomNavigationBar.
75
70
return Scaffold (
76
- body: widget. body,
77
- appBar: AppBar (title: widget. title),
71
+ body: body,
72
+ appBar: AppBar (title: title),
78
73
bottomNavigationBar: BottomNavigationBar (
79
74
items: [
80
- for (final destination in widget. destinations)
75
+ for (final destination in destinations)
81
76
BottomNavigationBarItem (
82
77
icon: Icon (destination.icon),
83
78
title: Text (destination.title),
84
79
),
85
80
],
86
- currentIndex: widget. currentIndex,
87
- onTap: widget. onNavigationIndexChange,
81
+ currentIndex: currentIndex,
82
+ onTap: onNavigationIndexChange,
88
83
),
89
- floatingActionButton: widget. floatingActionButton,
84
+ floatingActionButton: floatingActionButton,
90
85
);
91
86
case NavigationType .navigationRail:
92
87
// Show a Scaffold with a body containing a NavigationRail.
93
88
return Scaffold (
94
89
appBar: AppBar (
95
- title: widget. title,
90
+ title: title,
96
91
),
97
92
body: Row (
98
93
children: [
99
94
NavigationRail (
100
- leading: widget. floatingActionButton,
95
+ leading: floatingActionButton,
101
96
destinations: [
102
- for (final destination in widget. destinations)
97
+ for (final destination in destinations)
103
98
NavigationRailDestination (
104
99
icon: Icon (destination.icon),
105
100
label: Text (destination.title),
106
101
),
107
102
],
108
- selectedIndex: widget. currentIndex,
109
- onDestinationSelected: widget. onNavigationIndexChange ?? (_) {},
103
+ selectedIndex: currentIndex,
104
+ onDestinationSelected: onNavigationIndexChange ?? (_) {},
110
105
),
111
106
VerticalDivider (
112
107
width: 1 ,
113
108
thickness: 1 ,
114
109
),
115
110
Expanded (
116
- child: widget. body,
111
+ child: body,
117
112
),
118
113
],
119
114
),
@@ -127,15 +122,15 @@ class _AdaptiveNavigationScaffoldState
127
122
children: [
128
123
DrawerHeader (
129
124
child: Center (
130
- child: widget. title,
125
+ child: title,
131
126
),
132
127
),
133
- for (final destination in widget. destinations)
128
+ for (final destination in destinations)
134
129
ListTile (
135
130
leading: Icon (destination.icon),
136
131
title: Text (destination.title),
137
- selected: widget.destinations. indexOf (destination) ==
138
- widget. currentIndex,
132
+ selected:
133
+ destinations. indexOf (destination) == currentIndex,
139
134
onTap: () => _destinationTapped (destination),
140
135
),
141
136
],
@@ -148,8 +143,8 @@ class _AdaptiveNavigationScaffoldState
148
143
Expanded (
149
144
child: Scaffold (
150
145
appBar: AppBar (),
151
- body: widget. body,
152
- floatingActionButton: widget. floatingActionButton,
146
+ body: body,
147
+ floatingActionButton: floatingActionButton,
153
148
),
154
149
),
155
150
],
@@ -158,9 +153,9 @@ class _AdaptiveNavigationScaffoldState
158
153
}
159
154
160
155
void _destinationTapped (AdaptiveScaffoldDestination destination) {
161
- final index = widget. destinations.indexOf (destination);
162
- if (index != widget. currentIndex) {
163
- widget. onNavigationIndexChange (index);
156
+ final index = destinations.indexOf (destination);
157
+ if (index != currentIndex) {
158
+ onNavigationIndexChange (index);
164
159
}
165
160
}
166
161
}
0 commit comments