如何禁用颤振开关

时间:2020-12-21 17:41:27

标签: flutter dart layout flutter-layout flutter-dependencies

在我的帮助屏幕中,我有这个开关,它的目的是什么都不做,只是按原样显示。

但是我现在遇到的问题,即使它没有做任何事情,用户也可以拖动开关,所以我想弄清楚如何禁用它,以便没有人可以拖动开关按钮.

    return Container(
      child: Card(
        color: Theme.of(context).primaryColor,
        margin: EdgeInsets.only(bottom: 30, top: 10),
        child: ListTile(
          title: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text("Dark Theme",
                  style: TextStyle(color: Theme.of(context).accentColor)),
              Switch(
                  value: true,
                  onChanged: (value) {},
                  activeColor: Theme.of(context).accentColor),
              Text("Light Theme", style: TextStyle())
            ],
          ),
        ),
      ),
    );
  }

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:3)

要禁用您的 Switch,请像这样将其 onChanged 方法编辑为 null

Switch(
  onChanged: null,
  value: true,
  inactiveThumbColor: Colors.tealAccent,
  inactiveTrackColor: Colors.tealAccent.withOpacity(0.5),
  // ...
),

答案 1 :(得分:0)

如果你想手动设置静态开关,可以考虑使用AbsorbPointer。它不会改变您的开关的外观,也不会禁用它。 AbsorbPointer 只是阻止用户与元素交互