我爱模板网 > 建站教程 > dart >  dart笔记4:flutter折叠菜单正文

dart笔记4:flutter折叠菜单

dart笔记4:flutter折叠菜单
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笔记3:下拉刷新,上拉加载 下一篇:dart笔记5:重构override+画圆
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
选择头像:
最新评论

猜你喜欢