flutter14 - 按钮

Flutter14 - 按钮

Material组件库中的按钮

RaisedButton 漂浮按钮,默认有阴影和背景

FlatButton扁平按钮 默认背景透明不带阴影

OutlineButton 边框按钮,默认有一个边框

IconButton 一个可点击的icon,不包括文字,默认没有背景

带图标的按钮

RaisedButtonFlatButtonOutlineButton都有一个icon 构造函数,通过它可以轻松创建带图标的按钮,
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Text",
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('文本及其样式'),
        ),
        body: TextDemo(),
      ),
    );
  }
}

class TextDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          RaisedButton(
            child: Text("RaisedButton 漂浮按钮,默认有阴影和背景"),
            onPressed: () {
              print("点击了RaisedButton漂浮按钮");
            },
          ),
          FlatButton(
            child: Text("FlatButton扁平按钮 默认背景透明不带阴影"),
            onPressed: () {
              print("点击了FlatButton扁平按钮");
            },
          ),
          OutlineButton(
            child: Text("OutlineButton 边框按钮,默认有一个边框"),
            onPressed: () {
              print("点击了OutlineButton 边框按钮");
            },
          ),
          IconButton(
            icon: Icon(Icons.access_time),
            onPressed: () {},
          ),
          RaisedButton.icon(
            icon: Icon(Icons.access_time),
            label: Text("RaisedButton.icon"),
            onPressed: () {
              print("点击了RaisedButton漂浮按钮");
            },
          ),
          FlatButton.icon(
            icon: Icon(Icons.access_time),
            label: Text("FlatButton.icon"),
            onPressed: () {
              print("点击了FlatButton扁平按钮");
            },
          ),
          OutlineButton.icon(
            icon: Icon(Icons.access_time),
            label: Text("OutlineButton.icon"),
            onPressed: () {
              print("点击了OutlineButton 边框按钮");
            },
          ),
        ],
      ),
    );
  }
}

自定义按钮外观

const FlatButton({
  ...  
  @required this.onPressed, //按钮点击回调
  this.textColor, //按钮文字颜色
  this.disabledTextColor, //按钮禁用时的文字颜色
  this.color, //按钮背景颜色
  this.disabledColor,//按钮禁用时的背景颜色
  this.highlightColor, //按钮按下时的背景颜色
  this.splashColor, //点击时,水波动画中水波的颜色
  this.colorBrightness,//按钮主题,默认是浅色主题 
  this.padding, //按钮的填充
  this.shape, //外形
  @required this.child, //按钮的内容
})
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Text",
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('自定义按钮'),
        ),
        body: TextDemo(),
      ),
    );
  }
}

class TextDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          FlatButton(
            color: Colors.blue,
            //按钮背景颜色
            highlightColor: Colors.blue[700],
            //按钮按下时的背景颜色
            child: Text('Submit'),
            //子控件
            splashColor: Colors.grey,
            colorBrightness: Brightness.dark,
            //按钮主题,默认是浅色
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20.0)),
            onPressed: () {},
          )
        ],
      ),
    );
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

朱良浩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值