Skip to content

Commit 4bc7212

Browse files
authored
[material_ui] Migrate AppBar, SliverAppBar, BottomAppBar, and Scaffold API doc snippets to {@example} (batch 7) (flutter#13023)
This PR addresses batch 7 of the migration detailed in flutter#188530 (comment). ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 6d17b57 commit 4bc7212

13 files changed

Lines changed: 319 additions & 84 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:material_ui/material_ui.dart';
6+
7+
/// Flutter code sample for [AppBar.leading].
8+
9+
class AppBarExample extends StatelessWidget {
10+
const AppBarExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return Scaffold(
15+
drawer: const Drawer(child: Text('Drawer')),
16+
appBar:
17+
// #region body
18+
AppBar(
19+
leading: Builder(
20+
builder: (BuildContext context) {
21+
return IconButton(
22+
icon: const Icon(Icons.menu),
23+
onPressed: () {
24+
Scaffold.of(context).openDrawer();
25+
},
26+
tooltip: MaterialLocalizations.of(
27+
context,
28+
).openAppDrawerTooltip,
29+
);
30+
},
31+
),
32+
),
33+
// #endregion body
34+
);
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:material_ui/material_ui.dart';
6+
7+
/// Flutter code sample for [AppBar.actions].
8+
9+
class AppBarExample extends StatelessWidget {
10+
const AppBarExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return
15+
// #region body
16+
Scaffold(
17+
body: CustomScrollView(
18+
primary: true,
19+
slivers: <Widget>[
20+
SliverAppBar(
21+
title: const Text('Hello World'),
22+
actions: <Widget>[
23+
IconButton(
24+
icon: const Icon(Icons.shopping_cart),
25+
tooltip: 'Open shopping cart',
26+
onPressed: () {
27+
// handle the press
28+
},
29+
),
30+
],
31+
),
32+
// ...rest of body...
33+
],
34+
),
35+
)
36+
// #endregion body
37+
;
38+
}
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:material_ui/material_ui.dart';
6+
7+
/// Flutter code sample for [SliverAppBar].
8+
9+
class SliverAppBarExample extends StatelessWidget {
10+
const SliverAppBarExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return CustomScrollView(
15+
slivers: <Widget>[
16+
// #region body
17+
SliverAppBar(
18+
expandedHeight: 150.0,
19+
flexibleSpace: const FlexibleSpaceBar(title: Text('Available seats')),
20+
actions: <Widget>[
21+
IconButton(
22+
icon: const Icon(Icons.add_circle),
23+
tooltip: 'Add new entry',
24+
onPressed: () {
25+
/* ... */
26+
},
27+
),
28+
],
29+
),
30+
// #endregion body
31+
],
32+
);
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:material_ui/material_ui.dart';
6+
7+
/// Flutter code sample for [BottomAppBar].
8+
9+
class BottomAppBarExample extends StatelessWidget {
10+
const BottomAppBarExample({super.key, this.bottomAppBarContents});
11+
12+
final Widget? bottomAppBarContents;
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return
17+
// #region body
18+
Scaffold(
19+
bottomNavigationBar: BottomAppBar(
20+
color: Colors.white,
21+
child: bottomAppBarContents,
22+
),
23+
floatingActionButton: const FloatingActionButton(onPressed: null),
24+
)
25+
// #endregion body
26+
;
27+
}
28+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:material_ui/material_ui.dart';
6+
7+
/// Flutter code sample for [Scaffold].
8+
9+
class ScaffoldExample extends StatefulWidget {
10+
const ScaffoldExample({super.key});
11+
12+
@override
13+
State<ScaffoldExample> createState() => _ScaffoldExampleState();
14+
}
15+
16+
class _ScaffoldExampleState extends State<ScaffoldExample>
17+
with SingleTickerProviderStateMixin {
18+
static const int tabCount = 3;
19+
String appBarTitle = 'Tab 0';
20+
21+
TickerProvider get tickerProvider => this;
22+
23+
late final TabController tabController =
24+
// #region body
25+
TabController(vsync: tickerProvider, length: tabCount)..addListener(() {
26+
if (!tabController.indexIsChanging) {
27+
setState(() {
28+
// Rebuild the enclosing scaffold with a new AppBar title
29+
appBarTitle = 'Tab ${tabController.index}';
30+
});
31+
}
32+
});
33+
// #endregion body
34+
35+
@override
36+
void dispose() {
37+
tabController.dispose();
38+
super.dispose();
39+
}
40+
41+
@override
42+
Widget build(BuildContext context) {
43+
return Scaffold(
44+
appBar: AppBar(
45+
title: Text(appBarTitle),
46+
bottom: TabBar(
47+
controller: tabController,
48+
tabs: List<Widget>.generate(
49+
tabCount,
50+
(int index) => Tab(text: 'Item $index'),
51+
),
52+
),
53+
),
54+
body: TabBarView(
55+
controller: tabController,
56+
children: List<Widget>.generate(
57+
tabCount,
58+
(int index) => Center(child: Text('Content $index')),
59+
),
60+
),
61+
);
62+
}
63+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:material_ui/material_ui.dart';
7+
import 'package:material_ui_examples/app_bar/app_bar.snippet.0.dart' as example;
8+
9+
void main() {
10+
testWidgets('AppBar leading button opens Scaffold drawer', (
11+
WidgetTester tester,
12+
) async {
13+
await tester.pumpWidget(const MaterialApp(home: example.AppBarExample()));
14+
15+
expect(find.text('Drawer'), findsNothing);
16+
await tester.tap(find.byIcon(Icons.menu));
17+
await tester.pumpAndSettle();
18+
expect(find.text('Drawer'), findsOneWidget);
19+
});
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:material_ui/material_ui.dart';
7+
import 'package:material_ui_examples/app_bar/app_bar.snippet.1.dart' as example;
8+
9+
void main() {
10+
testWidgets('SliverAppBar displays title and shopping cart action button', (
11+
WidgetTester tester,
12+
) async {
13+
await tester.pumpWidget(const MaterialApp(home: example.AppBarExample()));
14+
15+
expect(find.text('Hello World'), findsOneWidget);
16+
expect(find.byIcon(Icons.shopping_cart), findsOneWidget);
17+
expect(find.byTooltip('Open shopping cart'), findsOneWidget);
18+
});
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:material_ui/material_ui.dart';
7+
import 'package:material_ui_examples/app_bar/sliver_app_bar.snippet.0.dart'
8+
as example;
9+
10+
void main() {
11+
testWidgets(
12+
'SliverAppBar displays flexible space title and add_circle action',
13+
(WidgetTester tester) async {
14+
await tester.pumpWidget(
15+
const MaterialApp(home: Scaffold(body: example.SliverAppBarExample())),
16+
);
17+
18+
expect(find.text('Available seats'), findsOneWidget);
19+
expect(find.byIcon(Icons.add_circle), findsOneWidget);
20+
expect(find.byTooltip('Add new entry'), findsOneWidget);
21+
},
22+
);
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:material_ui/material_ui.dart';
7+
import 'package:material_ui_examples/bottom_app_bar/bottom_app_bar.snippet.0.dart'
8+
as example;
9+
10+
void main() {
11+
testWidgets('Scaffold renders BottomAppBar and FloatingActionButton', (
12+
WidgetTester tester,
13+
) async {
14+
await tester.pumpWidget(
15+
const MaterialApp(
16+
home: example.BottomAppBarExample(
17+
bottomAppBarContents: Text('Bottom Content'),
18+
),
19+
),
20+
);
21+
22+
final BottomAppBar bottomAppBar = tester.widget<BottomAppBar>(
23+
find.byType(BottomAppBar),
24+
);
25+
expect(bottomAppBar.color, Colors.white);
26+
expect(find.text('Bottom Content'), findsOneWidget);
27+
expect(find.byType(FloatingActionButton), findsOneWidget);
28+
});
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:material_ui/material_ui.dart';
7+
import 'package:material_ui_examples/scaffold/scaffold.snippet.0.dart'
8+
as example;
9+
10+
void main() {
11+
testWidgets('TabController listener updates Scaffold AppBar title', (
12+
WidgetTester tester,
13+
) async {
14+
await tester.pumpWidget(const MaterialApp(home: example.ScaffoldExample()));
15+
16+
expect(find.widgetWithText(AppBar, 'Tab 0'), findsOneWidget);
17+
18+
await tester.tap(find.text('Item 2'));
19+
await tester.pumpAndSettle();
20+
21+
expect(find.widgetWithText(AppBar, 'Tab 2'), findsOneWidget);
22+
});
23+
}

0 commit comments

Comments
 (0)