【问题标题】:Flutter CupertinoSwitch background color changeFlutter CupertinoSwitch 背景颜色变化
【发布时间】:2021-11-10 11:41:56
【问题描述】:

如何更改颜色,人行道显示为白色 - 当我尝试使用活动颜色而不是更改整个边框颜色时。我只想更改图像切换颜色 - 橙色和背景颜色白色。

【问题讨论】:

  • 分享你的代码

标签: flutter flutter-layout


【解决方案1】:

虽然默认颜色是白色,因为

null 时默认为 [CupertinoColors.white]。

我们可以使用thumbColor根据活动状态分配颜色。

CupertinoSwitch(
                activeColor: Colors.white,
                thumbColor: on ? Colors.white : Colors.amber,
                trackColor: Colors.white,
                value: on,
                onChanged: (value) => setState(() {
                  on = !on;
                }),
              )

演示小部件


import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

class MyAppH extends StatefulWidget {
  const MyAppH({Key? key}) : super(key: key);

  @override
  State<MyAppH> createState() => _MyAppHState();
}

class _MyAppHState extends State<MyAppH> {
  bool on = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CupertinoSwitch(
          activeColor: Colors.white,
          thumbColor: on ? Colors.white : Colors.amber,
          trackColor: Colors.white,
          value: on,
          onChanged: (value) => setState(() {
            on = !on;
          }),
        ),
      ),
    );
  }
}

【讨论】:

  • ThumbColor 出现错误?
  • 在我的情况下很好,你能分享错误吗?
  • cupertinoSwitch 没有找到,thumbColor?可能需要吗?我该怎么做。
  • 检查你的导入是否有import 'package:flutter/cupertino.dart';
  • 解决问题了吗?
猜你喜欢
  • 2017-04-14
  • 2021-10-02
  • 2014-09-14
  • 2019-10-28
  • 2021-02-13
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多