flutter 展开平铺和列表平铺

vsaztqbk  于 2022-11-30  发布在  Flutter
关注(0)|答案(1)|浏览(171)

在此扩展平铺列表代码示例中:
我的列表来自于API,每个ExpansionTile都包含一些ListTile。如何实现ListTile像ExpansionTile一样?
使用列表视图显示ListTile中的所有项目。

import 'package:flutter/material.dart';
import 'package:flutter_expansion_tile_demo/Constants/Constants.dart';
import 'package:flutter_expansion_tile_demo/model/month_model.dart';

class ExpansionTileDemo extends StatefulWidget {
  @override
  _ExpansionTileDemoState createState() => _ExpansionTileDemoState();
}

class _ExpansionTileDemoState extends State<ExpansionTileDemo> {
  List<MonthModel> monthModel;

  @override
  void initState() {
    monthModel = Constants.getMonthModel();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Expansion Tile Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 0.0),
        child: ListView.builder(
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          physics: BouncingScrollPhysics(),
          itemCount: monthModel.length,
          itemBuilder: (BuildContext context, int index) {
            return _buildPlayerModelList(monthModel[index]);
          },
        ),
      ),
    );
  }

  Widget _buildPlayerModelList(MonthModel items) {
    return Card(
      child: ExpansionTile(
        title: Text(
          items.playerName,
          style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w500),
        ),
        children: <Widget>[
          ListTile(
            title: Text(
              items.description,
              style: TextStyle(fontWeight: FontWeight.w700),
            ),
          )
        ],
      ),
    );
  }
}

这个图像输出代码,但我想更多的ListTile?我怎么能做这项工作?

谢谢你帮我

mf98qq94

mf98qq941#

首先你需要改变你的MonthModel类,使description成为List of string,然后把你的_buildPlayerModelList改为:

Widget _buildPlayerModelList(MonthModel items) {
    return Card(
      child: ExpansionTile(
        title: Text(
          items.playerName,
          style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.w500),
        ),
        children: List<Widget>.generate(
            items.descriptions.length,
            (index) => ListTile(
                  title: Text(
                    items.descriptions[index],
                    style: TextStyle(fontWeight: FontWeight.w700),
                  ),
                )),
      ),
    );
  }

相关问题