Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Padding inside checkbox flutter

Want to more space between checkmark and checkbox

code

 Transform.scale(
                  scale: 1.3,
                  child: Checkbox(
                    checkColor: AppColors.buttonColor,
                  activeColor: AppColors.textWhiteColor,
                      side: MaterialStateBorderSide.resolveWith(
                        (states) => BorderSide(
                            width: 1.0, color: AppColors.unSelectedColor),
                      ),
                      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                      value: isCheck,
                      onChanged: (value) {
                        setState(() {
                          isCheck = value!;
                        });
                      }),
                ),

when checkbox active border color blue and padding between checkmark and checkbox border

like this
enter image description here


2 Answers

Please check below source for custom checkbox. This will fulfill your requirement.

Source:

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class CustomCheckBox extends StatefulWidget {
  final bool value;
  final ValueChanged<bool> onChanged;
  final Color checkedIconColor;
  final Color checkedFillColor;
  final IconData checkedIcon;
  final Color uncheckedIconColor;
  final Color uncheckedFillColor;
  final IconData uncheckedIcon;
  final double? borderWidth;
  final double? checkBoxSize;
  final bool shouldShowBorder;
  final Color? borderColor;
  final double? borderRadius;
  final double? splashRadius;
  final Color? splashColor;
  final String? tooltip;
  final MouseCursor? mouseCursors;

  const CustomCheckBox({
    Key? key,
    required this.value,
    required this.onChanged,
    this.checkedIconColor = Colors.white,
    this.checkedFillColor = Colors.teal,
    this.checkedIcon = Icons.check,
    this.uncheckedIconColor = Colors.white,
    this.uncheckedFillColor = Colors.white,
    this.uncheckedIcon = Icons.close,
    this.borderWidth,
    this.checkBoxSize,
    this.shouldShowBorder = false,
    this.borderColor,
    this.borderRadius,
    this.splashRadius,
    this.splashColor,
    this.tooltip,
    this.mouseCursors,
  }) : super(key: key);

  @override
  _CustomCheckBoxState createState() => _CustomCheckBoxState();
}

class _CustomCheckBoxState extends State<CustomCheckBox> {
  late bool _checked;
  late CheckStatus _status;

  @override
  void initState() {
    super.initState();
    _init();
  }

  @override
  void didUpdateWidget(CustomCheckBox oldWidget) {
    super.didUpdateWidget(oldWidget);
    _init();
  }

  void _init() {
    _checked = widget.value;
    if (_checked) {
      _status = CheckStatus.checked;
    } else {
      _status = CheckStatus.unchecked;
    }
  }

  Widget _buildIcon() {
    late Color fillColor;
    late Color iconColor;
    late IconData iconData;

    switch (_status) {
      case CheckStatus.checked:
        fillColor = widget.checkedFillColor;
        iconColor = widget.checkedIconColor;
        iconData = widget.checkedIcon;
        break;
      case CheckStatus.unchecked:
        fillColor = widget.uncheckedFillColor;
        iconColor = widget.uncheckedIconColor;
        iconData = widget.uncheckedIcon;
        break;
    }

    return Container(
      padding: EdgeInsets.zero,
      decoration: BoxDecoration(
        color: fillColor,
        borderRadius: BorderRadius.all(Radius.circular(widget.borderRadius ?? 6)),
        border: Border.all(
          color: widget.shouldShowBorder ? (widget.borderColor ?? Colors.teal.withOpacity(0.6)) : (!widget.value ? (widget.borderColor ?? Colors.teal.withOpacity(0.6)) : Colors.transparent),
          width: widget.shouldShowBorder ? widget.borderWidth ?? 2.0 : 1.0,
        ),
      ),
      child: Icon(
        iconData,
        color: iconColor,
        size: widget.checkBoxSize ?? 18,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: _buildIcon(),
      onPressed: () => widget.onChanged(!_checked),
      splashRadius: widget.splashRadius,
      splashColor: widget.splashColor,
      tooltip: widget.tooltip,
      mouseCursor: widget.mouseCursors ?? SystemMouseCursors.click,
    );
  }
}

enum CheckStatus {
  checked,
  unchecked,
}

Link: https://github.com/DipakShrestha-ADS/custom_check_box/blob/master/lib/custom_check_box.dart

like image 172
ABV Avatar answered Sep 10 '25 02:09

ABV


As far as I know, there is no official way to add content padding or reduce the size of the checkmark inside the checkbox.

What you can do though, is setting the stroke color to the color of your checkbox background, and then increasing the stroke width, and finally reducing the size of the checkbox entirely using transform.

Transform.scale(
  scale: 0.8,
  child: Checkbox(
    activeColor: theme,
    splashRadius: 0.0,
    shape: RoundedRectangleBorder(
       borderRadius: BorderRadius.circular(4.0),
    ),
    side: MaterialStateBorderSide.resolveWith(
       (states) => BorderSide(
            width: 6.0,
            color: controller.borderClr.value,
            strokeAlign:
                BorderSide.strokeAlignCenter,
                   ),
    ),
    value: true,
  ),
)

or, create a custom checkbox widget as @ABV suggested.

like image 20
Mihir Avatar answered Sep 10 '25 00:09

Mihir