如何将第一行和第一列固定为在数据表中始终可见-Flutter

时间:2019-06-19 07:46:41

标签: flutter dart flutter-layout

所以我有一个数据表,由于可用数据量的缘故,它可以水平和垂直滚动。我希望指定列名称的第一行在垂直向下滚动时始终可见,而最左侧的列在水平滚动时始终可见。

datatable

@override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      title: Text(appTitle),
    ),
    body: DataTableWidget(),
  );

class DataTableWidgetState extends State<DataTableWidget> {

  @override
  Widget build(BuildContext context) {
    final width = 100 * cityColumns.length;
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: SizedBox(
        width: width * 1.4,
        child: ListView(
          children: <Widget>[
            buildDataTable(),
          ],
        ),
      ),
    );
  }

  Widget buildDataTable() => DataTable(
    sortAscending: ascending,
    sortColumnIndex: 0,
    columns: cityColumns
        .map(
          (String column) => DataColumn(
        label: Text(column),
        onSort: (int columnIndex, bool ascending) => onSortColumn(
            columnIndex: columnIndex, ascending: ascending),
      ),
    )
        .toList(),
    rows: cities
        .map((City city) => DataRow(
      selected: selectedCities.contains(city),
      cells: [
        DataCell(Text('${city.name}')),
        DataCell(Text('${city.nation}')),
        DataCell(Text('${city.name2}')),
        DataCell(Text('${city.nation2}')),
        DataCell(Text('${city.name3}')),
        DataCell(Text('${city.nation3}')),
        DataCell(Text('${city.population}')),
        DataCell(Text('${city.nation}')),
      ],
    ))
        .toList(),
  );
}

0 个答案:

没有答案