Flutter聊天气泡插件flutter_basic_chat_bubble的使用
Flutter聊天气泡插件flutter_basic_chat_bubble的使用

此插件的目的是展示可自定义的聊天气泡。你可以以特定方式自定义这些气泡。目前这些气泡仅能显示文本消息。其他数据类型暂不支持。

使用方法
设置依赖
在pubspec.yaml
文件中添加以下依赖:
dependencies:
flutter_basic_chat_bubble: ^0.2.0+2
安装
运行以下命令安装依赖:
flutter pub get
导入
在你的Dart文件中导入插件:
import 'package:flutter_basic_chat_bubble/flutter_basic_chat_bubble.dart';
使用
创建一个包含聊天气泡的列表视图。以下是完整的示例代码:
import 'package:flutter/material.dart';
import 'package:flutter_basic_chat_bubble/flutter_basic_chat_bubble.dart';
void main() {
runApp(BasicChatBubbleDemo());
}
class BasicChatBubbleDemo extends StatelessWidget {
// 创建一个包含示例聊天消息的列表
final List<BasicChatMessage> messages = [
BasicChatMessage(peerUserName: 'User1', messageText: 'Awsome message!', timeStamp: '12:00'),
BasicChatMessage(peerUserName: 'User1', messageText: 'Awsome message!', timeStamp: 'Yesterday'),
BasicChatMessage(peerUserName: 'User1', messageText: 'Awsome message!', timeStamp: 'Tue'),
BasicChatMessage(peerUserName: 'User1', messageText: 'Awsome message!', timeStamp: 'Mon'),
BasicChatMessage(peerUserName: 'User1', messageText: 'Awsome message!', timeStamp: 'Sun'),
];
// 此小部件是应用程序的根。
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Basic Chat Bubble Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Scaffold(
body: ListView.builder(
itemCount: messages.length,
itemBuilder: (BuildContext context, int index) {
return BasicChatBubble(
message: messages[index],
isMe: index % 2 == 0, // 每第二个气泡将isMe标志设置为true
backgroundColor: (index % 2 == 0 ? Colors.green[400] : Colors.blue)!, // 根据索引设置背景颜色
textColor: Colors.white,
buttonWidget: index == messages.length - 1
? InkWell(
child: CircleAvatar(
backgroundColor: Colors.red,
child: Icon(
Icons.video_call,
color: Colors.white,
),
),
onTap: () {
print('Button tapped $index');
})
: null,
buttonText: 'Make a Call', // 如果需要显示按钮文本
);
},
),
),
);
}
}
属性
以下是BasicChatBubble
组件的属性:
final BasicChatMessage message;
final bool isMe;
final Color backgroundColor;
final Color textColor;
final Widget buttonWidget;
final String buttonText;
message
message
属性包含一个表示消息内容的对象。它定义如下:
/// `BasicChatMessage` 类代表单个消息,形成 `[BasicChatBubble]` 的内容。
/// 此对象具有以下属性:
/// `String peerUserName` 包含对方用户的名称(发送或接收消息的人)
/// `String ownUserName` 包含自己的用户名
/// `String messageText` 包含消息的文本
/// `DateTime timeStamp` 是消息发送的时间/日期
class BasicChatMessage {
String peerUserName;
String messageText;
String timeStamp;
BasicChatMessage({this.peerUserName, this.messageText, this.timeStamp});
}
isMe
此参数指示应用程序的用户是否是消息的发送者。它确定气泡的位置:
true
: 气泡右对齐false
: 气泡左对齐
backgroundColor
此属性定义聊天气泡的背景颜色。
textColor
通过此属性可以设置聊天气泡中文本元素的颜色。
嵌入按钮到聊天气泡
在某些情况下,可能希望在聊天气泡内嵌入一个按钮。想象一下带有视频或电话功能的消息应用。

这可以通过在buttonWidget
和 buttonText
参数中指定一个用于此目的的控件来实现。
buttonWidget
在此处指定一个应作为按钮使用的控件。通常你会使用一个InkWell
小部件使其可点击。
示例:
InkWell(
child: CircleAvatar(
backgroundColor: Colors.red,
child: Icon(
Icons.video_call,
color: Colors.white,
),
),
onTap: () {
print('Button tapped $index');
}
)
更多关于Flutter聊天气泡插件flutter_basic_chat_bubble的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter聊天气泡插件flutter_basic_chat_bubble的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_basic_chat_bubble
是一个用于在 Flutter 应用中实现聊天气泡的插件。它提供了简单的 API 来创建类似于聊天应用中的消息气泡,支持自定义样式、颜色、边框等。
安装
首先,你需要在 pubspec.yaml
文件中添加 flutter_basic_chat_bubble
依赖:
dependencies:
flutter:
sdk: flutter
flutter_basic_chat_bubble: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
基本使用
flutter_basic_chat_bubble
提供了一个 ChatBubble
小部件,你可以使用它来创建聊天气泡。
import 'package:flutter/material.dart';
import 'package:flutter_basic_chat_bubble/flutter_basic_chat_bubble.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Chat Bubble Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ChatBubble(
text: 'Hello, this is a chat bubble!',
isSender: true, // 设置为 true 表示发送者,false 表示接收者
backgroundColor: Colors.blue,
textColor: Colors.white,
),
SizedBox(height: 16), // 添加一些间距
ChatBubble(
text: 'Hi, this is another chat bubble!',
isSender: false,
backgroundColor: Colors.grey[300],
textColor: Colors.black,
),
],
),
),
),
);
}
}
参数说明
ChatBubble
小部件的常用参数如下:
text
: 气泡中显示的文本内容。isSender
: 布尔值,表示气泡是否为发送者的消息。true
表示发送者,false
表示接收者。backgroundColor
: 气泡的背景颜色。textColor
: 文本的颜色。borderRadius
: 气泡的圆角半径。padding
: 气泡的内边距。margin
: 气泡的外边距。borderWidth
: 气泡的边框宽度。borderColor
: 气泡的边框颜色。
自定义样式
你可以通过调整上述参数来自定义聊天气泡的样式。例如:
ChatBubble(
text: 'Customized chat bubble!',
isSender: true,
backgroundColor: Colors.green,
textColor: Colors.white,
borderRadius: BorderRadius.circular(20),
padding: EdgeInsets.all(12),
margin: EdgeInsets.symmetric(vertical: 8),
borderWidth: 2,
borderColor: Colors.black,
),