我找到了多個關于如何使用具有漸變背景的 RaiseButton 的堆疊溢位示例,但是自從發布這些答案以來,RaiseButton 已被棄用。
我為具有漸變背景的 ElevatedButton 找到的大多數解決方案在某種程度上都不完整;要么漸變沒有覆寫整個背景,要么陰影關閉。

下面是我的可重用漸變按鈕代碼。
import 'package:flutter/material.dart';
class GradientElevatedButton extends StatelessWidget {
const GradientElevatedButton({
Key? key,
required this.child,
required this.onPressed,
required this.linearGradient,
}) : super(key: key);
final Widget child;
final VoidCallback onPressed;
final LinearGradient linearGradient;
@override
Widget build(BuildContext context) {
return Padding(
// ElevatedButton has default 5 padding on top and bottom
padding: const EdgeInsets.symmetric(
vertical: 5,
),
// DecoratedBox contains our linear gradient
child: DecoratedBox(
decoration: BoxDecoration(
gradient: linearGradient,
// Round the DecoratedBox to match ElevatedButton
borderRadius: BorderRadius.circular(5),
),
child: ElevatedButton(
onPressed: onPressed,
// Duplicate the default styling of an ElevatedButton
style: ElevatedButton.styleFrom(
// Enables us to see the BoxDecoration behind the ElevatedButton
primary: Colors.transparent,
// Fits the Ink in the BoxDecoration
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
).merge(
ButtonStyle(
// Elevation declared here so we can cover onPress elevation
// Declaring in styleFrom does not allow for MaterialStateProperty
elevation: MaterialStateProperty.all(0),
),
),
child: child,
),
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364070.html
上一篇:反射增強賦值有什么神奇的方法嗎?
