Add Material 3 CheckboxListTile example and update existing examples (#118792)

* Add Material 3 `CheckboxListTile` example and update existing examples

* fix `list_tile.dart` doc issues

* Remove unnecessary comma
This commit is contained in:
Taha Tesser
2023-01-25 16:21:18 +03:00
committed by GitHub
parent eced23eaba
commit 29ab437e28
12 changed files with 432 additions and 185 deletions

View File

@@ -0,0 +1,32 @@
// 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/scheduler.dart';
import 'package:flutter_api_samples/material/checkbox_list_tile/checkbox_list_tile.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('CheckboxListTile can be checked', (WidgetTester tester) async {
await tester.pumpWidget(
const example.CheckboxListTileApp(),
);
CheckboxListTile checkboxListTile = tester.widget(find.byType(CheckboxListTile));
expect(checkboxListTile.value, isFalse);
await tester.tap(find.byType(CheckboxListTile));
await tester.pump();
timeDilation = 1.0;
checkboxListTile = tester.widget(find.byType(CheckboxListTile));
expect(checkboxListTile.value, isTrue);
await tester.tap(find.byType(CheckboxListTile));
await tester.pump();
checkboxListTile = tester.widget(find.byType(CheckboxListTile));
expect(checkboxListTile.value, isFalse);
});
}

View File

@@ -0,0 +1,75 @@
// 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/checkbox_list_tile/checkbox_list_tile.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Checkbox aligns appropriately', (WidgetTester tester) async {
await tester.pumpWidget(
const example.CheckboxListTileApp(),
);
expect(find.byType(CheckboxListTile), findsNWidgets(3));
Offset tileTopLeft = tester.getTopLeft(find.byType(CheckboxListTile).at(0));
Offset checkboxTopLeft = tester.getTopLeft(find.byType(Checkbox).at(0));
// The checkbox is centered vertically with the text.
expect(checkboxTopLeft - tileTopLeft, const Offset(736.0, 16.0));
tileTopLeft = tester.getTopLeft(find.byType(CheckboxListTile).at(1));
checkboxTopLeft = tester.getTopLeft(find.byType(Checkbox).at(1));
// The checkbox is centered vertically with the text.
expect(checkboxTopLeft - tileTopLeft, const Offset(736.0, 30.0));
tileTopLeft = tester.getTopLeft(find.byType(CheckboxListTile).at(2));
checkboxTopLeft = tester.getTopLeft(find.byType(Checkbox).at(2));
// The checkbox is aligned to the top vertically with the text.
expect(checkboxTopLeft - tileTopLeft, const Offset(736.0, 8.0));
});
testWidgets('Checkboxes can be checked', (WidgetTester tester) async {
await tester.pumpWidget(
const example.CheckboxListTileApp(),
);
expect(find.byType(CheckboxListTile), findsNWidgets(3));
// All checkboxes are checked.
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(0)).value, isTrue);
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(1)).value, isTrue);
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(2)).value, isTrue);
// Tap the first checkbox.
await tester.tap(find.byType(Checkbox).at(0));
await tester.pumpAndSettle();
// The first checkbox is unchecked.
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(0)).value, isFalse);
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(1)).value, isTrue);
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(2)).value, isTrue);
// Tap the second checkbox.
await tester.tap(find.byType(Checkbox).at(1));
await tester.pumpAndSettle();
// The first and second checkboxes are unchecked.
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(0)).value, isFalse);
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(1)).value, isFalse);
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(2)).value, isTrue);
// Tap the third checkbox.
await tester.tap(find.byType(Checkbox).at(2));
await tester.pumpAndSettle();
// All checkboxes are unchecked.
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(0)).value, isFalse);
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(1)).value, isFalse);
expect(tester.widget<Checkbox>(find.byType(Checkbox).at(2)).value, isFalse);
});
}

View File

@@ -0,0 +1,31 @@
// 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/checkbox_list_tile/custom_labeled_checkbox.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('LinkedLabelCheckbox contains RichText and Checkbox', (WidgetTester tester) async {
await tester.pumpWidget(
const example.LabeledCheckBoxApp(),
);
// Label text is in a RichText widget with the correct text.
final RichText richText = tester.widget(find.byType(RichText).first);
expect(richText.text.toPlainText(), 'Linked, tappable label text');
// Checkbox is initially unchecked.
Checkbox checkbox = tester.widget(find.byType(Checkbox));
expect(checkbox.value, isFalse);
// Tap the checkbox to check it.
await tester.tap(find.byType(Checkbox));
await tester.pumpAndSettle();
// Checkbox is now checked.
checkbox = tester.widget(find.byType(Checkbox));
expect(checkbox.value, isTrue);
});
}

View File

@@ -0,0 +1,27 @@
// 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/checkbox_list_tile/custom_labeled_checkbox.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Tapping LabeledCheckbox toggles the checkbox', (WidgetTester tester) async {
await tester.pumpWidget(
const example.LabeledCheckBoxApp(),
);
// Checkbox is initially unchecked.
Checkbox checkbox = tester.widget(find.byType(Checkbox));
expect(checkbox.value, isFalse);
// Tap the LabeledCheckBoxApp to toggle the checkbox.
await tester.tap(find.byType(example.LabeledCheckbox));
await tester.pumpAndSettle();
// Checkbox is now checked.
checkbox = tester.widget(find.byType(Checkbox));
expect(checkbox.value, isTrue);
});
}