拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Flutter-更改资料表复选框的填充颜色

Flutter-更改资料表复选框的填充颜色

白鹭 - 2022-01-25 2250 0 0

我想更改资料表复选框的背景颜色。

例子

我想从这个:

Flutter - 更改资料表复选框的填充颜色

对此(这是一张图纸):

Flutter - 更改资料表复选框的填充颜色

是否可以从主题更改它?

编辑 1:

我已经尝试过checkboxTheme,它只会将边框从白色更改为我的颜色。但我只想改变填充颜色。

我的复选框主题是:

CheckboxThemeData(fillColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
    if (states.contains(MaterialState.selected))
        return primaryColor;
    return null;
})),

但是我的资料表的复选框仍然是蓝色的,而复选框的小部件改变了颜色...... Flutter - 更改资料表复选框的填充颜色

(底部的复选框来自复选框小部件,用于比较)

uj5u.com热心网友回复:

这是重新创建复选框主题的尝试:

CheckboxThemeData(
  side: MaterialStateBorderSide.resolveWith(
      (_) => const BorderSide(width: 1, color: Colors.blue)),
  fillColor: MaterialStateProperty.all(Colors.red),
  checkColor: MaterialStateProperty.all(Colors.white),
),

尝试使用:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool checked = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light().copyWith(
        checkboxTheme: CheckboxThemeData(
          side: MaterialStateBorderSide.resolveWith(
              (_) => const BorderSide(width: 1, color: Colors.blue)),
          fillColor: MaterialStateProperty.all(Colors.red),
          checkColor: MaterialStateProperty.all(Colors.white),
        ),
      ),
      home: Scaffold(
        body: Center(
          child: Checkbox(
            value: checked,
            onChanged: (b) {
              setState(() {
                checked = b ?? false;
              });
            },
          ),
        ),
      ),
    );
  }
}

编辑:做一些挖后,我发现,DataTable使用ThemeData.colorScheme.primary作为填充颜色。

试试这个:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool checked = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light().copyWith(
        colorScheme: ThemeData.light().colorScheme.copyWith(
              onPrimary: Colors.white, // Color for checkmark in datatable
              primary: Colors.red, // Color used for checkbox fill in datatable
            ),
        checkboxTheme: CheckboxThemeData(
          side: MaterialStateBorderSide.resolveWith(
              (_) => const BorderSide(width: 1, color: Colors.blue)),
          fillColor: MaterialStateProperty.all(Colors.red),
          checkColor: MaterialStateProperty.all(Colors.white),
        ),
      ),
      home: Scaffold(
        body: DataTable(
          columns: const [
            DataColumn(label: Text('Checkbox')),
          ],
          rows: [
            DataRow(
              selected: checked,
              onSelectChanged: (b) {
                setState(() {
                  checked = b ?? false;
                });
              },
              cells: const [
                DataCell(Text('hi')),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

uj5u.com热心网友回复:

import 'package:flutter/material.dart';

void main() {
  runApp(const Example_App());
}

class Example_App extends StatefulWidget {
  const Example_App({Key? key}) : super(key: key);
  @override
  _Example_AppState createState() => _Example_AppState();
}

class _Example_AppState extends State<Example_App> {
  bool checked = false;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light().copyWith(
        checkboxTheme: CheckboxThemeData(
          side: MaterialStateBorderSide.resolveWith(
              (_) => const BorderSide(width: 1, color: Colors.blue)),
          fillColor: MaterialStateProperty.all(Colors.red),
          checkColor: MaterialStateProperty.all(Colors.white),
        ),
      ),

      home: Scaffold(
        body: Center(
          child: Checkbox(
            value: checked,
            onChanged: (b) {
              setState(() {
                checked = b ?? false;
              });
            },
          ),
        ),
      ),
    );
  }
}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *