Skip to content

Commit 42b20cf

Browse files
Added ListTile.titleAlignment, ListTileThemeData.titleAlignment (#119872)
* added ListTile.textAlignment * changed titlesHeight to titleHeight * fixed a typo * Add tests and example * Update tests * update example test --------- Co-authored-by: tahatesser <[email protected]>
1 parent 0521c60 commit 42b20cf

File tree

6 files changed

+831
-33
lines changed

6 files changed

+831
-33
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
// Flutter code sample for [ListTile].
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() => runApp(const ListTileApp());
10+
11+
class ListTileApp extends StatelessWidget {
12+
const ListTileApp({super.key});
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return MaterialApp(
17+
theme: ThemeData(useMaterial3: true),
18+
home: const ListTileExample(),
19+
);
20+
}
21+
}
22+
23+
class ListTileExample extends StatefulWidget {
24+
const ListTileExample({super.key});
25+
26+
@override
27+
State<ListTileExample> createState() => _ListTileExampleState();
28+
}
29+
30+
class _ListTileExampleState extends State<ListTileExample> {
31+
ListTileTitleAlignment? titleAlignment;
32+
33+
@override
34+
Widget build(BuildContext context) {
35+
return Scaffold(
36+
appBar: AppBar(title: const Text('ListTile.titleAlignment Sample')),
37+
body: Column(
38+
children: <Widget>[
39+
const Divider(),
40+
ListTile(
41+
titleAlignment: titleAlignment,
42+
leading: Checkbox(
43+
value: true,
44+
onChanged:(bool? value) { },
45+
),
46+
title: const Text('Headline Text'),
47+
subtitle: const Text('Tapping on the trailing widget will show a menu that allows you to change the title alignment. The title alignment is set to threeLine by default if `ThemeData.useMaterial3` is true. Otherwise, defaults to titleHeight.'),
48+
trailing: PopupMenuButton<ListTileTitleAlignment>(
49+
onSelected: (ListTileTitleAlignment? value) {
50+
setState(() {
51+
titleAlignment = value;
52+
});
53+
},
54+
itemBuilder: (BuildContext context) => <PopupMenuEntry<ListTileTitleAlignment>>[
55+
const PopupMenuItem<ListTileTitleAlignment>(
56+
value: ListTileTitleAlignment.threeLine,
57+
child: Text('threeLine'),
58+
),
59+
const PopupMenuItem<ListTileTitleAlignment>(
60+
value: ListTileTitleAlignment.titleHeight,
61+
child: Text('titleHeight'),
62+
),
63+
const PopupMenuItem<ListTileTitleAlignment>(
64+
value: ListTileTitleAlignment.top,
65+
child: Text('top'),
66+
),
67+
const PopupMenuItem<ListTileTitleAlignment>(
68+
value: ListTileTitleAlignment.center,
69+
child: Text('center'),
70+
),
71+
const PopupMenuItem<ListTileTitleAlignment>(
72+
value: ListTileTitleAlignment.bottom,
73+
child: Text('bottom'),
74+
),
75+
],
76+
),
77+
),
78+
const Divider(),
79+
],
80+
),
81+
);
82+
}
83+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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_api_samples/material/list_tile/list_tile.4.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Can choose different title aligments from popup menu', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.ListTileApp(),
13+
);
14+
15+
Offset titleOffset = tester.getTopLeft(find.text('Headline Text'));
16+
Offset leadingOffset = tester.getTopLeft(find.byType(Checkbox));
17+
Offset trailingOffset = tester.getTopRight(find.byIcon(Icons.adaptive.more));
18+
19+
// The default title alignment is threeLine.
20+
expect(leadingOffset.dy - titleOffset.dy, 48.0);
21+
expect(trailingOffset.dy - titleOffset.dy, 60.0);
22+
23+
await tester.tap(find.byIcon(Icons.adaptive.more));
24+
await tester.pumpAndSettle();
25+
26+
// Change the title alignment to titleHeight.
27+
await tester.tap(find.text('titleHeight'));
28+
await tester.pumpAndSettle();
29+
30+
titleOffset = tester.getTopLeft(find.text('Headline Text'));
31+
leadingOffset = tester.getTopLeft(find.byType(Checkbox));
32+
trailingOffset = tester.getTopRight(find.byIcon(Icons.adaptive.more));
33+
34+
expect(leadingOffset.dy - titleOffset.dy, 8.0);
35+
expect(trailingOffset.dy - titleOffset.dy, 20.0);
36+
37+
await tester.tap(find.byIcon(Icons.adaptive.more));
38+
await tester.pumpAndSettle();
39+
40+
// Change the title alignment to bottom.
41+
await tester.tap(find.text('bottom'));
42+
await tester.pumpAndSettle();
43+
44+
titleOffset = tester.getTopLeft(find.text('Headline Text'));
45+
leadingOffset = tester.getTopLeft(find.byType(Checkbox));
46+
trailingOffset = tester.getTopRight(find.byIcon(Icons.adaptive.more));
47+
48+
expect(leadingOffset.dy - titleOffset.dy, 96.0);
49+
expect(trailingOffset.dy - titleOffset.dy, 108.0);
50+
});
51+
}

0 commit comments

Comments
 (0)