Flutter 自定义SwitchButton

该文章展示了一个在AppBar中使用的自定义SwitchButton的实现,包括两种状态的颜色样式和点击事件处理。通过HomeSwitchButton类定义了开关按钮,根据pressed状态改变文字颜色和背景装饰,并在onTap回调中处理点击事件。
摘要由CSDN通过智能技术生成

效果:

(AppBar中间的SwitchButton)
请添加图片描述

代码:


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

typedef SingleCallback<int> = void Function(int obj);

class HomeSwitchButton extends StatefulWidget {

  final String? onString;
  final String? offString;
  final bool pressed;
  final SingleCallback? onTap;

  HomeSwitchButton({
    Key? key,
    required this.onString,
    required this.offString,
    required this.pressed,
    required this.onTap
  });

  
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return HomeSwitchButtonState();
  }

}

class HomeSwitchButtonState extends State<HomeSwitchButton> {

  
  Widget build(BuildContext context) {
    // TODO: implement build
    return _switchButton();
  }

  Widget _switchButton() {
    BoxDecoration BoxDecoration_left1 = BoxDecoration(
        color: Colors.pinkAccent,
        border: Border.all(
            width: 0.5, style: BorderStyle.solid, color: Colors.white),
        borderRadius: BorderRadius.circular(20));
    BoxDecoration BoxDecoration_left2 = BoxDecoration(
        color: Colors.white,
        border: Border.all(
            width: 0.5, style: BorderStyle.solid, color: Colors.white),
        borderRadius: BorderRadius.circular(20));
    BoxDecoration BoxDecoration_right1 = BoxDecoration(
        color: Colors.white,
        border: Border.all(
            width: 0.5, style: BorderStyle.solid, color: Colors.white),
        borderRadius: BorderRadius.circular(20));
    BoxDecoration BoxDecoration_right2 = BoxDecoration(
        color: Colors.pinkAccent,
        border: Border.all(
            width: 0.5, style: BorderStyle.solid, color: Colors.white),
        borderRadius: BorderRadius.circular(20));

    TextStyle textStyle = const TextStyle(fontSize: 12.0, color: Colors.white);
    TextStyle textStyle1 = const TextStyle(fontSize: 12.0, color: Color.fromARGB(255, 83, 81, 81));

    return Container(
      width: 100,
      height: 28,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
      ),
      child: Row(
        children: <Widget>[
          Container(
            width: 50,
            height: 28,
            alignment: Alignment.center,
            padding: const EdgeInsets.only(top: 3, bottom: 3, right: 3, left: 3),
            decoration: widget.pressed ? BoxDecoration_left1 : BoxDecoration_left2,
            child: GestureDetector(
                child: Text(widget.onString ?? "", style: widget.pressed ? textStyle : textStyle1, ),
                onTap: () {
                  if (widget.onTap != null){
                    widget.onTap!(0);
                  }
                }),
          ),
          Container(
            width: 50,
            height: 28,
            alignment: Alignment.center,
            padding: const EdgeInsets.only(top: 3, bottom: 3, right: 3, left: 3),
            decoration: widget.pressed ? BoxDecoration_right1 : BoxDecoration_right2,
            child: GestureDetector(
                child: Text(widget.offString ?? "", style: widget.pressed ? textStyle1 : textStyle),
                onTap: () {
                  if (widget.onTap != null){
                    widget.onTap!(1);
                  }
                }),
          ),
        ],
      ),
    );
  }

}

使用:

HomeSwitchButton(onString: "推荐", offString: "关注", pressed: _pressed, onTap: (obj){
	setState(() {
		print(obj);
	});
}),
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值