【问题标题】:How do I change the column widths on a PaginatedDataTable?如何更改 PaginatedDataTable 上的列宽?
【发布时间】:2018-06-22 04:30:55
【问题描述】:

现在列之间有很大的空间,即使我有一个 4 个字符的标题和一个 1 到 3 位的排名值,就好像我可以在每列中放置 4 倍。

我尝试使用较小的字体,但分隔保持不变。

这是我用来设置 PaginatedDataTable 的内容。我在 Table/Row/Cell/etc 中找不到任何似乎会影响宽度的参数。这个空间似乎是自动的,但完全没有必要。谢谢。

Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(title: const Text('Tour Rankings')),
        body:
        new ListView(
            padding: const EdgeInsets.all(5.0),
            children: <Widget>[
              new PaginatedDataTable(
                  header: const Text('Current Rankings'),
                  rowsPerPage: _rowsPerPage,
                  onRowsPerPageChanged: (int value) { setState(() { _rowsPerPage = value; }); },
                  sortColumnIndex: _sortColumnIndex,
                  sortAscending: _sortAscending,
                  columns: <DataColumn>[
                    new DataColumn(
                        label: new Text('Rank', style: new TextStyle(fontSize: 8.0)),
                        numeric: true,
                        onSort: (int columnIndex, bool ascending) => _sort<num>((Player d) => d.rank, columnIndex, ascending)
                    ),
                    new DataColumn(
                        label: new Text('Prev', style: new TextStyle(fontSize: 8.0)),
                        numeric: true,
                        onSort: (int columnIndex, bool ascending) => _sort<num>((Player d) => d.prevRank, columnIndex, ascending)
                    ),...

【问题讨论】:

  • 不清楚。可以提供截图+例子吗?
  • 我最终创建了自己的 PaginatedDataTable 和 DataTable 实现,因此我可以调整嵌入在 Data Table 中的固定填充值并进行其他一些调整。
  • 你能分享一下你是怎么解决的吗?

标签: flutter


【解决方案1】:

flutter中有一个分页数据表的属性为columnSpacing。 根据 Material Design 规范,此值默认为 56.0。

您可以根据需要更改该值以更改列内容之间的水平边距。

              PaginatedDataTable(
                showCheckboxColumn: false,
                columnSpacing: 10.0,
                header: Center(
                  child: Text("Statement"),
                ),
                columns: [
                  DataColumn(label: Text("Date")),
                  DataColumn(label: Text("Particular")),
                  DataColumn(label: Text("Debit")),
                  DataColumn(label: Text("Credit")),
                  DataColumn(label: Text("Detail")),
                ],
                source: YourDataSource(),
              ),

它应该看起来像这样:

【讨论】:

  • 我不太了解the docs 中的定义---这是否充当伪min-width 属性?我们是否可以只设置columnSpacing: 1.0,然后为每个DataColumn &gt; label 添加类似盒子的小部件,并单独设置每个DataColumn 的宽度(例如label: SizedBox(width: 24)BoxConstraintsContainer)?尽管它似乎有效,但这是否跨设备/平台可靠?副作用?
  • 完美,这就是答案
【解决方案2】:

this diff之后可以设置horizontalMargincolumnSpacing来实现自定义列宽。

  /// The horizontal margin between the edges of the table and the content
  /// in the first and last cells of each row.
  ///
  /// When a checkbox is displayed, it is also the margin between the checkbox
  /// the content in the first data column.
  ///
  /// This value defaults to 24.0 to adhere to the Material Design specifications.
  final double horizontalMargin;

  /// The horizontal margin between the contents of each data column.
  ///
  /// This value defaults to 56.0 to adhere to the Material Design specifications.
  final double columnSpacing;

【讨论】:

    【解决方案3】:

    flutter PaginatedDataTable 布局的灵活性我也遇到过同样的问题。 我的解决方案是使用布局变量扩展 DataTable 的构造函数:

    FlexibleDataTable({
        Key key,
        @required this.columns,
        this.sortColumnIndex,
        this.sortAscending = true,
        this.onSelectAll,
        this.headingRowHeight = 56.0,
        this.dataRowHeight = 48.0,
        this.tablePadding = 24.0,
        this.columnSpacing = 56.0,
        this.sortArrowPadding = 2.0,
        this.headingFontSize = 12.0,
        @required this.rows,
      }
    

    当然,我必须实现 FlexiblePaginatedDatatable 才能使用 FlexibleDataTable:

    new SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: new FlexibleDataTable(
            key: _tableKey,
            columns: widget.columns,
            sortColumnIndex: widget.sortColumnIndex,
            sortAscending: widget.sortAscending,
            onSelectAll: widget.onSelectAll,
            columnSpacing: 20.0,
            dataRowHeight: 40.0,
            rows: _getRows(_firstRowIndex, widget.rowsPerPage)
        )
    ),
    

    Flutter 团队应该在 DataTable 和 PaginatedDataTable 的正式版本中包含这些功能,这样其他人就不需要为这些琐碎的东西实现 ad hoc 版本。问题存在,我们将看到它何时完成。 https://github.com/flutter/flutter/issues/12775

    【讨论】:

    • 你能提供FlexibleDataTable类吗?
    • 你有了构造函数,变量不再是常量,而是从构造函数获取它们的值: final double headingRowHeight;最终双数据行高度; final double tablePadding;最终双列间距;最终双排序箭头填充;最终双标题字体大小;很抱歉,我无法粘贴所有课程,因为评论太长了。我想我没有改变 Flutter DataTable 类的任何其他东西。
    猜你喜欢
    • 2021-11-28
    • 2011-04-11
    • 2014-06-01
    • 2021-11-08
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多