flutter 自定义图片实现Radio和CheckBox

 自定义实现Radio和CheckBox,利用图片实现更换选中和不选择的状态,以下代码是关键代码

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

class RadioOption<T> extends StatelessWidget {
  final T value;
  final T? groupValue;
  final String selectImage;
  final String unSelectImage;
  final String text;
  final ValueChanged<T?> onChanged;

  const RadioOption({
    required this.value,
    required this.groupValue,
    required this.text,
    required this.selectImage,
    required this.unSelectImage,
    required this.onChanged,
  });

  Widget _buildLabel() {
    final bool isSelected = value == groupValue;
    return SizedBox(
      width: 30,
      height: 30,
      child: SvgPicture.asset(isSelected ? selectImage : unSelectImage),
    );
  }

  Widget _buildText() {
    return Text(
      text,
      style: const TextStyle(color: Colors.black, fontSize: 24),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(8),
      child: InkWell(
        onTap: () => onChanged(value),
        child: Padding(
          padding: const EdgeInsets.all(5),
          child: Row(
            children: [
              _buildLabel(),
              const SizedBox(width: 10),
              _buildText(),
            ],
          ),
        ),
      ),
    );
  }
}

class CheckBoxOption extends StatelessWidget {
  final bool value;
  final bool autofocus;
  final String selectImage;
  final String unSelectImage;
  final String text;
  final ValueChanged onChanged;

  const CheckBoxOption({
    required this.value,
    required this.text,
    required this.selectImage,
    required this.unSelectImage,
    required this.onChanged,
    this.autofocus = false,
  });

  Widget _buildLabel() {
    final bool isSelected = value;
    return SizedBox(
      width: 30,
      height: 30,
      child: SvgPicture.asset(isSelected ? selectImage : unSelectImage),
    );
  }

  Widget _buildText() {
    return Text(
      text,
      style: const TextStyle(color: Colors.black, fontSize: 24),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(8),
      child: InkWell(
        onTap: () => onChanged(value),
        child: Padding(
          padding: const EdgeInsets.all(5),
          child: Row(
            children: [
              _buildLabel(),
              const SizedBox(width: 10),
              _buildText(),
            ],
          ),
        ),
      ),
    );
  }
}

具体使用方式

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

class SettingView extends StatefulWidget {
  // const SettingView({Key? key}) : super(key: key);
  late int defaultValue = 1;

  @override
  State<SettingView> createState() => _GridViewItemState();
}

class _GridViewItemState extends State<SettingView> {
  // int defaultValue = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("AAAA"),
        ),
        body: Column(
          children: [
            RadioOption(
              value: 0,
              groupValue: widget.defaultValue,
              unSelectImage: "images/单选_未选中.svg",
              selectImage: "images/单选_复选.svg",
              text: "你的",
              onChanged: _onChange,
            ),
            RadioOption(
              value: 1,
              groupValue: widget.defaultValue,
              unSelectImage: "images/单选_未选中.svg",
              selectImage: "images/单选_复选.svg",
              text: "我的",
              onChanged: _onChange,
            ),
            CheckBoxOption(
              value: false,
              unSelectImage: "images/单选_未选中.svg",
              selectImage: "images/单选_复选.svg",
              text: "我的1",
              onChanged: (value) {},
            ),
            CheckBoxOption(
              value: true,
              unSelectImage: "images/单选_未选中.svg",
              selectImage: "images/单选_复选.svg",
              text: "我的2",
              onChanged: (value) {},
            ),
            CheckBoxOption(
              value: false,
              unSelectImage: "images/单选_未选中.svg",
              selectImage: "images/单选_复选.svg",
              text: "我的3",
              onChanged: (value) {},
            ),
            CheckBoxOption(
              value: false,
              unSelectImage: "images/单选_未选中.svg",
              selectImage: "images/单选_复选.svg",
              text: "我的4",
              onChanged: (value) {},
            ),
          ],
        ));
  }

  void _onChange(value) {
    if (mounted) {
      setState(() {
        widget.defaultValue = value;
      });
    }
  }
}

具体效果如下

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落后程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值