How can I disabled flutter switch

2,744

Solution 1

To disable your Switch, edit its onChanged method to null like this

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

Solution 2

If you want to have static switch with manually set value, consider using AbsorbPointer. It will not change way your switch looks and will not disable it. AbsorbPointer is just preventing user from interacting with element

Share:
2,744
Admin
Author by

Admin

Updated on December 26, 2022

Comments

  • Admin
    Admin over 1 year

    In my help screen, I have this switch and the purpose for that is to do nothing but just display like the way it is.

    But the problem I'm having right now, even though it doesn't do anything, the user can drag the switch so I'm trying to figure out how I can disabled it so that no one can drag the switch button.

        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

  • Admin
    Admin over 3 years
    ohh..thanks it got disabled but it turn out not the way I imagined. The switch got disabled but the color doesn't applied to the switch anymore, do you know how can I applied light blue color(just like in the image) and make the switch still disabled?.
  • dm_tr
    dm_tr over 3 years
    You can also use Theme.of(context).accentColor and Theme.of(context).accentColor.withOpacity(0.5) for example
  • Admin
    Admin over 3 years
    oh ok...but I think I'm not allow to use both inactiveThumbColor and inactiveTrackColor at the same time because I'm getting error. (attached image) do you know what might be the reason. thanks
  • Admin
    Admin over 3 years
    it saying Undefined name 'inactiveTrackColor'. Try correcting the name to one that is defined, or defining the name.
  • dm_tr
    dm_tr over 3 years
    Close your widget with ) symbol
  • Admin
    Admin over 3 years
    It work!.. Thank you so much for your help on both of my question. thanks you
  • Admin
    Admin over 3 years
    Sorry dm_tr hehe.. I got one more question and can you help me out again too... stackoverflow.com/questions/65398159/… .. thank you so much if you can.
  • dm_tr
    dm_tr over 3 years
  • ChillBroDev
    ChillBroDev over 2 years
    This should be the answer, thanks for teaching me about a new widget.