Flutter decrease space between checkbox and it's title

Viewed 4012

I am using CheckboxListTile in this way:

ListTileTheme(
  contentPadding: EdgeInsets.only(right: 20.0),
  child: CheckboxListTile(
    controlAffinity: ListTileControlAffinity.leading,
    value: false,
    onChanged: (value) {},
    title: Text(
      "افزودن به فهرست پرکاربردها"
    ),
  ),
),

and it's result :

enter image description here

How can I decrease space between checkbox and it's title?

3 Answers

use Transform.translate

Transform.translate(
                          offset: const Offset(-20, 0),
                         child: childWidget(),
)

Complete Example

Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 8.0),
                      child: CheckboxListTile(
                        value: isChecked,
                        controlAffinity: ListTileControlAffinity.leading,
                        contentPadding: EdgeInsets.zero,
                        title: Transform.translate(
                          offset: const Offset(-20, 0),
                          child: RichText(
                            text: TextSpan(
                              text: S.of(context).iAgreeWith,
                              style: TextStyle(
                                  color: Theme.of(context).hintColor,
                                  fontWeight: FontWeight.w400),
                              children: <TextSpan>[
                                TextSpan(
                                  text: S.of(context).terms,
                                  style: TextStyle(
                                      decoration: TextDecoration.underline,
                                      color: Theme.of(context).hintColor,
                                      fontWeight: FontWeight.w400),
                                  recognizer: TapGestureRecognizer()
                                    ..onTap = () {
                                      // Single tapped.

                                      Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) => WebView(
                                              url:
                                                  ksportsCornerWebsitePolicyUrl,
                                              title: S.of(context).termsCap,
                                            ),
                                          ));
                                    },
                                ),
                                TextSpan(
                                    text: " " + S.of(context).and + " ",
                                    style: TextStyle(
                                        color: Theme.of(context).hintColor,
                                        fontWeight: FontWeight.w400)),
                                TextSpan(
                                  text: S.of(context).privacyPolicy,
                                  style: TextStyle(
                                      decoration: TextDecoration.underline,
                                      color: Theme.of(context).hintColor,
                                      //fontSize: 14.0.sp,
                                      fontWeight: FontWeight.w400),
                                  recognizer: TapGestureRecognizer()
                                    ..onTap = () {
                                      // Single tapped.

                                      Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) => WebView(
                                              url:
                                                  ksportsCornerWebsitePolicyUrl,
                                              title: S
                                                  .of(context)
                                                  .privacyPolicyCaps,
                                            ),
                                          ));
                                    },
                                ),
                              ],
                            ),
                          ),
                        ),
                        activeColor: Theme.of(context).hintColor,
                        checkColor: Theme.of(context).cursorColor,
                        onChanged: (value) {
                          isChecked = !isChecked;
                          setState(() {});
                        },
                      ),
                    ),

enter image description here

In CheckBoxListTile, it is probably difficult to achieve what you want. However, there is always a workaround for that, and this is using Row(). Also, we will using FlatButton() to get the same effect.

The catch here is, in FlatButton(), you just have to do the same thing as onChanged in Checkbox()

Solution

     bool _myBool = false;

     Container(
        child: FlatButton(
          // here toggle the bool value so that when you click 
          // on the whole item, it will reflect changes in Checkbox
          onPressed: () => setState(() => _myBool = !_myBool),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              SizedBox(
                height: 24.0,
                width: 24.0,
                child: Checkbox(
                  value: _myBool,
                  onChanged: (value){
                     setState(() => _myBool = value);
                  }
                )
              ),
              // You can play with the width to adjust your
              // desired spacing
              SizedBox(width: 10.0),
              Text("افزودن به فهرست پرکاربردها")
            ]
          )
        )
      )

Result

RESULTANT IMAGE

And if you want the Checkbox() to be at the right, you can just switch the places of Text() and Checkbox(). Rest will remain the same.

Try replacing the ListTileTheme with this:

Padding(
  padding: EdgeInsets.only(right: 20.0),
  child: Row(
    mainAxisAlignment: MainAxisAlignment.end,
    children: <Widget>[
      Text(
        "افزودن به فهرست پرکاربردها"
      ),
      SizedBox(
        width: /* The spacing in between the checkbox and text */ 20,
      ),
      Checkbox(
        value: false,
        onChanged: (value) {},
      ),
    ],
  ),
),
Related