Flutter:ExpansionTile折叠展开层级菜单

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ExcerciseListPage extends StatefulWidget {
  ExcerciseListPage({Key? key}) : super(key: key);

  @override
  _ExcerciseListPageState createState() => new _ExcerciseListPageState();
}

const CITY_NAMES = {
  '北京': ['东城区', '西城区', '朝阳区', '顺义区', '南城区', '北城区'],
  '上海': ['浦东区', '浦西区', '崇明区', '虹口区'],
  '广州': ['越秀区', '珠海', '荔湾', '番禺'],
  '合肥': ['瑶海', '蜀山', '庐阳', '经开'],
};

class _ExcerciseListPageState extends State
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    final title = '列表展开收起';

    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Container(
        child: ListView(
          children: _buildList(),
        ),
      ),
    );
  }

  List _buildList() {
    List widgets = [];

    CITY_NAMES.keys.forEach((key) {
      widgets.add(_item(key, CITY_NAMES[key]!));
    });

    return widgets;
  }

  Widget _item(String city, List 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),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}

你可能感兴趣的:(Flutter:ExpansionTile折叠展开层级菜单)