Flutter-更改开关边框颜色?

cqoc49vn  于 2023-05-01  发布在  Flutter
关注(0)|答案(3)|浏览(140)

文档:https://api.flutter.dev/flutter/material/Switch-class.html
当开关关闭时,如何更改边框颜色?在材质3中,启用时,边框颜色与切换背景颜色相同,但禁用时,边框颜色具有边框。如何在材料3中更改它?

验证码:

Switch(
                      trackColor: (miniVerbose)
                          ? const MaterialStatePropertyAll(Colors.greenAccent)
                          : const MaterialStatePropertyAll(Colors.orange),
                      value: miniVerbose,
                      onChanged: (value) {
                        setState(() {
                          miniVerbose = value;
                        });
                      },
                    ),
5tmbdcev

5tmbdcev1#

刚刚更新Vivek代码和 Package 开关与主题部件的单个按钮,下面是更新的代码-

Theme(
      data: ThemeData(
        useMaterial3: true,
      ).copyWith(
        colorScheme: Theme.of(context).colorScheme.copyWith(outline: Colors.orange),
      ),
      child: Switch(
        value: value,
        activeTrackColor: Colors.orange,
        inactiveTrackColor: Colors.yellow,
        activeColor: Colors.yellow,
        onChanged: (_) => setState(() => value = !value),
      ),
    );
zbdgwd5y

zbdgwd5y2#

使用此代码:

theme: ThemeData(
        useMaterial3: true,
      ).copyWith(
        colorScheme:
        Theme.of(context).colorScheme.copyWith(outline: Colors.orange),
      ),

开关:

Switch(
       value: value,
       activeTrackColor: Colors.orange,
       inactiveTrackColor: Colors.yellow,
       activeColor: Colors.yellow,
       onChanged: (_) => setState(() => value = !value),
     ),

vq8itlhq

vq8itlhq3#

what about this alternative: 

Container(
  decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(20.0),
    border: Border.all(
      color: isSwitchedOn ? Colors.greenAccent : Colors.grey,
      width: 2.0,
    ),
  ),
  child: Switch(
    value: isSwitchedOn,
    onChanged: (value) {
      setState(() {
        isSwitchedOn = value;
      });
    },
  ),
),

相关问题