M.Ali
M.Ali

Reputation: 10235

Change color of DataTable rows and columns

I have a small data table and i want to change the column and row background color.
But unfortunately there is no property in DataColumn or DataRow to achieve this.
The only way i found is through modifying the label of DataColumn

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

but there is a default padding for the DataColumn and the color only applies to the text.

and here is my code:

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")),
          ],
        ),
      ]),
    );
  }
}

Upvotes: 4

Views: 10227

Answers (2)

Raju Gupta
Raju Gupta

Reputation: 792

If you are still looking for answer, you can use MaterialStateColor property. Here is the working code

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

Upvotes: 2

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27147

I found a way using that you can achieve exactly same look as table and also change the colors of background using Row and Expanded Widgets. I hope that Following Code Help You.

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")
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              )
          ),
        );
      }
    }

Upvotes: -1

Related Questions