Flutter | remove extra space from Switch widget
Asked Answered
D

6

16

I'm working on the below screen in a flutter app, but when I use Switch widget it breaks the alignment, because there is hardcoded height and width already defined in flutter.

enter image description here enter image description here

How can I get rid of the extra space

Note: same issue also using Radio button or Checkbox

Despot answered 3/2, 2020 at 4:21 Comment(5)
paste code to understand issue better. Please tell your required screen in a image.Whiteside
Of course you can do it by changing theme but It against Material Design guideline(current size is finger touch size). material.io/design/guidelines-overview/#additionInfertile
@RahulKhatri it's a common issue whenever Switch widget used.Despot
How did you display the borderlines of those widgets in you second image?Phenosafranine
@Phenosafranine when you run the project using vscode it's open a browser tab where you can inspect the widgets and check their bordersDespot
E
10
SizedBox(
  width: 50,
  height: 20,
  child: Switch(
  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  value: value,
  onChanged: (bool newvalue) {
    onChangeMethod(newvalue);
  },
  activeTrackColor: Colors.grey,
  activeColor: Colors.black,
  ),
);

You can refer https://flutterwebbies.com/flutter/how-to-remove-extra-space-from-switch-widget/

Ephemerality answered 6/6, 2022 at 7:16 Comment(1)
This one was the most complete answer, the combination of SizedBox() and MaterialTapTargetSize.shrinkWrap allowed me to resize the Switch widgetSpoken
M
7

enter image description here

You could try to wrap your Switch widget in Container

Container(
  height: 20,
  width: 50,
  child: Switch(
   value: _value,
   onChanged: (value){
    setState((){
      _value = value
    });
   }
  )
);
Minute answered 3/2, 2020 at 5:12 Comment(2)
Unfortunately still the same issueDespot
@Yogesh Chawla worked for me.Ananna
T
5

To remove the padding from the Switch widget you need to set the materialTapTargetSize to shrinkWrap.

Switch(
  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  onChanged: (bool value) {},
)
Tramontane answered 15/9, 2021 at 13:14 Comment(2)
This is much cleaner than hard coding a random heightAblative
MaterialTapTargetSize.shrinkWrap "Shrinks the tap target size to the minimum provided by the Material specification." It does NOT remove all the padding.Cool
M
1

You should wrap the Switch with SizedBox and set your width and height.

Melli answered 27/11, 2021 at 14:56 Comment(0)
D
1

We Can use Container With Transform.scale widget and set max Constraints according to requirements

Container(
                constraints: const BoxConstraints(maxHeight: 6.0),
                child: Transform.scale(
                       scale: 0.6,
                       child: Switch(
                                materialTapTargetSize:MaterialTapTargetSize.shrinkWrap,
                                activeColor: Colors.red,
                                onChanged: (bool value) { },
                                     ) 
                                      ),
               ),

switch look like

Dike answered 11/3 at 17:40 Comment(0)
S
0

We can use Container with constraints to set maxHeight

      Container(
                constraints: const BoxConstraints(maxHeight: 5.0),
                child: Switch(
                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  value: true,
                  activeColor: kAccentColor,
                  onChanged: (p0) => p0,
                ),
              ),
Surreptitious answered 13/3, 2023 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.