Add NavigationBar dartpad example (#97046)
This commit is contained in:
77
examples/api/lib/material/navigation_bar/navigation_bar.dart
Normal file
77
examples/api/lib/material/navigation_bar/navigation_bar.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
// 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],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user