美文网首页
flutter 选中按钮自定义

flutter 选中按钮自定义

作者: 大宝来巡山 | 来源:发表于2022-01-10 11:30 被阅读0次
    import 'dart:ffi';
    
    import 'package:flutter/material.dart';
    
    class setting extends StatefulWidget {
    
      @override
    
      _settingStatecreateState() =>_settingState();
    
    }
    
    class_settingStateextendsState {
    
      @override
    
      Widget build(BuildContext context) {
    
        return Scaffold(
    
            body:Container(
    
          width:double.infinity,
    
          child:Column(
    
              mainAxisAlignment:MainAxisAlignment.center,
    
              crossAxisAlignment:CrossAxisAlignment.center,
    
              children: [
    
                customButton(
    
                  '状态按钮',
    
                  normalColor:Colors.white,
    
                  normalTextColor:Colors.black,
    
                  selectedColor:Colors.red,
    
                  borderWidth:1,
    
                  normalBorderColor:Colors.grey,
    
                  selectedBorderColor:Colors.blue,
    
                  selected:false,
    
                  touchDown: (boolselected) {
    
                    if(selected) {
    
                      returnfalse;
    
                    }else{
    
                      returntrue;
    
                    }
    
                  },
    
                )
    
              ]),
    
        ));
    
      }
    
    }
    
    classcustomButtonextendsStatefulWidget{
    
      boolselected =false;
    
      doubleborderWidth;
    
      ColornormalColor =Colors.white;
    
      StringnormalText ='';
    
      ColornormalTextColor =Colors.black;
    
      ColornormalBorderColor =Colors.black;
    
      ColorselectedColor;
    
      StringselectedText ='';
    
      ColorselectedTextColor;
    
      ColorselectedBorderColor;
    
      FunctiontouchDown;
    
      customButton(this.normalText,
    
          {this.normalColor,
    
          this.normalTextColor,
    
          this.normalBorderColor,
    
          this.selectedColor,
    
          this.selectedText,
    
          this.selectedTextColor,
    
          this.selectedBorderColor,
    
          this.selected ,
    
          this.touchDown,
    
          this.borderWidth =0}) {
    
        if(this.selectedColor ==null) {
    
          this.selectedColor =this.normalColor;
    
        }
    
        if(this.selectedText ==null) {
    
          this.selectedText =this.normalText;
    
        }
    
        if(this.selectedTextColor ==null) {
    
          this.selectedTextColor =this.normalTextColor;
    
        }
    
        if(this.selectedBorderColor ==null) {
    
          this.selectedBorderColor =this.normalBorderColor;
    
        }
    
      }
    
      @override
    
      _customButtonStatecreateState() =>_customButtonState(this.normalText,
    
          normalColor:this.normalColor,
    
          normalTextColor:this.normalTextColor,
    
          normalBorderColor:this.normalBorderColor,
    
          selectedColor:this.selectedColor,
    
          selectedText:this.selectedText,
    
          selectedTextColor:this.selectedTextColor,
    
          selected:this.selected,
    
          selectedBorderColor:this.selectedBorderColor,
    
          touchDown:this.touchDown,
    
          borderWidth:this.borderWidth);
    
    }
    
    class_customButtonStateextendsState {
    
      boolselected;
    
      ColornormalColor;
    
      StringnormalText;
    
      ColornormalTextColor;
    
      ColornormalBorderColor;
    
      ColorselectedColor;
    
      StringselectedText;
    
      ColorselectedTextColor;
    
      ColorselectedBorderColor;
    
      doubleborderWidth;
    
      FunctiontouchDown;
    
      _customButtonState(this.normalText,
    
          {this.normalColor,
    
          this.normalTextColor,
    
          this.normalBorderColor,
    
          this.selectedColor,
    
          this.selectedText,
    
          this.selectedTextColor,
    
          this.selectedBorderColor,
    
          this.selected,
    
          this.touchDown,
    
          this.borderWidth}) {}
    
      @override
    
      Widget build(BuildContext context) {
    
        return  RaisedButton(
    
            autofocus:true,
    
            color:this.selected ?this.selectedColor :this.normalColor,
    
            shape:RoundedRectangleBorder(
    
                borderRadius:BorderRadius.all(Radius.circular(1)),
    
                side:BorderSide(
    
                    color:this.selected
    
                        ?this.selectedBorderColor
    
                        :this.normalBorderColor,
    
                    width:this.borderWidth,
    
                    style:BorderStyle.solid)),
    
            onPressed: () {
    
              boolvalue =this.touchDown(this.selected);
    
              if(value !=this.selected) {
    
                setState(() {
    
                  this.selected = value;
    
                });
    
              }
    
            },
    
            child:Container(
    
                child:Row(       
    
                  mainAxisSize:MainAxisSize.min,       
    
                  mainAxisAlignment:MainAxisAlignment.center,
    
                  crossAxisAlignment:CrossAxisAlignment.center,
    
                  children: [
    
              Text(
    
                this.selected ?this.selectedText :this.normalText,
    
                style:TextStyle(
    
                    color:this.selected
    
                        ?this.selectedTextColor
    
                        :this.normalTextColor),
    
              ),
    
              SizedBox(width:30),
    
              Text(
    
                this.selected ?this.selectedText :this.normalText,
    
                style:TextStyle(
    
                    color:this.selected
    
                        ?this.selectedTextColor
    
                        :this.normalTextColor),
    
              ),
    
            ]
    
            ),
    
          ),
    
        );
    
      }
    
    }
    

    参考链接:https://www.jianshu.com/p/452ecdb3d419

    相关文章

      网友评论

          本文标题:flutter 选中按钮自定义

          本文链接:https://www.haomeiwen.com/subject/hcsmcrtx.html