Flutter 中圆角按钮,渐变色按钮

如图所示

在这里插入图片描述

思路

ElevatedButton组件,我在网上没有找到直接设置的圆角和渐变色背景的属性。笔者的思路是,将按钮的背景色和阴影去除,在其外包裹Container,设置外层容器的圆角和背景色即可。

代码

Container(
   width: 340,
   height: 49,
   //在此设置
   decoration: BoxDecoration(
     borderRadius: BorderRadius.circular(9),
     gradient: const LinearGradient(colors: [
       Color(0xffde53fc),
       Color(0xff846dfc),
       Color(0xff30c1fd),
     ]),
   ),
   child: ElevatedButton(
     onPressed: onTap,
     child: const Text(
       '立即存款',
       style: TextStyle(fontSize: 17, color: Colors.white),
     ),
     style: ButtonStyle(
       //去除阴影
       elevation: MaterialStateProperty.all(0),
       //将按钮背景设置为透明
       backgroundColor: MaterialStateProperty.all(Colors.transparent),
     ),
   ),
 )
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现一个立体的彩色渐变按钮,可以按照以下步骤进行: 1. 创建一个自定义的按钮控件,可以继承自FlatButton或RaisedButton等现有的Flutter控件。 2. 在控件的build方法,使用Container包裹一个InkWell组件来实现按钮的点击效果。 3. 创建一个Gradient对象,用于定义按钮的颜色渐变效果。可以使用LinearGradient、RadialGradient或SweepGradient等不同的渐变方式。 4. 在Container的decoration属性,设置一个BoxDecoration对象,使用Gradient作为其color属性,实现彩色渐变的效果。 5. 为了实现立体的效果,可以在BoxDecoration设置boxShadow属性,定义按钮的阴影效果。 6. 最后,根据需要添加文字或图标等内容到按钮。 以下是一个简单的代码示例: ``` import 'package:flutter/material.dart'; class GradientButton extends StatelessWidget { final String text; final VoidCallback onPressed; GradientButton({@required this.text, @required this.onPressed}); @override Widget build(BuildContext context) { return Container( height: 50.0, decoration: BoxDecoration( gradient: LinearGradient( colors: [Colors.blue, Colors.purple], begin: Alignment.topLeft, end: Alignment.bottomRight, ), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 5, offset: Offset(0, 3), ), ], borderRadius: BorderRadius.circular(30.0), ), child: Material( color: Colors.transparent, child: InkWell( onTap: onPressed, child: Center( child: Text( text, style: TextStyle( color: Colors.white, fontSize: 18.0, fontWeight: FontWeight.bold, ), ), ), ), ), ); } } ``` 使用时,可以像使用普通的FlatButton或RaisedButton一样调用GradientButton控件,并传入需要的参数。例如: ``` GradientButton( text: '立体彩色渐变按钮', onPressed: () { print('点击了按钮!'); }, ), ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值