Skip to content

Instantly share code, notes, and snippets.

@wisnuwiry
Created April 5, 2021 01:24
Show Gist options
  • Save wisnuwiry/c7fda0c53a705fc48a1e44116c9ffbc1 to your computer and use it in GitHub Desktop.
Save wisnuwiry/c7fda0c53a705fc48a1e44116c9ffbc1 to your computer and use it in GitHub Desktop.
Flutter Auto Collapse ExpansionTile
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key? key, required this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _selectedItem = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body:
ListView.builder(
key: Key('selected $_selectedItem'),
itemBuilder: (context, index) {
return ExpansionTile(
key: Key(index.toString()), //attention
initiallyExpanded: index == _selectedItem, //attention
title: Text('Item $index'),
children: <Widget>[
Padding(
padding: EdgeInsets.all(25.0),
child: Text(
'DETAİL $index \n' +
'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
))
],
onExpansionChanged: (isOpen) {
if (isOpen) {
setState(() {
_selectedItem = index;
});
}
},
);
},
itemCount: 20,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment