Skip to content

Commit eb2ecc2

Browse files
authored
[cupertino_ui] Migrate a snippet in CupertinoCheckbox's API doc to {@example} and add unit tests (flutter#12180)
This PR demonstrates how to migrate an inline API documentation snippet into a separate file and verify it with unit tests. ### Steps 1. Create a new example file. * The file name should contain `snippet`. * The example file doesn't need a `main` function. * The widget body should contain the snippet, which is isolated by `#region` and `#endregion` directives. * If necessary, the file can have multiple regions. The region name doesn't have to be `body`. 2. Add a corresponding test file to ensure the snippet functions correctly. 3. Update the original documentation to use the new `@example` directive. 4. Remove the TODO. ## Pre-Review Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [ ] I read the [Tree Hygiene] page, which explains my responsibilities. - [ ] I read and followed the [relevant style guides] and ran [the auto-formatter]. - [ ] I signed the [CLA]. - [ ] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]` - [ ] I [linked to at least one issue that this PR fixes] in the description above. - [ ] I followed [the version and CHANGELOG instructions], using [semantic versioning] and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1]. - [ ] I updated/added any relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under[^1]. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **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. <!-- Links --> [Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style [the auto-formatter]: https://github.com/flutter/packages/blob/main/script/tool/README.md#format-code [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [the version and CHANGELOG instructions]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version-and-changelog-updates [semantic versioning]: https://dart.dev/tools/pub/versioning#semantic-versions [repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style [test exemption]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests
1 parent afe954a commit eb2ecc2

3 files changed

Lines changed: 87 additions & 15 deletions

File tree

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:cupertino_ui/cupertino_ui.dart';
6+
7+
/// Flutter code sample for [CupertinoCheckbox].
8+
9+
class CupertinoCheckboxExample extends StatelessWidget {
10+
const CupertinoCheckboxExample({super.key, required this.onChanged});
11+
12+
final ValueChanged<bool?>? onChanged;
13+
14+
ValueChanged<bool?>? get _nullableOnChanged => onChanged;
15+
bool get _value => true;
16+
17+
@override
18+
Widget build(BuildContext context) {
19+
return
20+
// #region body
21+
CupertinoCheckbox(
22+
value: _value,
23+
// If `onChanged` is null, this checkbox is disabled.
24+
// If `onChanged` is not null, this checkbox is enabled.
25+
onChanged: _nullableOnChanged,
26+
fillColor: WidgetStateProperty.resolveWith<Color>((
27+
Set<WidgetState> states,
28+
) {
29+
if (states.contains(WidgetState.disabled)) {
30+
return CupertinoColors.activeOrange.withValues(alpha: .32);
31+
}
32+
return CupertinoColors.activeOrange;
33+
}),
34+
)
35+
// #endregion body
36+
;
37+
}
38+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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:cupertino_ui/cupertino_ui.dart';
6+
import 'package:cupertino_ui_examples/checkbox/cupertino_checkbox.snippet.0.dart'
7+
as example;
8+
import 'package:flutter_test/flutter_test.dart';
9+
10+
void main() {
11+
testWidgets('Checkbox color is affected by whether it is enabled', (
12+
WidgetTester tester,
13+
) async {
14+
RenderBox getCheckboxRenderer() {
15+
return tester.renderObject<RenderBox>(find.byType(CupertinoCheckbox));
16+
}
17+
18+
Widget buildApp({required ValueChanged<bool?>? onChanged}) {
19+
return CupertinoApp(
20+
theme: CupertinoThemeData(brightness: .light),
21+
home: CupertinoPageScaffold(
22+
navigationBar: CupertinoNavigationBar(
23+
middle: Text('CupertinoCheckbox Example'),
24+
),
25+
child: SafeArea(
26+
child: example.CupertinoCheckboxExample(onChanged: onChanged),
27+
),
28+
),
29+
);
30+
}
31+
32+
await tester.pumpWidget(buildApp(onChanged: (bool? _) {}));
33+
await tester.pumpAndSettle();
34+
expect(
35+
getCheckboxRenderer(),
36+
paints..rrect(color: CupertinoColors.activeOrange.color),
37+
);
38+
39+
await tester.pumpWidget(buildApp(onChanged: null));
40+
await tester.pumpAndSettle();
41+
expect(
42+
getCheckboxRenderer(),
43+
paints..rrect(
44+
color: CupertinoColors.activeOrange.color.withValues(alpha: .32),
45+
),
46+
);
47+
});
48+
}

‎packages/cupertino_ui/lib/src/checkbox.dart‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,25 +210,11 @@ class CupertinoCheckbox extends StatefulWidget {
210210
///
211211
/// <callout-box>
212212
///
213-
// TODO(framework): Add unit tests to this code snippet.
214-
// https://github.com/flutter/flutter/issues/188530
215-
///
216213
/// This example resolves the [fillColor] based on the current [WidgetState]
217214
/// of the [CupertinoCheckbox], providing a different [Color] when it is
218215
/// [WidgetState.disabled].
219216
///
220-
/// ```dart
221-
/// CupertinoCheckbox(
222-
/// value: true,
223-
/// onChanged: (_){},
224-
/// fillColor: WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {
225-
/// if (states.contains(WidgetState.disabled)) {
226-
/// return Colors.orange.withValues(alpha: .32);
227-
/// }
228-
/// return Colors.orange;
229-
/// })
230-
/// )
231-
/// ```
217+
/// {@example /example/lib/checkbox/cupertino_checkbox.snippet.0.dart#body indent=strip}
232218
///
233219
/// </callout-box>
234220
/// {@endtemplate}

0 commit comments

Comments
 (0)