Reputation: 4319
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.
How can I get rid of the extra space
Note: same issue also using Radio button or Checkbox
Upvotes: 17
Views: 7616
Reputation: 917
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) { },
)
),
),
Upvotes: 1
Reputation: 398
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/
Upvotes: 10
Reputation: 191
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,
),
),
Upvotes: 0
Reputation: 75
You should wrap the Switch
with SizedBox
and set your width and height.
Upvotes: 1
Reputation: 51
To remove the padding from the Switch
widget you need to set the materialTapTargetSize
to shrinkWrap
.
Switch(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
onChanged: (bool value) {},
)
Upvotes: 5
Reputation: 723
You could try to wrap your Switch widget in Container
Container(
height: 20,
width: 50,
child: Switch(
value: _value,
onChanged: (value){
setState((){
_value = value
});
}
)
);
Upvotes: 7