You need to enable JavaScript to run this app. Flutter中如何实现DataTable组件拉伸至最大宽度全宽显示
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何实现DataTable组件拉伸至最大宽度全宽显示

问题说明
  • 需要让DataTable组件拉伸至容器最大可用宽度,当前展示效果未达到预期
  • 曾尝试使用ConstrainedBox设置宽度约束,操作后约束逻辑未生效,还抛出了运行时错误

表格功能实现代码

SingleChildScrollView _dataBody() {
    return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: DataTable(
            headingRowColor:
                MaterialStateColor.resolveWith((states) => Color(0xFF2F80ED)),
            headingTextStyle: TextStyle(color: Colors.white),
            columns: [
              DataColumn(label: Text('ID')),
              DataColumn(label: Text('Name')),
              DataColumn(label: Text('Surname')),
              DataColumn(label: Text('Birth date')),
              DataColumn(label: Text('Algebra')),
              DataColumn(label: Text('Chemistry')),
              DataColumn(label: Text('Programing'))
            ],
            rows: _students
                .map((student) => DataRow(cells: [
                      DataCell(Text(student.id)),
                      DataCell(Text(student.firstName.toString())),
                      DataCell(Text(student.lastName.toString())),
                      DataCell(Text(student.birthDate.toString())),
                      DataCell(Text(student.algebra.toString())),
                      DataCell(Text(student.chemistry.toString())),
                      DataCell(Text(student.programing.toString()))
                    ]))
                .toList()),
      ),
    );
  }

表格组件的挂载页面布局代码

Expanded(
          child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            SizedBox(
              height: 110,
              child: Container(),
            ),
            Expanded(
              child: _dataBody(),
            )
          ],
        ))

内容的提问来源于stack exchange,提问作者Zimme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.29 11:36:05