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

版权声明:本文为博主Pillar原创文章,欢迎转载并使用博客阅读器阅读 http://fir.im/CSDNBlog https://blog.csdn.net/Pillar1066527881/article/details/89096205

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: (){},
);

猜你喜欢

转载自blog.csdn.net/Pillar1066527881/article/details/89096205