Flutter学习之自定义组合控件法封装具有项目特色的统一样式的按钮

1、自定义具有统一样式的按钮控件

import 'package:flutter/material.dart';

/*
* 自定义适合项目特色的统一样式按钮
*/
class DefaultButton extends StatefulWidget {
  //点击回调
  final GestureTapCallback onPressed;
  final String text;
  final EdgeInsetsGeometry margin;
  EdgeInsetsGeometry marginDefault =
      EdgeInsets.fromLTRB(0, 90.0, 0, 30); //按钮默认的margin值

  DefaultButton({this.onPressed, this.text, this.margin});

  @override
  State createState() {
    if (this.margin == null) {
      return _DefaultButtonState(this.onPressed, this.text, marginDefault);
    }
    return _DefaultButtonState(this.onPressed, this.text, this.margin);
  }
}

class _DefaultButtonState extends State<DefaultButton> {
  //点击回调
  final GestureTapCallback onPressed;
  final String text;
  final EdgeInsetsGeometry margin;

  _DefaultButtonState(this.onPressed, this.text, this.margin);

  @override
  Widget build(BuildContext context) {
    Widget _SectionBtn = new Container(
      margin: this.margin,
      child: new Container(
        width: 200,
        child: RaisedButton(
          color: Colors.pink,
          highlightColor: Colors.pink[700],
          colorBrightness: Brightness.dark,
          splashColor: Colors.grey,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
          child: Text(text ?? ""),
          textColor: Colors.white,
          onPressed: onPressed,
        ),
      ),
    );

    return _SectionBtn;
  }
}

2、使用方法:
Widget _SectionSave = DefaultButton(
margin: EdgeInsets.fromLTRB(0, 30.0, 0, 0),//可选配置,自定义控件中有默认配置
text: “保存”,
onPressed: (){},
);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值