在Flutter中如何创建开关按钮?

huangapple go评论66阅读模式
英文:

How to create the switch button in a flutter?

问题

在Flutter中,如何在一行中创建开关按钮和文本。我尝试过但没有得到我想要的结果。以及如何删除自定义开关内部的标签。

import 'package:custom_switch/custom_switch.dart';

bool status = true;

Column(
  children: [
    Padding(
      padding: const EdgeInsets.only(top: 40),
      child: CustomSwitch(
        activeColor: Color(0xff771ae1),
        value: status,
        onChanged: (value) {
          setState(() {});
        },
      ),
      SizedBox(height: 12.0,),
      Text('Value : $status', style: TextStyle(
        color: Colors.black,
        fontSize: 20.0
      ),)
    ),
  ],
);
英文:

In flutter, how to create a switch button and text in a row. I tried but didn't get what I wanted. And how to remove the tag inside the custom switch.

在Flutter中如何创建开关按钮?

import 'package:custom_switch/custom_switch.dart';

bool status = true;

    Column(
            children: [
              Padding(
                padding: const EdgeInsets.only(top: 40),
                child: CustomSwitch(
                  activeColor: Color(0xff771ae1),
                  value: status,
                  onChanged: (value) {
                    setState(() {});
                  },
                ),
                SizedBox(height: 12.0,),
            Text('Value : $status', style: TextStyle(
              color: Colors.black,
              fontSize: 20.0
            ),)
              ),
            ],
          ),

</details>


# 答案1
**得分**: 1

你可以使用 [`SwitchListTile`][1] 小部件。对于你的情况,你需要使用 `CupertinoSwitch`。

```dart
CupertinoListTile(
    title: Text("title"),
    trailing: CupertinoSwitch(
      value: true,
      thumbColor: Colors.white,
      activeColor: Colors.deepPurple,
      // trackColor: ,
      
      onChanged: (value) {},
    )),

在Flutter中如何创建开关按钮?

英文:

You can use SwitchListTile widget. For your case, you need to use CupertinoSwitch.

CupertinoListTile(
    title: Text(&quot;title&quot;),
    trailing: CupertinoSwitch(
      value: true,
      thumbColor: Colors.white,
      activeColor: Colors.deepPurple,
      // trackColor: ,
      
      onChanged: (value) {},
    )),

在Flutter中如何创建开关按钮?

huangapple
  • 本文由 发表于 2023年2月18日 21:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定