需要将长宽和颜色进行封装
时间: 2024-02-23 09:00:17 浏览: 24
好的,那我对上面的代码进行修改,将长宽和颜色进行封装,你可以参考以下的代码:
```
import 'package:flutter/material.dart';
class GradientButton extends StatelessWidget {
final String text;
final double width;
final double height;
final Color startColor;
final Color endColor;
final VoidCallback onPressed;
GradientButton({
@required this.text,
this.width = 200.0,
this.height = 50.0,
this.startColor = Colors.blue,
this.endColor = Colors.purple,
@required this.onPressed,
});
@override
Widget build(BuildContext context) {
return Container(
width: width,
height: height,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [startColor, endColor],
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(height / 2),
),
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,
),
),
),
),
),
);
}
}
```
在这个版本的代码中,我添加了width、height、startColor、endColor等属性,这些属性可以在使用该组件时进行设置,也提供了默认值。这样,我们就可以在多个地方使用这个按钮,而不用每次都手动设置这些属性值了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)