I am facing a problem with the default padding of the widgets (IconButton, CheckBox, FlatButton). I have searched a lot for this concern but with no success.
In the above image, the outer blue rect is the actual size of these widgets and I have to remove that space.
Checkbox( onChanged: (value) { setState(() { _rememberMeFlag = !_rememberMeFlag; }); }, value: _rememberMeFlag, activeColor: const Color(0xff00bbff), materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, )
and below is the widget code for hide/show widget icon:
new Container( child: TextFormField( decoration: InputDecoration( labelText: "Password", suffixIcon: Padding( padding: EdgeInsetsDirectional.zero, child: GestureDetector( child: Icon( hidePassword ? Icons.visibility : Icons.visibility_off, size: 20.0, color: Colors.black, ), ), ), contentPadding: const EdgeInsets.only( left: 0.0, top: 6.0, bottom: 6.0, right: 0.0), ), obscureText: !hidePassword, maxLength: 20, ), )
I have tried to set the container size too but no luck. Also tried the padding properties of the widgets but with no success.
Is there any way to remove this extra spacing from these widgets?
10 Answers Simply pass an empty BoxConstrains to the constraints property and a padding of zero. You have to pass the empty constrains because, by default, the IconButton widget assumes a minimum size of 48px.
For all those who are wondering on how to remove the default padding around the text of a FlatButton , you can make use of RawMaterialButton instead and set the constraints to BoxConstraints() which will reset the default minimum width and height of button to zero.
It defaults to 8.0 padding on all sides.
wrap your CheckBox
inside SizedBox
will resize the padding of the check box
SizedBox( height: 24.0, width: 24.0, child: Checkbox(...), )
EDIT
Set the value of materialTapTargetSize to MaterialTapTargetSize.shrinkWrap
Checkbox( materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, ... )
Alternatively, you can achieve this by customising the Checkbox widget.
Create a CustomCheckbox using the exact code from flutter/packages/flutter/lib/src/material/checkbox.dart.
Add a new field to your CustomCheckbox widget
final bool useTapTarget;
Make sure to add the new field to your constructor with it default value set to true.
this.useTapTarget = true
Modify the build method in the _CheckboxState method. Add this block of code above the return call.
Size noTapTargetSize = Size(CustomCheckbox.width, CustomCheckbox.width); final BoxConstraints additionalConstraints = BoxConstraints.tight(widget .useTapTarget? size : noTapTargetSize);
Finally, use your CustomCheckbox widget in your code, and set your custom field to false to remove material padding. example
Container( margin: EdgeInsets.only(right: 15), child:CustomCheckbox( value: _checked, onChanged: _onCheckBoxChange, useTapTarget: false, activeColor: Colors.teal), )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With