Flutter交互按钮插件flutter_reaction_button的使用
Flutter交互按钮插件 flutter_reaction_button
的使用
flutter_reaction_button
是一个可定制的 Flutter 包,允许你轻松创建带有表情符号的交互式按钮,类似于 Facebook 的标志性反应按钮。
截图
安装
在你的 pubspec.yaml
文件中添加以下依赖项:
dependencies:
flutter:
sdk: flutter
flutter_reaction_button: <last-version>
请确保将 <last-version>
替换为最新版本号。你可以通过 pub.dev 查看最新的版本号。
使用方法
下面是一个简单的示例,展示如何使用 flutter_reaction_button
插件:
示例代码
import 'package:flutter/material.dart';
import 'package:flutter_reaction_button/flutter_reaction_button.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Reaction Button',
home: Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title: const Text('Flutter Reaction Button'),
actions: [
Builder(
builder: (context) {
return SizedBox.square(
dimension: 30,
child: ReactionButton<String>(
toggle: false,
direction: ReactionsBoxAlignment.rtl,
onReactionChanged: (Reaction<String>? reaction) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Selected language: ${reaction?.value}'),
),
);
},
reactions: [
Reaction<String>(
value: 'like',
icon: Icon(Icons.thumb_up),
),
Reaction<String>(
value: 'love',
icon: Icon(Icons.favorite),
),
Reaction<String>(
value: 'haha',
icon: Icon(Icons.face),
),
// 添加更多表情
],
placeholder: const Reaction<String>(
value: null,
icon: Icon(Icons.language),
),
boxColor: Colors.black.withOpacity(0.5),
boxRadius: 10,
itemsSpacing: 20,
itemSize: const Size(40, 60),
),
);
},
),
const SizedBox(width: 10),
],
),
body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(vertical: 5),
child: Column(
children: [
PostWidget(
title: 'Image 1',
imgPath: 'assets/images/img1.jpg',
reactions: [
Reaction<String>(
value: 'like',
icon: Icon(Icons.thumb_up),
),
Reaction<String>(
value: 'love',
icon: Icon(Icons.favorite),
),
Reaction<String>(
value: 'haha',
icon: Icon(Icons.face),
),
// 添加更多表情
],
),
// 添加更多的 PostWidget 和 ImageWidget
],
),
),
),
);
}
}
class PostWidget extends StatelessWidget {
final String title;
final String imgPath;
final List<Reaction<String>> reactions;
const PostWidget({
super.key,
required this.title,
required this.imgPath,
required this.reactions,
});
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(8.0),
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8.0),
),
child: Column(
children: [
Text(title),
Image.asset(imgPath),
ReactionButton<String>(
onReactionChanged: (Reaction<String>? reaction) {
debugPrint('Selected value: ${reaction?.value}');
},
reactions: reactions,
initialReaction: reactions.first,
selectedReaction: reactions.first,
),
],
),
);
}
}
关键属性说明
onReactionChanged
: 当用户选择一个表情时触发的回调函数。reactions
: 表情列表,每个表情由Reaction
对象表示。initialReaction
: 初始显示的表情。selectedReaction
: 用户选择后的表情。
许可证
MIT License
Copyright (c) 2023 Abdelouahed Medjoudja
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
希望这个示例能够帮助你快速上手 flutter_reaction_button
插件!如果有任何问题或需要进一步的帮助,请随时提问。
更多关于Flutter交互按钮插件flutter_reaction_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter交互按钮插件flutter_reaction_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter项目中使用flutter_reaction_button
插件的示例代码。这个插件通常用于实现类似于点赞、踩等交互按钮的功能。
首先,确保你已经在pubspec.yaml
文件中添加了flutter_reaction_button
的依赖:
dependencies:
flutter:
sdk: flutter
flutter_reaction_button: ^最新版本号 # 请替换为实际最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Dart文件中使用flutter_reaction_button
。以下是一个完整的示例,展示如何设置和使用这个插件:
import 'package:flutter/material.dart';
import 'package:flutter_reaction_button/flutter_reaction_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Reaction Button Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ReactionButtonDemo(),
);
}
}
class ReactionButtonDemo extends StatefulWidget {
@override
_ReactionButtonDemoState createState() => _ReactionButtonDemoState();
}
class _ReactionButtonDemoState extends State<ReactionButtonDemo> {
List<Reaction> reactions = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Reaction Button Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ReactionButton(
onReactionChanged: (List<Reaction> newReactions) {
setState(() {
reactions = newReactions;
});
},
initialReactions: reactions,
iconData: Icons.thumb_up,
reactionCountTextStyle: TextStyle(fontSize: 18),
maxReactions: 5, // 最大反应数,可以根据需要调整
reactionType: ReactionType.like, // 可以设置为 ReactionType.like, ReactionType.dislike 等
),
SizedBox(height: 20),
Text(
'Total Reactions: ${reactions.length}',
style: TextStyle(fontSize: 20),
),
],
),
),
);
}
}
在这个示例中:
- 我们创建了一个简单的Flutter应用,并在主页中使用了
ReactionButton
。 ReactionButton
的onReactionChanged
回调会在用户交互时更新反应列表。initialReactions
参数用于设置初始反应列表(在这个例子中为空列表)。iconData
参数设置了按钮的图标,这里使用了点赞图标Icons.thumb_up
。reactionCountTextStyle
参数用于自定义反应计数的文本样式。maxReactions
参数设置了最大反应数,这里设置为5。reactionType
参数设置了反应类型,这里设置为ReactionType.like
(点赞)。
这个示例展示了基本的用法,你可以根据实际需求进一步自定义和扩展功能。