Flutter自定义按钮插件nice_buttons的使用
Flutter自定义按钮插件nice_buttons的使用
nice_buttons
是一个为您的Flutter项目提供的美丽、动画且可定制的3D按钮插件。
平台支持
Android | iOS | Web | MacOS | Linux | Windows |
---|---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
安装
在 pubspec.yaml
文件中添加依赖:
dependencies:
nice_buttons: ^0.0.3
自定义属性
属性名 | 类型 | 默认值 | 描述 |
---|---|---|---|
startColor | Color | 0xFF2ec8ff | 渐变起始颜色 |
endColor | Color | 0xFF529fff | 渐变结束颜色 |
borderColor | Color | 0xFF3489e9 | 3D边框的颜色 |
progressColor | Color | white | 圆形进度指示器的颜色,默认为白色 |
progressSize | Double | 20 | 进度指示器圆的大小,默认为20 |
gradientOrientation | GradientOrientation | Horizontal | 渐变方向,默认为水平方向 |
borderThickness | Double | 5 | 3D边框的厚度,默认为5 |
height | Double | 60 | 按钮的高度,默认为60 |
width | Double | 200 | 按钮的宽度,默认为200,如果设置了stretch属性为true,则此属性无效 |
stretch | bool | true | 是否占据父容器的全部可用空间,默认为true |
borderRadius | Double | 20 | 按钮的圆角半径,默认为20 |
progress | bool | false | 是否显示进度指示器,默认为false |
disabled | bool | false | 是否禁用按钮,默认为false |
onTap | Function | - | 按钮点击事件处理器,必填项 |
child | Widget | - | 按钮内部的内容,必填项 |
使用示例
导入包
import 'package:nice_buttons/nice_buttons.dart';
示例代码
以下是一个完整的示例代码,展示了如何使用 nice_buttons
插件创建不同样式的按钮。
import 'package:flutter/material.dart';
import 'package:nice_buttons/nice_buttons.dart';
import 'dart:async';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Nice Buttons Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title!),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
NiceButtons(
stretch: false,
progress: true,
gradientOrientation: GradientOrientation.Horizontal,
onTap: (finish) {
print('On tap called');
Timer(Duration(seconds: 5), () {
finish();
});
},
child: Text(
'With Progress',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
Container(width: double.infinity, height: 30),
NiceButtons(
stretch: false,
gradientOrientation: GradientOrientation.Horizontal,
onTap: (finish) {
print('On tap called');
},
child: Text(
'Nice Buttons',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
Container(width: double.infinity, height: 30),
NiceButtons(
stretch: true,
gradientOrientation: GradientOrientation.Horizontal,
onTap: (finish) {
print('On tap called');
},
child: Text(
'Full Width',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
Container(width: double.infinity, height: 30),
NiceButtons(
stretch: false,
borderRadius: 30,
gradientOrientation: GradientOrientation.Horizontal,
onTap: (finish) {
print('On tap called');
},
child: Text(
'3D Pill',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
Container(width: double.infinity, height: 30),
NiceButtons(
stretch: false,
gradientOrientation: GradientOrientation.Vertical,
onTap: (finish) {
print('On tap called');
},
child: Text(
'Vertical Gradient',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
],
),
),
);
}
}
这个示例展示了如何创建带有进度条的按钮、普通按钮、全宽按钮、3D药丸形状按钮和垂直渐变按钮。您可以根据需要调整这些按钮的属性,以满足您的设计需求。
更多关于Flutter自定义按钮插件nice_buttons的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter自定义按钮插件nice_buttons的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter中使用自定义按钮插件nice_buttons
的示例代码。首先,确保你已经在pubspec.yaml
文件中添加了该插件的依赖项:
dependencies:
flutter:
sdk: flutter
nice_buttons: ^latest_version # 请替换为最新版本号
然后,运行flutter pub get
来获取该插件。
下面是一个简单的示例,展示了如何使用nice_buttons
插件来创建自定义按钮:
import 'package:flutter/material.dart';
import 'package:nice_buttons/nice_buttons.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Nice Buttons Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Nice Buttons Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// 使用NiceButton创建自定义按钮
NiceButton(
text: 'Primary Button',
onPressed: () {
// 按钮点击后的处理逻辑
print('Primary Button Clicked');
},
color: Colors.blue,
textColor: Colors.white,
borderRadius: 25.0,
width: 200.0,
height: 50.0,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
SizedBox(height: 20.0),
// 使用NiceButton.builder进行更高级的自定义
NiceButton.builder()
.text('Gradient Button')
.onPressed(() {
print('Gradient Button Clicked');
})
.gradient(
LinearGradient(
colors: [Colors.red, Colors.yellow],
),
)
.borderRadius(25.0)
.width(200.0)
.height(50.0)
.fontSize(18.0)
.fontWeight(FontWeight.bold)
.build(),
SizedBox(height: 20.0),
// 使用NiceOutlineButton创建自定义轮廓按钮
NiceOutlineButton(
text: 'Outline Button',
onPressed: () {
print('Outline Button Clicked');
},
borderColor: Colors.blue,
textColor: Colors.blue,
borderRadius: 25.0,
width: 200.0,
height: 50.0,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
],
),
),
);
}
}
在这个示例中,我们展示了三种类型的按钮:
- 使用
NiceButton
创建一个具有指定颜色和文字的按钮。 - 使用
NiceButton.builder()
进行更高级的自定义,比如创建一个具有渐变颜色的按钮。 - 使用
NiceOutlineButton
创建一个具有指定边框和文字颜色的轮廓按钮。
你可以根据需要调整这些按钮的属性,例如颜色、文字颜色、边框半径、大小等,以创建符合你需求的自定义按钮。