Flutter - 数据表 - 如何设置标题/第一行的背景颜色?

问题描述 投票:0回答:2

我一直在查看 API 文档,但似乎无法设置标题行的背景颜色。有什么建议吗? https://api.flutter.dev/flutter/material/DataTable-class.html

flutter
2个回答
7
投票

我已经简单地做了这个,这对我有用

headingRowColor: MaterialStateColor.resolveWith(
    (states) {
      return HexColor('#222D65');
    },
  ),

2
投票

您可以使用

Stack

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IntrinsicWidth(
          child: Stack(
            children: [
              Container(
                height: 100,
                color: Colors.yellow,
              ),
              DataTable(
                headingRowHeight: 100,
                dataRowHeight: 100,
                columns: [
                  DataColumn(
                    label: Text("Col1"),
                  ),
                  DataColumn(
                    label: Text("Col2"),
                  ),
                ],
                rows: List.generate(2, (index) {
                  return DataRow(cells: <DataCell>[
                    DataCell(
                      Text(
                        "row $index content1",
                      ),
                    ),
                    DataCell(
                      Text(
                        "row $index content2",
                      ),
                    ),
                  ]);
                }),
              ),
            ],
          ),
        ),
      ),
    );
  }

设置为容器高度的

100
是标题行的高度。
IntrinsicWidth
用于将
Stack
的宽度设置为其子级的宽度。

结果:

res

© www.soinside.com 2019 - 2024. All rights reserved.