Skip to content

Commit a7bdf36

Browse files
authored
improve focus example (#147464)
Part of #130459. Adds a test to the [second focus example](https://api.flutter.dev/flutter/widgets/Focus-class.html#widgets.Focus.2) and makes the function of the example more clear.
1 parent be3e916 commit a7bdf36

3 files changed

Lines changed: 101 additions & 2 deletions

File tree

dev/bots/check_code_samples.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@ final Set<String> _knownMissingTests = <String>{
459459
'examples/api/test/widgets/actions/focusable_action_detector.0_test.dart',
460460
'examples/api/test/widgets/color_filter/color_filtered.0_test.dart',
461461
'examples/api/test/widgets/focus_scope/focus.2_test.dart',
462-
'examples/api/test/widgets/focus_scope/focus.1_test.dart',
463462
'examples/api/test/widgets/focus_scope/focus_scope.0_test.dart',
464463
'examples/api/test/widgets/scroll_view/custom_scroll_view.1_test.dart',
465464
'examples/api/test/widgets/inherited_notifier/inherited_notifier.0_test.dart',

examples/api/lib/widgets/focus_scope/focus.1.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class FocusableText extends StatelessWidget {
4747
return Container(
4848
padding: const EdgeInsets.all(8.0),
4949
// Change the color based on whether or not this Container has focus.
50-
color: Focus.of(context).hasPrimaryFocus ? Colors.black12 : null,
50+
color: Focus.of(context).hasPrimaryFocus ? Colors.red : Colors.white,
5151
child: Text(data),
5252
);
5353
}),
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
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/material.dart';
6+
import 'package:flutter/services.dart';
7+
import 'package:flutter_api_samples/widgets/focus_scope/focus.1.dart'
8+
as example;
9+
import 'package:flutter_test/flutter_test.dart';
10+
11+
void main() {
12+
testWidgets('FocusableText shows content and color depending on focus',
13+
(WidgetTester tester) async {
14+
await tester.pumpWidget(const MaterialApp(
15+
home: Scaffold(
16+
body: example.FocusableText(
17+
'Item 0',
18+
autofocus: false,
19+
),
20+
),
21+
));
22+
// Autofocus needs to check that no other node in the [FocusScope] is
23+
// focused and can only request focus for the second frame.
24+
await tester.pumpAndSettle();
25+
expect(find.descendant(
26+
of: find.byType(example.FocusableText),
27+
matching: find.byType(Focus),
28+
), findsOneWidget);
29+
expect(find.text('Item 0'), findsOneWidget);
30+
31+
expect(find.byType(Container), findsOneWidget);
32+
final Container container1 = tester.widget<Container>(
33+
find.byType(Container)
34+
);
35+
expect(container1.color, Colors.white);
36+
37+
await tester.pumpWidget(const MaterialApp(
38+
home: Scaffold(
39+
body: example.FocusableText(
40+
'Item 1',
41+
autofocus: true,
42+
),
43+
),
44+
));
45+
await tester.pumpAndSettle();
46+
47+
final Finder focusableTextFinder2 = find.ancestor(
48+
of: find.text('Item 1'),
49+
matching: find.byType(example.FocusableText),
50+
);
51+
expect(tester.widget<Focus>(find.descendant(
52+
of: focusableTextFinder2,
53+
matching: find.byType(Focus),
54+
)).autofocus, isTrue);
55+
final Container container2 = tester.widget<Container>(find.descendant(
56+
of: focusableTextFinder2,
57+
matching: find.byType(Container),
58+
));
59+
expect(container2.color, Colors.red);
60+
});
61+
62+
testWidgets('builds list showcasing focus traversal',
63+
(WidgetTester tester) async {
64+
await tester.pumpWidget(const example.FocusExampleApp());
65+
await tester.pumpAndSettle();
66+
67+
expect(find.byType(ListView), findsOneWidget);
68+
69+
final Finder childFinder = find.descendant(
70+
of: find.byType(ListView),
71+
matching: find.byType(example.FocusableText),
72+
);
73+
expect(childFinder, findsAtLeastNWidgets(2));
74+
75+
Container container0 = tester.widget<Container>(find.descendant(
76+
of: childFinder.first,
77+
matching: find.byType(Container),
78+
));
79+
Container container1 = tester.widget<Container>(find.descendant(
80+
of: childFinder.at(1),
81+
matching: find.byType(Container),
82+
));
83+
expect(container0.color, Colors.red);
84+
expect(container1.color, Colors.white);
85+
86+
await tester.sendKeyDownEvent(LogicalKeyboardKey.arrowDown);
87+
await tester.pumpAndSettle();
88+
89+
container0 = tester.widget<Container>(find.descendant(
90+
of: childFinder.first,
91+
matching: find.byType(Container),
92+
));
93+
container1 = tester.widget<Container>(find.descendant(
94+
of: childFinder.at(1),
95+
matching: find.byType(Container),
96+
));
97+
expect(container0.color, Colors.white);
98+
expect(container1.color, Colors.red);
99+
});
100+
}

0 commit comments

Comments
 (0)