我正在try 创建一个类似于这个按钮,我没有确切的 colored颜色 ,所以使用黄色和黑色.

Want this

enter image description here

My Code Output

enter image description here

以下是我的代码:

class CustomButton extends StatelessWidget {
  final String? text;
  double width;
  final Function()? onPressed;
  CustomButton({this.width = 0.8, this.text, this.onPressed});
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPressed,
      child: Container(
        child: Center(
            child: CustomTextWidget(
          text: text!,
          textColor: AppColors.BLACK_COLOR,
          fontWeight: FontWeight.bold,
          fontSize: 1.1,
        )),
        height: ScreenSize.heightSize * 0.08,
        width: width.sw,
        decoration: BoxDecoration(
            gradient: const LinearGradient(
              colors: [Colors.yellow, Colors.black],
              begin: Alignment.topRight,
              end: Alignment.topRight,
              stops: [0.7, 0.8],
              tileMode: TileMode.repeated,
            ),
            borderRadius: BorderRadius.circular(4)),
      ),
    );
  }
}

请帮助我做这件事.

推荐答案

try 下面的代码,希望它能对你有所帮助,并用了FractionalOffset

GestureDetector(
  onTap: () {},
  child: Container(
    alignment: Alignment.center,
    height: 44.0,
    width: 100,
    decoration: const BoxDecoration(
      gradient: LinearGradient(
        colors: [
          Colors.yellow,
          Colors.black,
        ],
        begin: FractionalOffset.bottomCenter,
        end: FractionalOffset.topCenter,
      ),
    ),
    child: const Text('SIGN UP'),
  ),
),

Result-> image

Flutter相关问答推荐

Flutter bloc -如何使用BlocBuilder?

如何在凌晨12点显示00:00(24小时格式)而不是在Flutter 中显示24:00

下拉图标未居中

Flutter 蜂窝-检索非基元数据类型列表

将CLOUD_FIRESTORE程序包更新到版本4.13.0(到Windows)时出现问题

点击表情符号按钮后,Flutter 文本域文本编辑控制器将文本恢复为原始值

StateNotifer正在riverpod中提供初始状态值(bool)

如何在Flutter 中使用选定的键和值从 map 列表创建 map

如何在flutter中将所有单词的第一个字母大写

如何获取flutter中gridview项目占用的高度和宽度?

如何计算 Firestore 集合中的文档数量?

如何从另一个页面访问 Firebase 值?

Flutter sliver 持久化

String类型不是Widget类型的子类型?在dart 中

如何在 flutter 中过滤 DateTime(intl)?

扫描flutter生成的二维码后如何弹出文本字段值

Flutter 平台通道将多数据传递给 windows 功能

在 Flutter 应用程序中全局使用 assets 文件夹

如何验证位于 PageView 内不同页面的 TextFormFields

单击列表视图导航到 Flutter 中的另一个页面