Flutter 更改颤振中数据表的备用行的颜色
这是我的密码Flutter 更改颤振中数据表的备用行的颜色,flutter,datatable,Flutter,Datatable,这是我的密码 class Reference extends StatefulWidget { @override _ReferenceState createState() => _ReferenceState(); } class _ReferenceState extends State<Reference> { @override Widget build(BuildContext context) { return Scaffold(
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,
),
],
));
}
}
类引用扩展了StatefulWidget{
@凌驾
_ReferenceState createState()=>\u ReferenceState();
}
类_ReferenceState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:Colors.white,
正文:ListView(
儿童:[
SingleChildScrollView(
滚动方向:轴水平,
子:数据表(
数据行高:30,
分割厚度:1.0,
柱间距:30,
栏目:[
数据列(
标签:文本('ID',
样式:TextStyle(
fontSize:18,fontWeight:fontWeight.bold)),
数据列(
标签:文本('参数',
样式:TextStyle(
fontSize:18,fontWeight:fontWeight.bold)),
行:[
数据行(单元格:[
数据单元(文本('1')),
数据单元(文本('a')),
]),
数据行(单元格:[
数据单元(文本('2')),
数据单元(文本('b')),
]),
数据行(单元格:[
数据单元(文本('15')),
数据单元(文本('c')),
]),
],
),
),
分隔器(
身高:5,,
厚度:5,
),
],
));
}
}
我的表有50行长。我没有使用构造函数,因为数据是预先确定的,并且每行都不同。我如何为我的备用行着色?我必须单独设置每个数据行的颜色,还是有自动设置的方法?
我搜索并得到了所有与构造函数一起创建行的解决方案
例如,我在颤振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;
});
},
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
///这是主要的应用程序小部件。
类MyApp扩展了无状态小部件{
静态常量字符串_title='颤振代码示例';
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:_标题,
家:脚手架(
appBar:appBar(标题:常量文本(_title)),
正文:MyStatefulWidget(),
),
);
}
}
///这是主应用程序实例化的有状态小部件。
类MyStatefulWidget扩展了StatefulWidget{
MyStatefulWidget({Key}):超级(Key:Key);
@凌驾
_MyStatefulWidgetState createState()=>\u MyStatefulWidgetState();
}
///这是MyStatefulWidget附带的私有状态类。
类_MyStatefulWidgetState扩展状态{
静态常数int numItems=10;
所选列表=List.generate(numItems,(index)=>false);
@凌驾
小部件构建(构建上下文){
返回大小框(
宽度:double.infinity,
子:数据表(
列:常量[
数据列(
标签:文本(“数字”),
),
],
行:List.generate(
努米特斯,
(索引)=>DataRow(
颜色:MaterialStateProperty.resolveWith(
(设定状态){
//所有行都将具有相同的选定颜色。
if(states.contains(MaterialState.selected))
返回Theme.of(context.colorScheme.primary.withOpacity(0.08);
//即使是行也会有灰色。
如果(索引%2==0)返回颜色。灰色,不透明度(0.3);
返回null;//对其他状态和奇数行使用默认值。
}),
单元格:[数据单元格(文本('Row$index'))],
已选择:已选择[索引],
onSelectChanged:(布尔值){
设置状态(){
所选[索引]=值;
});
},
),
),
),
);
}
}
它与构造函数一起工作终于找到了一种方法
我创建了一个类和该类的对应列表来包含表中的所有数据,然后我使用问题中提到的构造函数方法来完成
最终代码
rows: List<DataRow>.generate(
ReferenceList.length,
(index) => DataRow(
color: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
// Even rows will have a grey color.
if (index % 2 == 0)
return Colors.pinkAccent.withOpacity(0.3);
return null; // Use default value for other states and odd rows.
}),
cells: [
DataCell(Center(
child: Text(ReferenceList[index].id))),
DataCell(Center(
child: Text(
ReferenceList[index].parameter))),
DataCell(Center(
child: Text(ReferenceList[index]
.refValu1))),
DataCell(Center(
child: Text(ReferenceList[index]
.refVal2))),
])),
行:List.generate(
ReferenceList.length,
(索引)=>DataRow(
颜色:MaterialStateProperty.resolveWith(
(设定状态){
//即使是行也会有灰色。
如果(索引%2==0)
返回颜色。pinkAccent。不透明度(0.3);
返回null;//对其他状态和奇数行使用默认值。
}),
单元格:[
数据单元(中心)(
子项:文本(引用列表[index].id)),
数据单元(中心)(
子:文本(
ReferenceList[索引].参数]),
数据单元(中心)(
子:文本(引用列表[索引]
参考文献1),
数据单元(中心)(
子:文本(引用列表[索引]
参考文献2),
])),