flutter DataTable标题如何在顶部标题上?

发布于 2025-02-11 21:35:41 字数 1500 浏览 2 评论 0原文

我正在使用dataTable制作表,我想将标题行做像这个。只有headingRowColor参数,所以我真的不知道。
这是我到目前为止写的:

DataTable(
      columnSpacing: 35,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20),  // this only make bottom rounded and not top
        color: const Color(0xE61B1D1C),
      ),
      headingRowColor: MaterialStateProperty.all<Color>(Color(0xE6292D2C)),
      columns: [
        DataColumn(label: Text("Referral Code", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)), softWrap: true, textAlign: TextAlign.center,)),
        DataColumn(label: Text("Share (%)\nYou-Friend", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)), softWrap: true, textAlign: TextAlign.center,)),
        DataColumn(label: Text("Friends", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)))),
        DataColumn(label: Text("Vol. (USD)", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)))),
      ],
      rows: [
        DataRow(cells: [DataCell(Text('ADHJKGO')), DataCell(Text('100-0')), DataCell(Text('0')), DataCell(Text('100'))]),
        DataRow(cells: [DataCell(Text('CXMDJEO')), DataCell(Text('50-50')), DataCell(Text('0')), DataCell(Text('2000'))]),
      ],
    );

I'm making a table using DataTable and I would want to make the heading row like this. There's only headingRowColor parameter so I really have no idea.
Here's what I've written so far:

DataTable(
      columnSpacing: 35,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(20),  // this only make bottom rounded and not top
        color: const Color(0xE61B1D1C),
      ),
      headingRowColor: MaterialStateProperty.all<Color>(Color(0xE6292D2C)),
      columns: [
        DataColumn(label: Text("Referral Code", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)), softWrap: true, textAlign: TextAlign.center,)),
        DataColumn(label: Text("Share (%)\nYou-Friend", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)), softWrap: true, textAlign: TextAlign.center,)),
        DataColumn(label: Text("Friends", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)))),
        DataColumn(label: Text("Vol. (USD)", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)))),
      ],
      rows: [
        DataRow(cells: [DataCell(Text('ADHJKGO')), DataCell(Text('100-0')), DataCell(Text('0')), DataCell(Text('100'))]),
        DataRow(cells: [DataCell(Text('CXMDJEO')), DataCell(Text('50-50')), DataCell(Text('0')), DataCell(Text('2000'))]),
      ],
    );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗷兮 2025-02-18 21:35:41

最糟糕的解决方案是使用堆栈小部件,添加圆形容器并设置headingRowColor透明。

我设置headingRowheight 60和圆形容器高度为40,所以我需要将圆形容器 margin(60-40)/2 = 10设置为使其成为垂直中心。

@override
  Widget build(BuildContext context) {
    TextStyle cellStyle = TextStyle(color: Colors.white);
    TextStyle rowHeaderStyle = TextStyle(color: Colors.greenAccent);
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Stack(
        children: [
          // Add rounded container
          Container(
            width: MediaQuery.of(context).size.width,
            // set height 40
            height: 40,
            margin: EdgeInsets.symmetric(vertical: 10,horizontal: 14),
            decoration: BoxDecoration(
                color: Colors.blueGrey,
                borderRadius: BorderRadius.circular(20)
            ),
          ),
          Container(
            width: MediaQuery.of(context).size.width,
            child: DataTable(
              columnSpacing: 35,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),  // this only make bottom rounded and not top
                color: const Color(0xE61B1D1C),
              ),
              // set heading row height 60
              headingRowHeight: 60,
              headingRowColor: MaterialStateProperty.all<Color>(Colors.transparent),
              columns: [
                DataColumn(label: Text("Referral Code", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)), softWrap: true, textAlign: TextAlign.center,)),
                DataColumn(label: Text("Share (%)\nYou-Friend", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)), softWrap: true, textAlign: TextAlign.center,)),
                DataColumn(label: Text("Friends", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)))),
                DataColumn(label: Text("Vol. (USD)", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)))),
              ],
              rows: [
                DataRow(cells: [DataCell(Text('ADHJKGO',style: rowHeaderStyle,)), DataCell(Text('100-0',style: cellStyle,)), DataCell(Text('0',style: cellStyle)), DataCell(Text('100',style: cellStyle))]),
                DataRow(cells: [DataCell(Text('CXMDJEO',style: rowHeaderStyle)), DataCell(Text('50-50',style: cellStyle)), DataCell(Text('0',style: cellStyle)), DataCell(Text('2000',style: cellStyle))]),
              ],
            ),
          ),
        ],
      ),
    );
}

中构建它

我在桌面

The worst solution is to use stack widget, add rounded Container and set headingRowColor transparent.

I set headingRowHeight 60 and rounded Container height is 40, so I need to set rounded Container margin (60-40)/2 = 10 to make it vertical center.

@override
  Widget build(BuildContext context) {
    TextStyle cellStyle = TextStyle(color: Colors.white);
    TextStyle rowHeaderStyle = TextStyle(color: Colors.greenAccent);
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Stack(
        children: [
          // Add rounded container
          Container(
            width: MediaQuery.of(context).size.width,
            // set height 40
            height: 40,
            margin: EdgeInsets.symmetric(vertical: 10,horizontal: 14),
            decoration: BoxDecoration(
                color: Colors.blueGrey,
                borderRadius: BorderRadius.circular(20)
            ),
          ),
          Container(
            width: MediaQuery.of(context).size.width,
            child: DataTable(
              columnSpacing: 35,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),  // this only make bottom rounded and not top
                color: const Color(0xE61B1D1C),
              ),
              // set heading row height 60
              headingRowHeight: 60,
              headingRowColor: MaterialStateProperty.all<Color>(Colors.transparent),
              columns: [
                DataColumn(label: Text("Referral Code", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)), softWrap: true, textAlign: TextAlign.center,)),
                DataColumn(label: Text("Share (%)\nYou-Friend", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)), softWrap: true, textAlign: TextAlign.center,)),
                DataColumn(label: Text("Friends", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)))),
                DataColumn(label: Text("Vol. (USD)", style: TextStyle(fontWeight: FontWeight.w400, fontSize: 12, color: Color(0xF2979797)))),
              ],
              rows: [
                DataRow(cells: [DataCell(Text('ADHJKGO',style: rowHeaderStyle,)), DataCell(Text('100-0',style: cellStyle,)), DataCell(Text('0',style: cellStyle)), DataCell(Text('100',style: cellStyle))]),
                DataRow(cells: [DataCell(Text('CXMDJEO',style: rowHeaderStyle)), DataCell(Text('50-50',style: cellStyle)), DataCell(Text('0',style: cellStyle)), DataCell(Text('2000',style: cellStyle))]),
              ],
            ),
          ),
        ],
      ),
    );
}

I build it in desktop

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文