Skip to content

Commit 19ba9aa

Browse files
authored
[material_ui] Migrate ListTile and SwitchListTile API doc snippets to {@example} (batch 10) (#13097)
This PR addresses batch 10 of the migration detailed in flutter/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 f2ecb6a commit 19ba9aa

14 files changed

Lines changed: 362 additions & 97 deletions
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 [ListTile].
8+
9+
class ListTileExample extends StatelessWidget {
10+
const ListTileExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return
15+
// #region body
16+
const ColoredBox(
17+
color: Colors.green,
18+
child: Material(
19+
child: ListTile(
20+
title: Text('ListTile with red background'),
21+
tileColor: Colors.red,
22+
),
23+
),
24+
)
25+
// #endregion body
26+
;
27+
}
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 [ListTile].
8+
9+
class ListTileExample extends StatelessWidget {
10+
const ListTileExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return
15+
// #region body
16+
const Row(
17+
children: <Widget>[
18+
Expanded(
19+
child: ListTile(
20+
leading: FlutterLogo(),
21+
title: Text('These ListTiles are expanded '),
22+
),
23+
),
24+
Expanded(
25+
child: ListTile(
26+
trailing: FlutterLogo(),
27+
title: Text('to fill the available space.'),
28+
),
29+
),
30+
],
31+
)
32+
// #endregion body
33+
;
34+
}
35+
}
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 [ListTile].
8+
9+
class ListTileExample extends StatelessWidget {
10+
const ListTileExample({super.key, this.act = 1});
11+
12+
final int act;
13+
14+
int get _act => act;
15+
16+
@override
17+
Widget build(BuildContext context) {
18+
return
19+
// #region body
20+
ListTile(
21+
leading: const Icon(Icons.flight_land),
22+
title: const Text("Trix's airplane"),
23+
subtitle: _act != 2
24+
? const Text('The airplane is only in Act II.')
25+
: null,
26+
enabled: _act == 2,
27+
onTap: () {
28+
/* react to the tile being tapped */
29+
},
30+
)
31+
// #endregion body
32+
;
33+
}
34+
}
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 [ListTile].
8+
9+
class ListTileExample extends StatelessWidget {
10+
const ListTileExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return
15+
// #region body
16+
ListTile(
17+
leading: GestureDetector(
18+
behavior: HitTestBehavior.translucent,
19+
onTap: () {},
20+
child: Container(
21+
width: 48,
22+
height: 48,
23+
padding: const EdgeInsets.symmetric(vertical: 4.0),
24+
alignment: Alignment.center,
25+
child: const CircleAvatar(),
26+
),
27+
),
28+
title: const Text('title'),
29+
dense: false,
30+
)
31+
// #endregion body
32+
;
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 [SwitchListTile].
8+
9+
class SwitchListTileExample extends StatelessWidget {
10+
const SwitchListTileExample({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return
15+
// #region body
16+
ColoredBox(
17+
color: Colors.green,
18+
child: Material(
19+
child: SwitchListTile(
20+
tileColor: Colors.red,
21+
title: const Text('SwitchListTile with red background'),
22+
value: true,
23+
onChanged: (bool value) {},
24+
),
25+
),
26+
)
27+
// #endregion body
28+
;
29+
}
30+
}
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 [SwitchListTile.onChanged].
8+
9+
class SwitchListTileExample extends StatefulWidget {
10+
const SwitchListTileExample({super.key});
11+
12+
@override
13+
State<SwitchListTileExample> createState() => _SwitchListTileExampleState();
14+
}
15+
16+
class _SwitchListTileExampleState extends State<SwitchListTileExample> {
17+
bool _isSelected = false;
18+
19+
@override
20+
Widget build(BuildContext context) {
21+
return
22+
// #region body
23+
SwitchListTile(
24+
value: _isSelected,
25+
onChanged: (bool newValue) {
26+
setState(() {
27+
_isSelected = newValue;
28+
});
29+
},
30+
title: const Text('Selection'),
31+
)
32+
// #endregion body
33+
;
34+
}
35+
}
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:flutter_test/flutter_test.dart';
6+
import 'package:material_ui/material_ui.dart';
7+
import 'package:material_ui_examples/list_tile/list_tile.snippet.0.dart'
8+
as example;
9+
10+
void main() {
11+
testWidgets('ListTile renders with red tileColor inside Material', (
12+
WidgetTester tester,
13+
) async {
14+
await tester.pumpWidget(
15+
const MaterialApp(home: Scaffold(body: example.ListTileExample())),
16+
);
17+
18+
expect(find.text('ListTile with red background'), findsOneWidget);
19+
expect(
20+
find.byType(example.ListTileExample),
21+
paints
22+
..rect(color: Colors.green.shade500)
23+
..rect(color: Colors.red.shade500),
24+
);
25+
});
26+
}
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/list_tile/list_tile.snippet.1.dart'
8+
as example;
9+
10+
void main() {
11+
testWidgets('Row renders two Expanded ListTiles with FlutterLogo', (
12+
WidgetTester tester,
13+
) async {
14+
await tester.pumpWidget(
15+
const MaterialApp(home: Scaffold(body: example.ListTileExample())),
16+
);
17+
18+
expect(find.byType(ListTile), findsNWidgets(2));
19+
expect(find.byType(FlutterLogo), findsNWidgets(2));
20+
expect(find.text('These ListTiles are expanded '), findsOneWidget);
21+
expect(find.text('to fill the available space.'), findsOneWidget);
22+
});
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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/list_tile/list_tile.snippet.2.dart'
8+
as example;
9+
10+
void main() {
11+
testWidgets('ListTile enabled and subtitle depend on act value', (
12+
WidgetTester tester,
13+
) async {
14+
await tester.pumpWidget(
15+
const MaterialApp(home: Scaffold(body: example.ListTileExample(act: 1))),
16+
);
17+
18+
expect(find.text("Trix's airplane"), findsOneWidget);
19+
expect(find.text('The airplane is only in Act II.'), findsOneWidget);
20+
expect(tester.widget<ListTile>(find.byType(ListTile)).enabled, isFalse);
21+
22+
await tester.pumpWidget(
23+
const MaterialApp(home: Scaffold(body: example.ListTileExample(act: 2))),
24+
);
25+
26+
expect(find.text("Trix's airplane"), findsOneWidget);
27+
expect(find.text('The airplane is only in Act II.'), findsNothing);
28+
expect(tester.widget<ListTile>(find.byType(ListTile)).enabled, isTrue);
29+
});
30+
}
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/list_tile/list_tile.snippet.3.dart'
8+
as example;
9+
10+
void main() {
11+
testWidgets('ListTile renders 48x48 leading GestureDetector with avatar', (
12+
WidgetTester tester,
13+
) async {
14+
await tester.pumpWidget(
15+
const MaterialApp(home: Scaffold(body: example.ListTileExample())),
16+
);
17+
18+
expect(find.text('title'), findsOneWidget);
19+
expect(find.byType(CircleAvatar), findsOneWidget);
20+
final ListTile tile = tester.widget<ListTile>(find.byType(ListTile));
21+
expect(tile.dense, isFalse);
22+
expect(tester.getSize(find.byWidget(tile.leading!)), const Size(48, 48));
23+
});
24+
}

0 commit comments

Comments
 (0)