Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - how to remove default padding (48 px as per doc) from widgets (IconButton, CheckBox, FlatButton)

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.

enter image description here

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?

like image 405
Rahul Sharma Avatar asked Nov 05 '18 10:11

Rahul Sharma


People also ask

How do I get rid of IconButton padding flutter?

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.

How do you get rid of the padding of an elevated button flutter?

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.

What is default padding in flutter?

It defaults to 8.0 padding on all sides.


2 Answers

wrap your CheckBox inside SizedBox will resize the padding of the check box

  SizedBox(     height: 24.0,     width: 24.0,     child: Checkbox(...),  ) 
like image 117
Afinas EM Avatar answered Sep 19 '22 09:09

Afinas EM


EDIT

Set the value of materialTapTargetSize to MaterialTapTargetSize.shrinkWrap

Checkbox(    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,     ...  ) 

Alternatively, you can achieve this by customising the Checkbox widget.

  1. Create a CustomCheckbox using the exact code from flutter/packages/flutter/lib/src/material/checkbox.dart.

  2. Add a new field to your CustomCheckbox widget

         final bool useTapTarget; 
  3. Make sure to add the new field to your constructor with it default value set to true.

         this.useTapTarget = true 
  4. 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); 
  5. 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),       ) 

Screenshot

like image 38
Myorh Avatar answered Sep 18 '22 09:09

Myorh