在颤动中更改数据表的交替行的颜色

时间:2021-01-26 10:45:44

标签: flutter datatable

这是我的代码

class Reference extends StatefulWidget {
  @override
  _ReferenceState createState() => _ReferenceState();
}

class _ReferenceState extends State<Reference> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: ListView(
          children: [
            SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: DataTable(
                dataRowHeight: 30,
                dividerThickness: 1.0,
                columnSpacing: 30,
                columns: [
                  DataColumn(
                      label: Text('ID',
                          style: TextStyle(
                              fontSize: 18, fontWeight: FontWeight.bold))),
                  DataColumn(
                      label: Text('Parameter',
                          style: TextStyle(
                              fontSize: 18, fontWeight: FontWeight.bold))),
                 
                rows: [
                  DataRow(cells: [
                    DataCell(Text('1')),
                    DataCell(Text('a')),
                  ]),
                  DataRow(cells: [
                    DataCell(Text('2')),
                    DataCell(Text('b')),
                  ]),
                  DataRow(cells: [
                    DataCell(Text('15')),
                    DataCell(Text('c')),
                  ]),
                ],
              ),
            ),
            Divider(
              height: 5,
              thickness: 5,
            ),
            
 ],
        ));
  }
}

我的表有 50 行。我没有使用构造函数,因为数据是预先确定的,每一行都不同。如何为备用行着色?我必须单独设置每个 DataRow 的颜色还是有办法自动设置? 我进行了搜索,我得到的所有解决方案都适用于创建行的构造函数。

例如我在 flutter api 上发现了这个

import 'package:flutter/material.dart';

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

/// This is the main application widget.
class MyApp extends StatelessWidget {
  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: MyStatefulWidget(),
      ),
    );
  }
}

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  static const int numItems = 10;
  List<bool> selected = List<bool>.generate(numItems, (index) => false);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      child: DataTable(
        columns: const <DataColumn>[
          DataColumn(
            label: Text('Number'),
          ),
        ],
        rows: List<DataRow>.generate(
          numItems,
          (index) => DataRow(
            color: MaterialStateProperty.resolveWith<Color>(
                (Set<MaterialState> states) {
              // All rows will have the same selected color.
              if (states.contains(MaterialState.selected))
                return Theme.of(context).colorScheme.primary.withOpacity(0.08);
              // Even rows will have a grey color.
              if (index % 2 == 0) return Colors.grey.withOpacity(0.3);
              return null; // Use default value for other states and odd rows.
            }),
            cells: [DataCell(Text('Row $index'))],
            selected: selected[index],
            onSelectChanged: (bool value) {
              setState(() {
                selected[index] = value;
              });
            },
          ),
          ),
        ),
      );
    }
  }

与构造函数一起使用

1 个答案:

答案 0 :(得分:1)

终于找到方法了!

我创建了一个类和该类的相应列表来包含表的所有数据。然后我用问题中提到的构造函数方法来做。

最终代码

Editor placeholder in source file