Flutter 是一种流行的跨平台移动应用开发框架。Flutter 提供了许多内置的小部件,但有时您可能需要创建自己的小部件以满足特定的需求。这个文档将介绍如何创建一个自定义渐变按钮小部件 GradientButton。
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
class GradientButton extends StatelessWidget {
// TODO: Add button properties and constructor here
}
class GradientButton extends StatelessWidget {
final String text;
final double width;
final double height;
final Function onPressed;
GradientButton({
required this.text,
required this.width,
required this.height,
required this.onPressed,
});
@override
Widget build(BuildContext context) {
// TODO: Add button widget here
}
}
class GradientButton extends StatelessWidget {
final String text;
final double width;
final double height;
final Function onPressed;
GradientButton({
required this.text,
required this.width,
required this.height,
required this.onPressed,
});
@override
Widget build(BuildContext context) {
return Container(
width: width,
height: height,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.red, Colors.orange],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(30.0),
),
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: onPressed as void Function()?,
child: Center(
child: Text(
text,
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
);
}
}
现在,我们已经创建了一个自定义渐变按钮小部件 GradientButton,您可以在 Flutter 应用程序中使用它。这个按钮看起来很好,可以使您的应用程序更加吸引人。