【问题标题】:How do you center the label in a Flutter DataColumn widget?如何在 Flutter DataColumn 小部件中将标签居中?
【发布时间】:2019-06-16 00:13:38
【问题描述】:

我可以在 DataRow 中将 DataCell 居中,但如何为 DataColumn 标签做到这一点?

我希望第一个 DataColumn 左对齐,其余居中。将标签包裹在 Center 小部件中不会生效。

new DataColumn(
            label: Center(
              child: Text(statName,textAlign: TextAlign.center,
                style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),),
            )
        );

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    如何找到:

    DataColumn(
        label: Expanded(
            child: Text(
      'Label',
      textAlign: TextAlign.center,
    ))),
    

    【讨论】:

    • 使用这个后我得到了这个错误Incorrect use of ParentDataWidget. The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type BoxParentData. Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets. The offending Expanded is currently placed inside a Align widget.
    • 哪个 Flutter 版本?我刚刚在 2.10.3 上再次测试它并且它正在工作。
    • 我也在使用当前最新版本
    • 嘿,伙计,将给您错误的代码发布为另一个问题,以便我可以尝试帮助您。我相信它与上面的sn-p无关。
    【解决方案2】:

    您可能希望将 Label 的内容包装在 Center 小部件中。还有一个 Align 小部件,它使用 alignment: Alignment.center 和您的 Text 作为它的孩子。

    【讨论】:

    • 感谢您的回复。不幸的是,即使使用 Align 或 Center 包裹的 label: contents (Text) 也不会将其从左对齐位置移动。尝试将标签内容居中并对齐。
    【解决方案3】:

    这就是我解决这个问题的方法:

    DataColumn(
                label: Center(
                widthFactor: 5.0, // You can set as per your requirement.
                child: Text(
                'View',
                style: style_16_bold_primary,
                ),
              ),
          ),
    

    【讨论】:

    • 问题已经在说Center 对他不起作用。
    【解决方案4】:

    我遇到了同样的问题,通过注释 data_table.dart 文件中的以下代码部分解决了这个问题。

    if (onSort != null) {
      final Widget arrow = _SortArrow(
        visible: sorted,
        down: sorted ? ascending : null,
        duration: _sortArrowAnimationDuration,
      );
      const Widget arrowPadding = SizedBox(width: _sortArrowPadding);
      label = Row(
        textDirection: numeric ? TextDirection.rtl : null,
        children: <Widget>[ label, arrowPadding, arrow ],
      );
    }
    

    【讨论】:

    • 我不认为改变核心功能是个好主意。也许从它们扩展并更改您想要的方法会更合适。
    猜你喜欢
    • 2023-01-09
    • 2019-03-30
    • 2022-06-26
    • 2021-03-26
    • 1970-01-01
    • 2023-02-06
    • 2018-03-11
    • 2018-04-16
    • 2022-08-08
    相关资源
    最近更新 更多