你能改变 Flutter 中 ExpansionTile 的高度吗?

Can you change the height of an ExpansionTile in Flutter?

ExpansionTile inherits from the ListTile,它有一个固定的高度。没有图块高度的输入参数。

我尝试将 ExpansionTile 包装在具有硬编码高度的 Container 小部件中,但这会导致子小部件仅占用硬编码高度内的 space。目前,由于 title 小部件中的内容很大,我有一个 "Column overflowed by 23 pixels" 消息。

有什么方法可以改变 Expansion Tile 的高度吗?或者是否有另一个我可以使用的具有 expansion/accordion 功能的小部件?

我可能会复制 ExpansionTile 并制作您自己的版本。使用 ContainerSizedBox 调整 ListTile 的大小很容易,但 ExpansionTile 是一个 material 小部件,它看起来不像是用您的用例构建的记住了。

您可能想试试这个包:configurable_expansion_tile

示例应用程序:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Configurable Expansion Tile Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Configurable Expansion Tile Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ConfigurableExpansionTile(
              borderColorStart: Colors.blue,
              borderColorEnd: Colors.orange,
              animatedWidgetFollowingHeader: const Icon(
                Icons.expand_more,
                color: const Color(0xFF707070),
              ),
              headerExpanded:
                  Flexible(child: Center(child: Text("A Header Changed"))),
              header: Container(
                  color: Colors.transparent,
                  child: Center(child: Text("A Header"))),
              headerBackgroundColorStart: Colors.grey,
              expandedBackgroundColor: Colors.amber,
              headerBackgroundColorEnd: Colors.teal,
              children: [
                Row(
                  children: <Widget>[Text("CHILD 1")],
                ),
                Row(
                  children: <Widget>[Text("CHILD 2")],
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}

它应该能让您得到想要的结果。

希望对您有所帮助,

谢谢

这是一个解决方法:您可以使用 ExpansionTile 小部件的 title 来呈现整个内容,而不是使用前导或尾随。您可以向标题小部件添加填充以增加 ExpansionTile 的高度。

将 'ExpansionTile' 包裹在 'Container' 内并设置边距

Container(
                  margin: EdgeInsets.only(right: .5.sw),
                  decoration: BoxDecoration(
                      color: AppColor.profileBoarderColor,
                      borderRadius: BorderRadius.circular(10),
                      border: Border.all(color: AppColor.profileBoarderColor)
                  ),
                  child: ExpansionTile(
                    title: AppTextView(text: 'Seeson 01',),
                    children: List.generate(10, (index) {
                      return SizedBox(
                        width: 200,
                          child: ListTile(title: AppTextView(text: 'Seeson 01',),));
                    }
                    ),
                  )