import 'package:flutter/material.dart'; void main() => runApp(MyApp()); const CITY_NAMES = { '北京': ['东城区', '西城区', '朝阳区', '顺义区', '南城区', '北城区'], '上海': ['浦东区', '浦西区', '崇明区', '虹口区'], '广州': ['越秀区', '珠海', '荔湾', '番禺'], '合肥': ['瑶海', '蜀山', '庐阳', '经开'], }; class MyApp extends StatelessWidget { const MyApp({Key key}) : super(key: key); @override Widget build(BuildContext context) { final title = "列表展开收起"; return MaterialApp( title: title, home: Scaffold( appBar: AppBar( title: Text(title), ), body: Container( child: ListView( children: _buildList(), ), ), ), ); } List<Widget> _buildList() { List<Widget> widgets = []; CITY_NAMES.keys.forEach((key) { widgets.add(_item(key, CITY_NAMES[key])); }); return widgets; } Widget _item(String city, List<String> subCities) { return ExpansionTile( title: Text( city, style: TextStyle(color: Colors.black54, fontSize: 20), ), children: subCities.map((subCity) => _buildSubCity(subCity)).toList(), ); } Widget _buildSubCity(String subCity) { return FractionallySizedBox( widthFactor: 1, child: Container( height: 50, margin: EdgeInsets.only(bottom: 5), decoration: BoxDecoration(color: Colors.lightBlue), child: Text(subCity), ), ); } }
dart笔记4:flutter折叠菜单
dart笔记4:flutter折叠菜单