Flutter浮动头部组件插件floaty_head的使用
Flutter浮动头部组件插件floaty_head的使用
One thing that I always loved was the simplicity that Messenger had using Chatheads.
因此,为了实现这种简洁的效果,创建了此插件 floaty_head
。
使用步骤
要使用此插件,请在您的 pubspec.yaml
文件中添加以下依赖:
dependencies:
floaty_head: ^1.1.0
如果需要在代码中与按钮进行交互,请在 Kotlin/Java 类中添加以下代码:
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
Application().onCreate()
}
在 Application.kt
中添加以下代码:
class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FloatyHeadPlugin().setPluginRegistrant(this)
Managment.pluginRegistrantC = this
}
override fun registerWith(registry: PluginRegistry) {
FloatyHeadPlugin().registerWith(registry.registrarFor(this.packageName))
}
}
如果有任何疑问,请查看 example
文件夹。
示例代码
要设置带有任何所需文本或按钮的头部,只需添加以下代码即可。就像任何其他自定义小部件一样!
设置头部
final header = FloatyHeadHeader(
title: FloatyHeadText(
text: "Outgoing Call", fontSize: 10, textColor: Colors.black45),
padding: FloatyHeadPadding.setSymmetricPadding(12, 12),
subTitle: FloatyHeadText(
text: "8989898989",
fontSize: 14,
fontWeight: FontWeight.bold,
textColor: Colors.black87),
decoration: FloatyHeadDecoration(startColor: Colors.grey[100]),
button: FloatyHeadButton(
text: FloatyHeadText(
text: "Personal", fontSize: 10, textColor: Colors.black45),
tag: "personal_btn"),
);
设置主体
final body = FloatyHeadBody(
rows: [
EachRow(
columns: [
EachColumn(
text: FloatyHeadText(
text: "Updated body", fontSize: 12, textColor: Colors.black45),
),
],
gravity: ContentGravity.center,
),
EachRow(columns: [
EachColumn(
text: FloatyHeadText(
text: "Updated long data of the body",
fontSize: 12,
textColor: Colors.black87,
fontWeight: FontWeight.bold),
padding: FloatyHeadPadding.setSymmetricPadding(6, 8),
decoration: FloatyHeadDecoration(
startColor: Colors.black12, borderRadius: 25.0),
margin: FloatyHeadMargin(top: 4),
),
], gravity: ContentGravity.center),
EachRow(
columns: [
EachColumn(
text: FloatyHeadText(
text: "Notes", fontSize: 10, textColor: Colors.black45),
),
],
gravity: ContentGravity.left,
margin: FloatyHeadMargin(top: 8),
),
EachRow(
columns: [
EachColumn(
text: FloatyHeadText(
text: "Updated random notes.",
fontSize: 13,
textColor: Colors.black54,
fontWeight: FontWeight.bold),
),
],
gravity: ContentGravity.left,
),
],
padding: FloatyHeadPadding(left: 16, right: 16, bottom: 12, top: 12),
);
设置底部
final footer = FloatyHeadFooter(
buttons: [
FloatyHeadButton(
text: FloatyHeadText(
text: "Simple button",
fontSize: 12,
textColor: Color.fromRGBO(250, 139, 97, 1)),
tag: "simple_button",
padding: FloatyHeadPadding(left: 10, right: 10, bottom: 10, top: 10),
width: 0,
height: FloatyHeadButton.WRAP_CONTENT,
decoration: FloatyHeadDecoration(
startColor: Colors.white,
endColor: Colors.white,
borderWidth: 0,
borderRadius: 0.0),
),
FloatyHeadButton(
text: FloatyHeadText(
text: "Focus button", fontSize: 12, textColor: Colors.white),
tag: "focus_button",
width: 0,
padding: FloatyHeadPadding(left: 10, right: 10, bottom: 10, top: 10),
height: FloatyHeadButton.WRAP_CONTENT,
decoration: FloatyHeadDecoration(
startColor: Color.fromRGBO(250, 139, 97, 1),
endColor: Color.fromRGBO(247, 28, 88, 1),
borderWidth: 0,
borderRadius: 30.0),
)
],
padding: FloatyHeadPadding(left: 16, right: 16, bottom: 12),
decoration: FloatyHeadDecoration(startColor: Colors.white),
buttonsPosition: ButtonPosition.center,
);
完成以上步骤后,您可以实现以下功能:
- ✅ 第一次运行时的权限。
- ✅ 解决从 Android 检索按下的按钮标签到 Flutter 的问题。
- ✅ 自定义头部、主体和底部的小部件。
- ✅ 自定义通知文本。
- ✅ 自定义通知图标。
- ✅ 自定义气泡图标。
- ✅ 自定义关闭图标。
- ✅ 自定义关闭背景图标。
- ✅ 即使应用程序被杀死,FloatyHead 仍然可以运行。
- ✅ 为所有在 Dart 和 Kotlin 中使用的类添加了文档。
贡献
欢迎所有贡献!
如果您喜欢这个项目,请点击 ⭐ 它,这将非常感谢!或者如果您想添加更多史诗级的功能,可以通过提交拉取请求来实现,这将被欣然接受 🙆♂️。
如果您有任何想法,请发送电子邮件至:luisalfonsocb83@gmail.com。
完整示例代码
import 'dart:async';
import 'package:floaty_head/floaty_head.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Future<void> main() async {
runApp(MaterialApp(home: Home()));
}
class Home extends StatefulWidget {
_Home createState() => _Home();
}
class _Home extends State<Home> {
final FloatyHead floatyHead = FloatyHead();
final header = FloatyHeadHeader(
title: FloatyHeadText(
text: "Outgoing Call", fontSize: 10, textColor: Colors.black45),
padding: FloatyHeadPadding.setSymmetricPadding(12, 12),
subTitle: FloatyHeadText(
text: "8989898989",
fontSize: 14,
fontWeight: FontWeight.bold,
textColor: Colors.black87),
decoration: FloatyHeadDecoration(startColor: Colors.grey[100]),
button: FloatyHeadButton(
text: FloatyHeadText(
text: "Personal", fontSize: 10, textColor: Colors.black45),
tag: "personal_btn"),
);
final body = FloatyHeadBody(
rows: [
EachRow(
columns: [
EachColumn(
text: FloatyHeadText(
text: "Updated body", fontSize: 12, textColor: Colors.black45),
),
],
gravity: ContentGravity.center,
),
EachRow(columns: [
EachColumn(
text: FloatyHeadText(
text: "Updated long data of the body",
fontSize: 12,
textColor: Colors.black87,
fontWeight: FontWeight.bold),
padding: FloatyHeadPadding.setSymmetricPadding(6, 8),
decoration: FloatyHeadDecoration(
startColor: Colors.black12, borderRadius: 25.0),
margin: FloatyHeadMargin(top: 4),
),
], gravity: ContentGravity.center),
EachRow(
columns: [
EachColumn(
text: FloatyHeadText(
text: "Notes", fontSize: 10, textColor: Colors.black45),
),
],
gravity: ContentGravity.left,
margin: FloatyHeadMargin(top: 8),
),
EachRow(
columns: [
EachColumn(
text: FloatyHeadText(
text: "Updated random notes.",
fontSize: 13,
textColor: Colors.black54,
fontWeight: FontWeight.bold),
),
],
gravity: ContentGravity.left,
),
],
padding: FloatyHeadPadding(left: 16, right: 16, bottom: 12, top: 12),
);
final footer = FloatyHeadFooter(
buttons: [
FloatyHeadButton(
text: FloatyHeadText(
text: "Simple button",
fontSize: 12,
textColor: Color.fromRGBO(250, 139, 97, 1)),
tag: "simple_button",
padding: FloatyHeadPadding(left: 10, right: 10, bottom: 10, top: 10),
width: 0,
height: FloatyHeadButton.WRAP_CONTENT,
decoration: FloatyHeadDecoration(
startColor: Colors.white,
endColor: Colors.white,
borderWidth: 0,
borderRadius: 0.0),
),
FloatyHeadButton(
text: FloatyHeadText(
text: "Focus button", fontSize: 12, textColor: Colors.white),
tag: "focus_button",
width: 0,
padding: FloatyHeadPadding(left: 10, right: 10, bottom: 10, top: 10),
height: FloatyHeadButton.WRAP_CONTENT,
decoration: FloatyHeadDecoration(
startColor: Color.fromRGBO(250, 139, 97, 1),
endColor: Color.fromRGBO(247, 28, 88, 1),
borderWidth: 0,
borderRadius: 30.0),
)
],
padding: FloatyHeadPadding(left: 16, right: 16, bottom: 12),
decoration: FloatyHeadDecoration(startColor: Colors.white),
buttonsPosition: ButtonPosition.center,
);
bool alternateColor = false;
[@override](/user/override)
void initState() {
super.initState();
FloatyHead.registerOnClickListener(callBack);
}
[@override](/user/override)
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text('Floaty Chathead')),
body: SingleChildScrollView(
padding: EdgeInsets.all(50),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
RaisedButton(
child: Text('Open Floaty Chathead'),
onPressed: () => floatyHead.openBubble()),
RaisedButton(
child: Text('Close Floaty Chathead'),
onPressed: () => closeFloatyHead()),
RaisedButton(
child: Text('Set icon Floaty Chathead'),
onPressed: () => setIcon()),
RaisedButton(
child: Text('Set close icon Floaty Chathead'),
onPressed: () => setCloseIcon()),
RaisedButton(
child: Text('Set close background Icon Floaty Chathead'),
onPressed: () => setCloseIconBackground()),
RaisedButton(
child: Text(
'Set notification title to: OH MY GOD! THEY KILL KENNY!!! Floaty Chathead'),
onPressed: () => setNotificationTitle()),
RaisedButton(
child: Text('Set notification Icon Floaty Chathead'),
onPressed: () => setNotificationIcon()),
RaisedButton(
child: Text('Set Custom Header into Floaty Chathead'),
onPressed: () => setCustomHeader()),
],
),
),
);
void setCustomHeader() {
floatyHead.updateFloatyHeadContent(
header: header,
body: body,
footer: footer,
);
}
void closeFloatyHead() {
if (floatyHead.isOpen) {
floatyHead.closeHead();
}
}
Future<void> setNotificationTitle() async {
String result;
try {
result = await floatyHead
.setNotificationTitle("OH MY GOD! THEY KILL KENNY!!!");
} on PlatformException {
result = 'Failed to get icon.';
}
if (!mounted) return;
}
Future<void> setNotificationIcon() async {
String result;
String assetPath = "assets/notificationIcon.png";
try {
result = await floatyHead.setNotificationIcon(assetPath);
print(result);
} on PlatformException {
result = 'Failed to get icon.';
print("failed: $result");
}
if (!mounted) return;
}
Future<void> setIcon() async {
String result;
String assetPath = "assets/chatheadIcon.png";
try {
result = await floatyHead.setIcon(assetPath);
} on PlatformException {
result = 'Failed to get icon.';
}
if (!mounted) return;
}
Future<void> setCloseIcon() async {
String assetPath = "assets/close.png";
try {
await floatyHead.setCloseIcon(assetPath);
} on PlatformException {
return;
}
if (!mounted) return;
}
Future<void> setCloseIconBackground() async {
String assetPath = "assets/closeBg.png";
try {
await floatyHead.setCloseBackgroundIcon(assetPath);
} on PlatformException {
return;
}
if (!mounted) return;
}
}
void callBack(String tag) {
print('CALLBACK FROM FRAGMENT BUILDED: $tag');
switch (tag) {
case "simple_button":
case "updated_simple_button":
break;
case "focus_button":
print("Focus button has been called");
break;
default:
print("OnClick event of $tag");
}
}
更多关于Flutter浮动头部组件插件floaty_head的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter浮动头部组件插件floaty_head的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
floaty_head
是一个用于在 Flutter 应用中创建浮动头部组件的插件。它允许你在应用界面上创建一个悬浮的头部,类似于 Facebook Messenger 或 WhatsApp 的聊天头部。这个插件非常适合用于需要快速访问某些功能或信息的场景。
安装 floaty_head
首先,你需要在 pubspec.yaml
文件中添加 floaty_head
插件的依赖:
dependencies:
flutter:
sdk: flutter
floaty_head: ^0.1.0 # 请检查最新版本
然后运行 flutter pub get
来安装依赖。
使用 floaty_head
以下是一个简单的示例,展示如何使用 floaty_head
创建一个浮动头部组件。
import 'package:flutter/material.dart';
import 'package:floaty_head/floaty_head.dart';
import 'package:floaty_head/head_content.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Floaty Head Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
_showFloatyHead(context);
},
child: Text('Show Floaty Head'),
),
),
),
);
}
void _showFloatyHead(BuildContext context) {
FloatyHead.floatyHead(
context,
headContent: HeadContent(
title: "Floaty Head",
icon: Icons.message,
onTap: () {
print("Floaty Head tapped!");
},
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("This is the body of the floaty head."),
SizedBox(height: 16.0),
ElevatedButton(
onPressed: () {
FloatyHead.closeFloatyHead();
},
child: Text("Close"),
),
],
),
),
);
}
}