Flutter: Is there any way to change the row line color of DataTable?

7,338

Solution 1

Use Theme widget and overriding the dividerColor as shown below.

enter image description here

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Theme(
          data: Theme.of(context).copyWith(
              dividerColor: Colors.green
          ),
          child: DataTable(
              columns: [
                DataColumn(label: Text('Language')),
                DataColumn(label: Text('Year'))
              ],
              rows: [
                DataRow(
                  cells: [
                    DataCell(Text('Go')),
                    DataCell(Text('2009'))
                  ],
                ),
                DataRow(
                  cells: [
                    DataCell(Text('Dart')),
                    DataCell(Text('2018'))
                  ],
                ),
                DataRow(
                  cells: [
                    DataCell(Text('Java')),
                    DataCell(Text('1992'))
                  ],
                ),
              ]
          ),
        ),
      ),
    );
  }
}

Solution 2

If you are still looking for the answer, here is the code

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

Yousef Roshandel

I'm Yousef Roshandel, born in March 1999 in Arak, and graduated from Amirkabir University of Arak with a degree in software engineering. I'm currently working at the Xeniac team as a Front-end developer. I started programming from 2014 with C# language. Then, I learnt HTML/CSS and JAVA. My main specialty is Front-end development, but I'm also very interested in designing User interfaces as well. Before starting the web application development, I was a mobile application dveloper with the Flutter framework and Android native with JAVA.

Updated on December 15, 2022

Comments

  • Yousef Roshandel
    Yousef Roshandel over 1 year

    I'm using DataTable in my recent app and i need to change the color of the row line or make it invisible (i mean I don't want to my table shows any row line)

    If anyone knows please help! thanks