import 'package:flutter/material.dart';

//CheckBox控件封装
class AppCheckbox extends StatefulWidget {
  final String? label;
  final bool? checked;
  final TextStyle? labelStyle;
  final Function(bool?)? onChecked;

  AppCheckbox({this.label, this.checked, this.labelStyle, this.onChecked});

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

class _AppCheckboxState extends State<AppCheckbox> {
  bool? _checked = false;

  void _onChecked(bool? checked) {
    setState(() {
      _checked = checked;
    });

    if (widget.onChecked != null) {
      widget.onChecked!(checked);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(
            width: 24.0,
            height: 24.0,
            child: Checkbox(value: _checked, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, onChanged: _onChecked),
          ),
          const SizedBox(width: 10),
          Flexible(
            child: Text(
              widget.label!,
              textAlign: TextAlign.left,
              style: ( widget.labelStyle!=null )? widget.labelStyle: const TextStyle(
                fontSize: 13.0,
                color: Colors.white,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
