Flutter: изменить цвет строк и столбцов DataTable - PullRequest
0 голосов
/ 24 сентября 2018

У меня небольшая таблица данных, и я хочу изменить цвет фона столбца и строки, но, к сожалению, в DataColumn или DataRow нет свойства для достижения этой цели.единственный способ, который я нашел, - это изменить метку 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")),
          ],
        ),
      ]),
    );
  }
}

1 Ответ

0 голосов
/ 24 сентября 2018

Я нашел способ, с помощью которого вы можете получить точно такой же внешний вид, что и таблица, а также изменить цвета фона, используя Строка и Расширенные Виджеты.Я надеюсь, что следующий код поможет вам.

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