Flutter动画按钮插件morph_button的使用
Flutter 动画按钮插件 MorphButton 的使用
MorphButton
一个具有形状变换动画效果的按钮。
安装
将 morph_button
添加到你的 pubspec.yaml
文件中。
使用 Dart 包
dart pub add morph_button
使用 Git URL
dependencies:
morph_button:
git:
url: https://github.com/anoochit/morph_button.git
使用
以下是一个简单的示例代码:
MorphButton(
width: 200.0, // 按钮宽度
height: 200.0, // 按钮高度
pressedColor: Theme.of(context).colorScheme.primary, // 按钮按下的颜色
initialColor: Theme.of(context).colorScheme.primary.withOpacity(0.6), // 按钮初始颜色
initialRadius: 24.0, // 按钮初始圆角半径
title: const Text( // 按钮文本
'Click Me',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
onTap: () {}, // 按钮点击事件
);
示例代码
下面是一个完整的示例代码,展示了如何在 Flutter 应用中使用 MorphButton。
// ignore_for_file: deprecated_member_use
import 'package:flutter/material.dart';
import 'package:morph_button/morph_button_widget.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// 这个 widget 是应用的根节点。
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.deepPurple,
),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
child: GridView.builder(
shrinkWrap: true,
itemCount: 9,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 8.0,
crossAxisSpacing: 8.0,
),
itemBuilder: (context, index) {
return DialButton(
title: '${index + 1}',
);
},
),
),
),
);
}
}
class DialButton extends StatelessWidget {
const DialButton({
super.key,
required this.title,
});
final String title;
[@override](/user/override)
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
return MorphButton(
width: constraints.maxWidth, // 按钮宽度
height: constraints.maxWidth, // 按钮高度
pressedColor: Theme.of(context).colorScheme.primary, // 按钮按下的颜色
initialColor: Theme.of(context).colorScheme.inversePrimary, // 按钮初始颜色
initialRadius: 12.0, // 按钮初始圆角半径
title: Text(
title,
style: Theme.of(context)
.textTheme
.headlineLarge!
.apply(color: Colors.white),
),
onTap: () {}, // 按钮点击事件
);
});
}
}
更多关于Flutter动画按钮插件morph_button的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter动画按钮插件morph_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
morph_button
是一个 Flutter 插件,用于创建具有形状变换效果的按钮。它可以在不同的形状之间进行平滑的过渡,例如从圆形变为矩形,或者从矩形变为其他形状。这种动画效果可以增强用户体验,使界面更加生动。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 morph_button
插件的依赖:
dependencies:
flutter:
sdk: flutter
morph_button: ^0.0.1 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
基本用法
以下是一个简单的示例,展示如何使用 morph_button
插件创建一个可以在圆形和矩形之间变换的按钮。
import 'package:flutter/material.dart';
import 'package:morph_button/morph_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Morph Button Example'),
),
body: Center(
child: MorphButton(
initialShape: MorphShape.circle,
duration: Duration(milliseconds: 500),
child: Text('Click Me'),
onPressed: () {
print('Button Pressed!');
},
onMorph: (MorphShape shape) {
print('Button morphed to: $shape');
},
),
),
),
);
}
}
参数说明
initialShape
: 按钮的初始形状,可以是MorphShape.circle
或MorphShape.rectangle
。duration
: 形状变换的动画持续时间。child
: 按钮内部的内容,通常是一个Text
或Icon
。onPressed
: 按钮点击事件的回调函数。onMorph
: 形状变换完成后的回调函数,返回当前的形状。
高级用法
你还可以通过 MorphButton
的其他属性来自定义按钮的外观和行为,例如:
color
: 按钮的背景颜色。padding
: 按钮的内边距。borderRadius
: 矩形形状时的圆角半径。border
: 按钮的边框。elevation
: 按钮的阴影效果。
MorphButton(
initialShape: MorphShape.circle,
duration: Duration(milliseconds: 500),
color: Colors.blue,
padding: EdgeInsets.all(20),
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.black, width: 2),
elevation: 5,
child: Text('Click Me'),
onPressed: () {
print('Button Pressed!');
},
onMorph: (MorphShape shape) {
print('Button morphed to: $shape');
},
)