【问题标题】:Change color of DataTable rows and columns更改 DataTable 行和列的颜色
【发布时间】:2019-02-28 07:00:19
【问题描述】:

我有一个小数据表,我想更改列和行的背景颜色。
但不幸的是,DataColumnDataRow 中没有属性来实现这一点。
我找到的唯一方法是通过修改DataColumn的标签

DataColumn(label: Container(child: Text('Person'),color: Colors.amberAccent,)),

但 DataColumn 有默认填充,颜色仅适用于文本。

这是我的代码:

class _ContactLocationsState extends State<ContactLocations> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: DataTable(columns: <DataColumn>[
        DataColumn(label: Text('Person')),
        DataColumn(label: Text('Rating')),
        DataColumn(label: Text('Distance')),
        DataColumn(label: Text('Max Price')),
        DataColumn(label: Text('Fee')),
      ], rows: <DataRow>[
        DataRow(
          cells: <DataCell>[
            DataCell(Text("test")),
            DataCell(Text("test")),
            DataCell(Text("test")),
            DataCell(Text("test")),
            DataCell(Text("test")),
          ],
        ),
      ]),
    );
  }
}

【问题讨论】:

  • 那么你在哪里可以做到?

标签: dart flutter flutter-layout


【解决方案1】:

如果您仍在寻找答案,您可以使用 MaterialStateColor 属性。这是工作代码

 return DataRow.byIndex(
              index: row.key,
              color: MaterialStateColor.resolveWith(
                (states) {
                  if (row.key % 2 == 0) {
                    return Colors.blue[50];
                  } else {
                    return Colors.white;
                  }
                },
              ),

【讨论】:

    【解决方案2】:

    我找到了一种方法,您可以使用该方法获得与表格完全相同的外观,还可以使用 RowExpanded 小部件更改背景颜色。 希望以下代码对您有所帮助。

    import 'package:flutter/material.dart';
    
      void main() => runApp(new MyApp());
    
      class MyApp extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return new MaterialApp(
            title: 'Flutter Demo',
            theme: new ThemeData(
              primarySwatch: Colors.blue,
            ),
            debugShowCheckedModeBanner: false,
            home: new MyHomePage(),
          );
        }
      }
    
      class MyHomePage extends StatefulWidget {
        @override
        _MyHomePageState createState() => _MyHomePageState();
      }
    
      class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
        AnimationController _controller;
    
        @override
        void initState() {
          _controller = AnimationController(vsync: this);
          super.initState();
        }
    
        @override
        void dispose() {
          _controller.dispose();
          super.dispose();
        }
    
    
          @override
          Widget build(BuildContext context) {
            return Scaffold(
              body: Container(
                  child: Center(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Expanded(
                              child: Container(
                                  color: Colors.green,
                                  child: Text("person")
                              ),
                            ),Expanded(
                              child: Container(
                                  color: Colors.red,
                                  child: Text("Rating")
                              ),
                            ),Expanded(
                              child: Container(
                                  color: Colors.green,
                                  child: Text("Distance")
                              ),
                            ),Expanded(
                              child: Container(
                                  color: Colors.red,
                                  child: Text("Max Price")
                              ),
                            ),Expanded(
                              child: Container(
                                  color: Colors.green,
                                  child: Text("Free")
                              ),
                            ),
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            Expanded(
                              child: Container(
                                  color: Colors.red,
                                  child: Text("Test")
                              ),
                            ),Expanded(
                              child: Container(
                                  color: Colors.green,
                                  child: Text("Test")
                              ),
                            ),Expanded(
                              child: Container(
                                  color: Colors.red,
                                  child: Text("Test")
                              ),
                            ),Expanded(
                              child: Container(
                                  color: Colors.green,
                                  child: Text("Test")
                              ),
                            ),Expanded(
                              child: Container(
                                  color: Colors.red,
                                  child: Text("Test")
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  )
              ),
            );
          }
        }
    

    【讨论】:

    • 我认为这效率不高,因为会有很多行取决于 json 响应
    • 你能解释一下它为什么不能高效吗?出了什么问题?
    • 你为什么不使用列表视图呢。
    猜你喜欢
    • 2019-07-26
    • 2020-02-04
    • 1970-01-01
    • 2014-11-02
    • 2012-11-24
    • 1970-01-01
    • 2018-06-24
    • 2019-05-29
    • 2020-01-13
    相关资源
    最近更新 更多