flutteralignment

How can I right align each column in the Flutter DataTable?


How can I right align each DataCell in the Flutter DataTable?

For example, in this case, how can I have all the numbers aligned to the right?

Widget build(BuildContext context) {
  return DataTable(
    columns: const <DataColumn>[
      DataColumn(
        label: Expanded(child: Text('Name', style: TextStyle(fontStyle: FontStyle.italic))),
      ),
      DataColumn(
        label: Expanded(child: Text('Number', style: TextStyle(fontStyle: FontStyle.italic))),
      ),
    ],
    rows: const <DataRow>[
      DataRow(
        cells: <DataCell>[
          DataCell(Text('Sarah')),
          DataCell(Text('1')),
        ],
      ),
      DataRow(
        cells: <DataCell>[
          DataCell(Text('Janine')),
          DataCell(Text('100')),
        ],
      ),
      DataRow(
        cells: <DataCell>[
          DataCell(Text('William')),
          DataCell(Text('1000')),
        ],
      ),
    ],
  );
}

Thanks in advance!


Solution

  • You can just use the numeric property.

    @immutable
    class DataColumn {
      const DataColumn({
        required this.label,
        this.tooltip,
        this.numeric = false,
        this.onSort,
      }) : assert(label != null);
    
    
      /// Whether this column represents numeric data or not.
      ///
      /// The contents of cells of columns containing numeric data are
      /// right-aligned.
      final bool numeric;
      ///...
    }