flutter - 在 Flutter 中单击另一个扩展 Tile 后如何折叠已经打开的扩展 Tiles?
问题描述
我在我的应用程序中使用了多个扩展图块。单击另一个后,我需要关闭已经打开的磁贴。我尝试使用默认具有该功能的扩展面板。但我需要重新设计扩展瓷砖,所以我使用的是扩展瓷砖。如何在扩展图块中实现该功能
解决方案
GitHub中的这个答案应该可以解决问题。
这是示例:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ExpansionTile Collapse',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'ExpansionTile Collapse'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// selected's value = 0. For default first item is open.
int selected = 0; //attention
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('ExpansionTile Collapse',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
),
body: Container(
color: Colors.white,
child: SingleChildScrollView(
child: Column(children: [
ListView.builder(
key: Key('builder ${selected.toString()}'), //attention
padding: EdgeInsets.only(left: 13.0, right: 13.0, bottom: 25.0),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 5,
itemBuilder: (context, index) {
return Column(children: <Widget>[
Divider(
height: 17.0,
color: Colors.white,
),
ExpansionTile(
key: Key(index.toString()), //attention
initiallyExpanded: index == selected, //attention
leading: Icon(
Icons.person,
size: 50.0,
color: Colors.black,
),
title: Text('ExpansionTile ${index}',
style: TextStyle(
color: Color(0xFF09216B),
fontSize: 17.0,
fontWeight: FontWeight.bold)),
subtitle: Text(
'Software Engineer',
style: TextStyle(
color: Colors.black,
fontSize: 13.0,
fontWeight: FontWeight.bold),
),
children: <Widget>[
Padding(
padding: EdgeInsets.all(25.0),
child: Text(
'DETAİL ${index} \n' + 'Expanded',
),
)
],
onExpansionChanged: ((newState) {
if (newState)
setState(() {
Duration(seconds: 20000);
selected = index;
});
else
setState(() {
selected = -1;
});
}),
),
]);
},
)
]),
),
),
);
}
}
实际输出:
推荐阅读
- java - 在加载 swagger-ui.html 页面时,向 host:port/ 和 host:port/csfr 发出请求
- java - Hibernate 异常没有被捕获
- linux - 尽管已经安装了依赖项,但从文件夹中安装 npm 会为 wikimedia 包提供 git 错误(共享主机)
- android - Android 项目在发布版本中生成过多的 DEX 文件
- c# - 异步读取 Excel 工作表
- java - 未能执行目标 org.springframework
- ocaml - 有没有一种简单的方法可以将元组列表转换为具有单个元素的列表,反之亦然?
- r - 是否有 R 代码仅删除带有连续空白的行
- python - 拖动时按钮强制鼠标进入窗口的左上角
- python - 如何让辅助脚本中的函数不需要带参数?