Skip to content

Commit 59c9d4e

Browse files
authored
Added ExpansionTileController (#123298)
Added ExpansionTileController
1 parent f7fb14e commit 59c9d4e

File tree

4 files changed

+434
-15
lines changed

4 files changed

+434
-15
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 [ExpansionTile] and [ExpansionTileController]
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() {
10+
runApp(const ExpansionTileControllerApp());
11+
}
12+
13+
class ExpansionTileControllerApp extends StatefulWidget {
14+
const ExpansionTileControllerApp({ super.key });
15+
16+
@override
17+
State<ExpansionTileControllerApp> createState() => _ExpansionTileControllerAppState();
18+
}
19+
20+
class _ExpansionTileControllerAppState extends State<ExpansionTileControllerApp> {
21+
final ExpansionTileController controller = ExpansionTileController();
22+
23+
@override
24+
Widget build(BuildContext context) {
25+
return MaterialApp(
26+
title: 'Flutter Code Sample for ExpansionTileController.',
27+
theme: ThemeData(useMaterial3: true),
28+
home: Scaffold(
29+
appBar: AppBar(title: const Text('ExpansionTileController Example')),
30+
body: Column(
31+
children: <Widget>[
32+
// A controller has been provided to the ExpansionTile because it's
33+
// going to be accessed from a component that is not within the
34+
// tile's BuildContext.
35+
ExpansionTile(
36+
controller: controller,
37+
title: const Text('ExpansionTile with explicit controller.'),
38+
children: <Widget>[
39+
Container(
40+
alignment: Alignment.center,
41+
padding: const EdgeInsets.all(24),
42+
child: const Text('ExpansionTile Contents'),
43+
),
44+
],
45+
),
46+
const SizedBox(height: 8),
47+
ElevatedButton(
48+
child: const Text('Expand/Collapse the Tile Above'),
49+
onPressed: () {
50+
if (controller.isExpanded) {
51+
controller.collapse();
52+
} else {
53+
controller.expand();
54+
}
55+
},
56+
),
57+
const SizedBox(height: 48),
58+
// A controller has not been provided to the ExpansionTile because
59+
// the automatically created one can be retrieved via the tile's BuildContext.
60+
ExpansionTile(
61+
title: const Text('ExpansionTile with implicit controller.'),
62+
children: <Widget>[
63+
Builder(
64+
builder: (BuildContext context) {
65+
return Container(
66+
padding: const EdgeInsets.all(24),
67+
alignment: Alignment.center,
68+
child: ElevatedButton(
69+
child: const Text('Collapse This Tile'),
70+
onPressed: () {
71+
return ExpansionTileController.of(context).collapse();
72+
},
73+
),
74+
);
75+
},
76+
),
77+
],
78+
),
79+
],
80+
),
81+
),
82+
);
83+
}
84+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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_api_samples/material/expansion_tile/expansion_tile.1.dart' as example;
6+
import 'package:flutter_test/flutter_test.dart';
7+
8+
void main() {
9+
testWidgets('Test the basics of ExpansionTileControllerApp', (WidgetTester tester) async {
10+
await tester.pumpWidget(
11+
const example.ExpansionTileControllerApp(),
12+
);
13+
14+
expect(find.text('ExpansionTile Contents'), findsNothing);
15+
expect(find.text('Collapse This Tile'), findsNothing);
16+
17+
await tester.tap(find.text('Expand/Collapse the Tile Above'));
18+
await tester.pumpAndSettle();
19+
expect(find.text('ExpansionTile Contents'), findsOneWidget);
20+
await tester.tap(find.text('Expand/Collapse the Tile Above'));
21+
await tester.pumpAndSettle();
22+
expect(find.text('ExpansionTile Contents'), findsNothing);
23+
24+
await tester.tap(find.text('ExpansionTile with implicit controller.'));
25+
await tester.pumpAndSettle();
26+
expect(find.text('Collapse This Tile'), findsOneWidget);
27+
await tester.tap(find.text('Collapse This Tile'));
28+
await tester.pumpAndSettle();
29+
expect(find.text('Collapse This Tile'), findsNothing);
30+
});
31+
}

0 commit comments

Comments
 (0)