简体   繁体   English

对齐 Flutter 中数据表中的小数点

[英]Align decimal points in a datatable in Flutter

Values are centered: -值居中:-

I need them to be centered with respect to the decimal point: -我需要它们以小数点为中心:-

Hello, good afternoon.你好,下午好。 I am making a POS to practice Flutter and I need to know if it is possible to center a text with respect to a certain character.我正在制作一个 POS 来练习 Flutter,我需要知道是否可以针对某个字符将文本居中。

Inside my DataTable I have my DataColumns and my Datacels, inside the latter I have an Align with a Text.在我的 DataTable 中,我有我的 DataColumns 和我的 Datacels,在后者中我有一个 Align with a Text。 But obviously that align does not center as I want.但很明显,对齐并不像我想要的那样居中。

return DataCell(
      Align(
        alignment: Alignment.center,
        child: Text(
          datos,
          style: const TextStyle(fontSize: 18, color: style.Colors.lightColor),
        ),
      ),
    );

This is workaround, Add textAlign Property of Text Widget then use a Container Widget and apply margin Property of it to add a padding to right side.这是解决方法,添加Text小部件的textAlign属性,然后使用Container小部件并应用它的margin属性以向右侧添加填充。

Sample Code: -示例代码:-

SizedBox(
width: 110,
child: Container(
  margin: const EdgeInsets.only(right: 25),
  child: const Text(
    '15201,36',
    style: TextStyle(fontSize: 18),
    textAlign: TextAlign.end,
  )),
),

Complete Code: -完整代码:-

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatelessWidget(),
      ),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: DataTable(
        columns: const <DataColumn>[
          DataColumn(
            label: Expanded(
              child: Center(
                child: Text(
                  'Number',
                  style: TextStyle(fontStyle: FontStyle.italic),
                ),
              ),
            ),
          ),
        ],
        rows: <DataRow>[
          DataRow(
            cells: <DataCell>[
              DataCell(Center(
                child: SizedBox(
                  width: 110,
                  child: Container(
                      margin: const EdgeInsets.only(right: 25),
                      child: const Text(
                        '950,00',
                        style: TextStyle(fontSize: 18),
                        textAlign: TextAlign.end,
                      )),
                ),
              )),
            ],
          ),
          DataRow(
            cells: <DataCell>[
              DataCell(Center(
                child: SizedBox(
                  width: 110,
                  child: Container(
                      margin: const EdgeInsets.only(right: 25),
                      child: const Text(
                        '1300,00',
                        style: TextStyle(fontSize: 18),
                        textAlign: TextAlign.end,
                      )),
                ),
              )),
            ],
          ),
          DataRow(
            cells: <DataCell>[
              DataCell(Center(
                child: SizedBox(
                  width: 110,
                  child: Container(
                      margin: const EdgeInsets.only(right: 25),
                      child: const Text(
                        '6949,85',
                        style: TextStyle(fontSize: 18),
                        textAlign: TextAlign.end,
                      )),
                ),
              )),
            ],
          ),
          DataRow(
            cells: <DataCell>[
              DataCell(Center(
                child: SizedBox(
                  width: 110,
                  child: Container(
                      margin: const EdgeInsets.only(right: 25),
                      child: const Text(
                        '15201,36',
                        style: TextStyle(fontSize: 18),
                        textAlign: TextAlign.end,
                      )),
                ),
              )),
            ],
          ),
        ],
      ),
    );
  }
}

Output: - Output:-

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM