Skip to content

Commit 029be53

Browse files
authored
[material_ui] Migrate MaterialApp API doc snippets to {@example} and add unit tests (batch 6) (flutter#13021)
This PR addresses batch 6 of the migration detailed in flutter#188530 (comment). `app.snippet.4.dart‎` contains some non-trivial changes to fix a type error and to make it testable. ## 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 4bc7212 commit 029be53

11 files changed

Lines changed: 282 additions & 92 deletions
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:material_ui/material_ui.dart';
6+
7+
/// Flutter code sample for [MaterialApp].
8+
9+
class MaterialAppExample extends StatelessWidget {
10+
const MaterialAppExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return
15+
// #region body
16+
MaterialApp(
17+
home: Scaffold(appBar: AppBar(title: const Text('Home'))),
18+
debugShowCheckedModeBanner: false,
19+
)
20+
// #endregion body
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:material_ui/material_ui.dart';
6+
7+
/// Flutter code sample for [MaterialApp].
8+
9+
class MaterialAppExample extends StatelessWidget {
10+
const MaterialAppExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return
15+
// #region body
16+
MaterialApp(
17+
routes: <String, WidgetBuilder>{
18+
'/': (BuildContext context) {
19+
return Scaffold(appBar: AppBar(title: const Text('Home Route')));
20+
},
21+
'/about': (BuildContext context) {
22+
return Scaffold(appBar: AppBar(title: const Text('About Route')));
23+
},
24+
},
25+
)
26+
// #endregion body
27+
;
28+
}
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 [MaterialApp].
8+
9+
class MaterialAppExample extends StatelessWidget {
10+
const MaterialAppExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return
15+
// #region body
16+
MaterialApp(
17+
theme: ThemeData(
18+
brightness: Brightness.dark,
19+
primaryColor: Colors.blueGrey,
20+
),
21+
home: Scaffold(appBar: AppBar(title: const Text('MaterialApp Theme'))),
22+
)
23+
// #endregion body
24+
;
25+
}
26+
}
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:flutter/services.dart';
6+
import 'package:material_ui/material_ui.dart';
7+
8+
/// Flutter code sample for [MaterialApp.shortcuts].
9+
10+
class MaterialAppExample extends StatelessWidget {
11+
const MaterialAppExample({super.key});
12+
13+
// #region body
14+
@override
15+
Widget build(BuildContext context) {
16+
return WidgetsApp(
17+
shortcuts: <ShortcutActivator, Intent>{
18+
...WidgetsApp.defaultShortcuts,
19+
const SingleActivator(LogicalKeyboardKey.keyE): const ActivateIntent(),
20+
},
21+
color: const Color(0xFFFF0000),
22+
builder: (BuildContext context, Widget? child) {
23+
return const Placeholder();
24+
},
25+
);
26+
}
27+
// #endregion body
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 [MaterialApp.actions].
8+
9+
class MaterialAppExample extends StatelessWidget {
10+
const MaterialAppExample({super.key, this.onActivated});
11+
12+
final VoidCallback? onActivated;
13+
14+
// #region body
15+
@override
16+
Widget build(BuildContext context) {
17+
return WidgetsApp(
18+
actions: <Type, Action<Intent>>{
19+
...WidgetsApp.defaultActions,
20+
ActivateIntent: CallbackAction<Intent>(
21+
onInvoke: (Intent intent) {
22+
// Do something here...
23+
onActivated?.call();
24+
return null;
25+
},
26+
),
27+
},
28+
color: const Color(0xFFFF0000),
29+
builder: (BuildContext context, Widget? child) {
30+
return const Placeholder();
31+
},
32+
);
33+
}
34+
// #endregion body
35+
}
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/app.snippet.0.dart' as example;
8+
9+
void main() {
10+
testWidgets(
11+
'MaterialApp displays home route and disables debug mode banner',
12+
(WidgetTester tester) async {
13+
await tester.pumpWidget(const example.MaterialAppExample());
14+
15+
expect(find.byType(CheckedModeBanner), findsNothing);
16+
expect(find.widgetWithText(AppBar, 'Home'), findsOneWidget);
17+
},
18+
);
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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/app.snippet.1.dart' as example;
8+
9+
void main() {
10+
testWidgets('MaterialApp routes map navigates between Home and About', (
11+
WidgetTester tester,
12+
) async {
13+
await tester.pumpWidget(const example.MaterialAppExample());
14+
15+
expect(find.widgetWithText(AppBar, 'Home Route'), findsOneWidget);
16+
expect(find.widgetWithText(AppBar, 'About Route'), findsNothing);
17+
18+
final BuildContext context = tester.element(find.text('Home Route'));
19+
Navigator.of(context).pushNamed('/about');
20+
await tester.pumpAndSettle();
21+
22+
expect(find.widgetWithText(AppBar, 'About Route'), findsOneWidget);
23+
});
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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/app.snippet.2.dart' as example;
8+
9+
void main() {
10+
testWidgets('MaterialApp applies custom ThemeData', (
11+
WidgetTester tester,
12+
) async {
13+
await tester.pumpWidget(const example.MaterialAppExample());
14+
15+
expect(find.widgetWithText(AppBar, 'MaterialApp Theme'), findsOneWidget);
16+
17+
final BuildContext context = tester.element(find.text('MaterialApp Theme'));
18+
final ThemeData theme = Theme.of(context);
19+
expect(theme.brightness, Brightness.dark);
20+
expect(theme.primaryColor, Colors.blueGrey);
21+
});
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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/services.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:material_ui/material_ui.dart';
8+
import 'package:material_ui_examples/app/app.snippet.3.dart' as example;
9+
10+
void main() {
11+
testWidgets('WidgetsApp shortcuts maps select key to ActivateIntent', (
12+
WidgetTester tester,
13+
) async {
14+
bool invoked = false;
15+
await tester.pumpWidget(
16+
Actions(
17+
actions: <Type, Action<Intent>>{
18+
ActivateIntent: CallbackAction<ActivateIntent>(
19+
onInvoke: (ActivateIntent intent) {
20+
invoked = true;
21+
return null;
22+
},
23+
),
24+
},
25+
child: const example.MaterialAppExample(),
26+
),
27+
);
28+
29+
final Element placeholderElement = tester.element(find.byType(Placeholder));
30+
final FocusNode focusNode = Focus.of(placeholderElement);
31+
focusNode.canRequestFocus = true;
32+
focusNode.requestFocus();
33+
await tester.pump();
34+
35+
await tester.sendKeyEvent(LogicalKeyboardKey.keyE);
36+
expect(invoked, isTrue);
37+
});
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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/services.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:material_ui/material_ui.dart';
8+
import 'package:material_ui_examples/app/app.snippet.4.dart' as example;
9+
10+
void main() {
11+
testWidgets('WidgetsApp actions handles ActivateIntent', (
12+
WidgetTester tester,
13+
) async {
14+
bool activated = false;
15+
await tester.pumpWidget(
16+
example.MaterialAppExample(
17+
onActivated: () {
18+
activated = true;
19+
},
20+
),
21+
);
22+
23+
final Element placeholderElement = tester.element(find.byType(Placeholder));
24+
final FocusNode focusNode = Focus.of(placeholderElement);
25+
focusNode.canRequestFocus = true;
26+
focusNode.requestFocus();
27+
await tester.pump();
28+
29+
await tester.sendKeyEvent(LogicalKeyboardKey.space);
30+
expect(activated, isTrue);
31+
});
32+
}

0 commit comments

Comments
 (0)