Flutter:ExpansionTile 示例
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ExpansionTileExample(),
);
}
}
class ExpansionTileExample extends StatefulWidget {
@override
_ExpansionTileExampleState createState() => _ExpansionTileExampleState();
}
class _ExpansionTileExampleState extends State<ExpansionTileExample> {
bool _isExpanded = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ExpansionTile(
title: Text('Expansion Tile Example'),
children: <Widget>[
ListTile(
title: Text('Item 1'),
),
ListTile(
title: Text('Item 2'),
),
ListTile(
title: Text('Item 3'),
),
],
onExpansionChanged: (bool isExpanded) {
setState(() {
_isExpanded = isExpanded;
});
},
),
),
);
}
}
这段代码创建了一个简单的Flutter应用,其中包含了一个ExpansionTile
的示例。当用户点击标题时,ExpansionTile
会展开或折叠,显示或隐藏子列表项。通过使用onExpansionChanged
回调,我们可以跟踪ExpansionTile的展开状态,并在状态发生变化时更新UI。
评论已关闭