gpt4 book ai didi

flutter - 展开新的ExpansionTile时折叠ExpansionTile

转载 作者:行者123 更新时间:2023-12-03 03:57:23 30 4
gpt4 key购买 nike

我试图解决这个问题几个小时,但是没有运气。我在这里看到过类似的问题,但它们似乎都不适合我的情况。这是简化的代码

static final List<String> _listViewData = [
'Option1',
'Option2',
"Option3",
"Option4",
"Option5",
"Option6",
"Option7"
];


body: Container(
height: 450,
child: ListView.builder(
itemCount: _listViewData.length,
itemBuilder: (context, index) => ExpansionTile(
subtitle: Text('description'),
children: <Widget>[Text('expanded')],
title: Text(_listViewData[index]),
),
),
),

到目前为止,当我从列表中选择Option1时,Option1将展开。然后,当我选择Option2时,Option2将展开,但Option1保持展开状态。那么,当新的瓷砖展开时,如何折叠展开的瓷砖?

最佳答案

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('Faruk AYDIN ${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' + 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using "Content here, content here", making it look like readable English.',)
)
],
onExpansionChanged: ((newState){
if(newState)
setState(() {
Duration(seconds: 20000);
selected = index;
});
else setState(() {
selected = -1;
});
})
),

]
);
},
)
]
),
)
)
);
}
}

关于flutter - 展开新的ExpansionTile时折叠ExpansionTile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59924241/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com