Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
0038a3e2
Unverified
Commit
0038a3e2
authored
Mar 01, 2022
by
Mahesh Jamdade
Committed by
GitHub
Mar 01, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add NavigationBar dartpad example (#97046)
parent
4be09a11
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
124 additions
and
38 deletions
+124
-38
navigation_bar.dart
examples/api/lib/material/navigation_bar/navigation_bar.dart
+77
-0
navigation_bar_test.dart
...api/test/material/navigation_bar/navigation_bar_test.dart
+37
-0
navigation_bar.dart
packages/flutter/lib/src/material/navigation_bar.dart
+10
-38
No files found.
examples/api/lib/material/navigation_bar/navigation_bar.dart
0 → 100644
View file @
0038a3e2
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flutter code sample for NavigationBar
import
'package:flutter/material.dart'
;
void
main
(
)
{
runApp
(
const
MyApp
());
}
class
MyApp
extends
StatelessWidget
{
const
MyApp
({
Key
?
key
})
:
super
(
key:
key
);
@override
Widget
build
(
BuildContext
context
)
{
return
const
MaterialApp
(
home:
NavigationExample
());
}
}
class
NavigationExample
extends
StatefulWidget
{
const
NavigationExample
({
Key
?
key
})
:
super
(
key:
key
);
@override
State
<
NavigationExample
>
createState
()
=>
_NavigationExampleState
();
}
class
_NavigationExampleState
extends
State
<
NavigationExample
>
{
int
currentPageIndex
=
0
;
@override
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
bottomNavigationBar:
NavigationBar
(
onDestinationSelected:
(
int
index
)
{
setState
(()
{
currentPageIndex
=
index
;
});
},
selectedIndex:
currentPageIndex
,
destinations:
const
<
Widget
>[
NavigationDestination
(
icon:
Icon
(
Icons
.
explore
),
label:
'Explore'
,
),
NavigationDestination
(
icon:
Icon
(
Icons
.
commute
),
label:
'Commute'
,
),
NavigationDestination
(
selectedIcon:
Icon
(
Icons
.
bookmark
),
icon:
Icon
(
Icons
.
bookmark_border
),
label:
'Saved'
,
),
],
),
body:
<
Widget
>[
Container
(
color:
Colors
.
red
,
alignment:
Alignment
.
center
,
child:
const
Text
(
'Page 1'
),
),
Container
(
color:
Colors
.
green
,
alignment:
Alignment
.
center
,
child:
const
Text
(
'Page 2'
),
),
Container
(
color:
Colors
.
blue
,
alignment:
Alignment
.
center
,
child:
const
Text
(
'Page 3'
),
),
][
currentPageIndex
],
);
}
}
examples/api/test/material/navigation_bar/navigation_bar_test.dart
0 → 100644
View file @
0038a3e2
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'package:flutter/material.dart'
;
import
'package:flutter_api_samples/material/navigation_bar/navigation_bar.dart'
as
example
;
import
'package:flutter_test/flutter_test.dart'
;
void
main
(
)
{
testWidgets
(
'Navigation bar updates destination on tap'
,
(
WidgetTester
tester
)
async
{
await
tester
.
pumpWidget
(
const
example
.
MyApp
(),
);
final
NavigationBar
navigationBarWidget
=
tester
.
firstWidget
(
find
.
byType
(
NavigationBar
));
/// NavigationDestinations must be rendered
expect
(
find
.
text
(
'Explore'
),
findsOneWidget
);
expect
(
find
.
text
(
'Commute'
),
findsOneWidget
);
expect
(
find
.
text
(
'Saved'
),
findsOneWidget
);
/// initial index must be zero
expect
(
navigationBarWidget
.
selectedIndex
,
0
);
/// switch to second tab
await
tester
.
tap
(
find
.
text
(
'Commute'
));
await
tester
.
pumpAndSettle
();
expect
(
find
.
text
(
'Page 2'
),
findsOneWidget
);
/// switch to third tab
await
tester
.
tap
(
find
.
text
(
'Saved'
));
await
tester
.
pumpAndSettle
();
expect
(
find
.
text
(
'Page 3'
),
findsOneWidget
);
});
}
packages/flutter/lib/src/material/navigation_bar.dart
View file @
0038a3e2
...
...
@@ -33,47 +33,19 @@ import 'tooltip.dart';
/// This widget holds a collection of destinations (usually
/// [NavigationDestination]s).
///
/// Usage:
/// ```dart
/// Scaffold(
/// bottomNavigationBar: NavigationBar(
/// onDestinationSelected: (int index) {
/// setState(() { _currentPageIndex = index; }),
/// },
/// selectedIndex: _currentPageIndex,
/// destinations: [
/// NavigationDestination(
/// icon: Icon(Icons.explore),
/// label: 'Explore',
/// ),
/// NavigationDestination(
/// icon: Icon(Icons.commute),
/// label: 'Commute',
/// ),
/// NavigationDestination(
/// selectedIcon: Icon(Icons.bookmark),
/// icon: Icon(Icons.bookmark_border),
/// label: 'Saved',
/// ),
/// ],
/// ),
/// ),
/// ```
///
/// {@tool dartpad}
/// This example has a [NavigationBar] where each destination has its
/// own Navigator, Scaffold, and Appbar. That means that each
/// destination has an independent route history and (app bar) back
/// button. A [Stack] is used to display one destination at a time and
/// destination changes are handled by cross fade transitions. Destinations
/// that have been completely faded out are [Offstage].
///
/// One can see that the appearance of each destination's dialogs, bottom sheet,
/// list scrolling state, and text field state, persist when another destination
/// is selected.
/// This example shows a [NavigationBar] as it is used within a [Scaffold]
/// widget. The [NavigationBar] has three [NavigationDestination] widgets
/// and the [selectedIndex] is set to index 0. The `onDestinationSelected` callback
/// changes the selected item's index and displays a corresponding widget in the body of the [Scaffold].
///
/// ** See code in examples/api/lib/material/navigation_bar/navigation_bar.
0.
dart **
/// ** See code in examples/api/lib/material/navigation_bar/navigation_bar.dart **
/// {@end-tool}
/// See also:
///
/// * [NavigationDestination]
/// * [BottomNavigationBar]
/// * <https://api.flutter.dev/flutter/material/NavigationDestination-class.html>
class
NavigationBar
extends
StatelessWidget
{
/// Creates a Material 3 Navigation Bar component.
///
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment