Skip to content

Commit add4d18

Browse files
[flutter_adaptive_scaffold] Change selectedIndex on standardNavigationRail to allow null value. (flutter#3088)
* changed selectedIndex on standardNavigationRail to allow null value * Update CHANGELOG.md * increase version number * Update CHANGELOG.md * Update CHANGELOG.md * change index types from int to int? * add test for slectedIndex can be set to null * Update CHANGELOG.md * index types set to int? and ensure BottomNavigationRail currentIndex isn't null * Update packages/flutter_adaptive_scaffold/CHANGELOG.md Co-authored-by: Greg Spencer <[email protected]> * Update packages/flutter_adaptive_scaffold/lib/src/adaptive_scaffold.dart Co-authored-by: Greg Spencer <[email protected]> * Update packages/flutter_adaptive_scaffold/CHANGELOG.md Co-authored-by: Greg Spencer <[email protected]> Co-authored-by: Greg Spencer <[email protected]>
1 parent 59707ba commit add4d18

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

packages/flutter_adaptive_scaffold/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.1.0
2+
3+
* Change the `selectedIndex` parameter on `standardNavigationRail` to allow null values to indicate "no destination".
4+
* An explicitly null `currentIndex` parameter passed to `standardBottomNavigationBar` will also default to 0, just like implicitly null missing parameters.
5+
6+
17
## 0.0.9
28

39
* Fix passthrough of `leadingExtendedNavRail`, `leadingUnextendedNavRail` and `trailingNavRail`

packages/flutter_adaptive_scaffold/lib/src/adaptive_scaffold.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class AdaptiveScaffold extends StatefulWidget {
110110
final List<NavigationDestination> destinations;
111111

112112
/// The index to be used by the [NavigationRail].
113-
final int selectedIndex;
113+
final int? selectedIndex;
114114

115115
/// Option to display a leading widget at the top of the navigation rail
116116
/// at the middle breakpoint.
@@ -251,7 +251,7 @@ class AdaptiveScaffold extends StatefulWidget {
251251
static Builder standardNavigationRail({
252252
required List<NavigationRailDestination> destinations,
253253
double width = 72,
254-
int selectedIndex = 0,
254+
int? selectedIndex,
255255
bool extended = false,
256256
Color backgroundColor = Colors.transparent,
257257
EdgeInsetsGeometry padding = const EdgeInsets.all(8.0),
@@ -305,14 +305,15 @@ class AdaptiveScaffold extends StatefulWidget {
305305
/// a list of [NavigationDestination]s.
306306
static Builder standardBottomNavigationBar({
307307
required List<NavigationDestination> destinations,
308-
int currentIndex = 0,
308+
int? currentIndex,
309309
double iconSize = 24,
310310
ValueChanged<int>? onDestinationSelected,
311311
}) {
312+
currentIndex ??= 0;
312313
return Builder(
313314
builder: (_) {
314315
return BottomNavigationBar(
315-
currentIndex: currentIndex,
316+
currentIndex: currentIndex ?? 0,
316317
iconSize: iconSize,
317318
items: destinations
318319
.map((NavigationDestination e) => _toBottomNavItem(e))

packages/flutter_adaptive_scaffold/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_adaptive_scaffold
22
description: Widgets to easily build adaptive layouts, including navigation elements.
3-
version: 0.0.9
3+
version: 0.1.0
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_adaptive_scaffold%22
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_adaptive_scaffold
66

packages/flutter_adaptive_scaffold/test/adaptive_scaffold_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:flutter/material.dart';
66
import 'package:flutter_adaptive_scaffold/src/adaptive_scaffold.dart';
77
import 'package:flutter_test/flutter_test.dart';
8+
89
import 'simulated_layout.dart';
910
import 'test_breakpoints.dart';
1011

@@ -222,6 +223,22 @@ void main() {
222223
});
223224
},
224225
);
226+
227+
/// Verify that selectedIndex of [AdaptiveScaffold.standardNavigationRail]
228+
/// and [AdaptiveScaffold] can be set to null
229+
testWidgets(
230+
'adaptive scaffold selectedIndex can be set to null',
231+
(WidgetTester tester) async {
232+
await Future.forEach(SimulatedLayout.values,
233+
(SimulatedLayout region) async {
234+
int? selectedIndex;
235+
final MaterialApp app = region.app(initialIndex: selectedIndex);
236+
await tester.binding.setSurfaceSize(region.size);
237+
await tester.pumpWidget(app);
238+
await tester.pumpAndSettle();
239+
});
240+
},
241+
);
225242
}
226243

227244
/// An empty widget that implements [PreferredSizeWidget] to ensure that

packages/flutter_adaptive_scaffold/test/simulated_layout.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TestScaffold extends StatefulWidget {
3636
this.isAnimated = true,
3737
});
3838

39-
final int initialIndex;
39+
final int? initialIndex;
4040
final bool isAnimated;
4141

4242
static const List<NavigationDestination> destinations =
@@ -63,7 +63,7 @@ class TestScaffold extends StatefulWidget {
6363
}
6464

6565
class TestScaffoldState extends State<TestScaffold> {
66-
late int index = widget.initialIndex;
66+
late int? index = widget.initialIndex;
6767

6868
@override
6969
Widget build(BuildContext context) {
@@ -110,7 +110,7 @@ enum SimulatedLayout {
110110
Size get size => Size(_width, _height);
111111

112112
MaterialApp app({
113-
int initialIndex = 0,
113+
int? initialIndex,
114114
bool animations = true,
115115
}) {
116116
return MaterialApp(

0 commit comments

Comments
 (0)