【问题标题】:ExpansionTile and ListTileExpansionTile 和 ListTile
【发布时间】:2022-11-26 19:26:32
【问题描述】:

嗨,这个扩展图块列表的代码示例:

我的列表来自 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? 我该如何做这项工作?

感谢你们对我的帮助

【问题讨论】:

  • 您想在 ExpansionTile 子项中显示 ListView 列表吗?
  • 是的,我想这样做

标签: flutter flutter-dependencies flutter-web


【解决方案1】:

首先,您需要更改您的MonthModel 类并将description 更改为字符串列表,然后将您的_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),
                  ),
                )),
      ),
    );
  }

【讨论】:

    猜你喜欢
    • 2021-05-15
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 2018-12-24
    • 2017-12-01
    • 2021-06-25
    相关资源
    最近更新 更多