Flutter聊天气泡插件nice_chat_bubble的使用

Flutter聊天气泡插件nice_chat_bubble的使用

开始使用

Nice Chat Bubble 是一个用于在 Flutter 中创建聊天气泡效果的组件。这个插件易于使用,可以快速地为您的应用添加聊天功能。

聊天气泡属性

以下是一个简单的 ChatBubble 组件的属性配置示例:

ChatBubble(
     message: 'Hello, How are you?',
     bubbleColor: Colors.blue,
     isReciever: true,
     isSeen: true,
     bubbleTextStyle: const TextStyle(
            color: Colors.white,
            fontSize: 15,        
   ),
),

完整代码示例

以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 nice_chat_bubble 插件:

import 'package:flutter/material.dart';
import 'package:nice_chat_bubble/nice_chat_bubble.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        physics: const BouncingScrollPhysics(),
        child: Column(
          children: [
            const SizedBox(height: 400), // 添加一些顶部空白
            ChatBubble(
              message:
                  'How are you? I hope you are doing well. Where are you currently? I"'
                  'm at your home.',
              bubbleColor: Colors.green,
              isReciever: false,
              isSeen: false,
              bubbleTextStyle: const TextStyle(
                color: Colors.white,
                fontSize: 15,
              ),
            ),
            ChatBubble(
              message:
                  'I am fine. Please wait 1 hour, coming as soon as possible. Is everything ok? What is your phone password buddy?',
              bubbleColor: Colors.blue,
              isReciever: true,
              isSeen: true,
              bubbleTextStyle: const TextStyle(
                color: Colors.white,
                fontSize: 15,
              ),
            ),
            const SizedBox(height: 20), // 添加一些底部间距
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
              child: TextFormField(
                decoration: InputDecoration(
                  hintText: 'Type a message',
                  enabledBorder: OutlineInputBorder(
                    borderSide: const BorderSide(color: Colors.grey),
                    borderRadius: BorderRadius.circular(10),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: const BorderSide(color: Colors.grey),
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter聊天气泡插件nice_chat_bubble的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter聊天气泡插件nice_chat_bubble的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


nice_chat_bubble 是一个用于 Flutter 的聊天气泡插件,可以帮助你轻松地在应用中创建类似于聊天应用中的消息气泡。它提供了多种自定义选项,例如气泡颜色、边框、圆角、阴影等。

安装 nice_chat_bubble

首先,你需要在 pubspec.yaml 文件中添加 nice_chat_bubble 依赖:

dependencies:
  flutter:
    sdk: flutter
  nice_chat_bubble: ^1.0.0  # 请查看最新版本

然后运行 flutter pub get 来安装依赖。

使用 nice_chat_bubble

基本用法

import 'package:flutter/material.dart';
import 'package:nice_chat_bubble/nice_chat_bubble.dart';

class ChatScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Chat Bubble Example'),
      ),
      body: ListView(
        children: [
          // 发送者消息气泡
          NiceChatBubble(
            message: 'Hello, how are you?',
            isSender: true,
            color: Colors.blue,
            textColor: Colors.white,
          ),
          SizedBox(height: 10),
          // 接收者消息气泡
          NiceChatBubble(
            message: 'I\'m fine, thank you!',
            isSender: false,
            color: Colors.grey[300],
            textColor: Colors.black,
          ),
        ],
      ),
    );
  }
}

自定义选项

nice_chat_bubble 提供了多种自定义选项,例如:

  • message: 消息内容。
  • isSender: 是否为发送者消息。
  • color: 气泡背景颜色。
  • textColor: 消息文本颜色。
  • borderRadius: 气泡的圆角半径。
  • borderColor: 气泡边框颜色。
  • borderWidth: 气泡边框宽度。
  • shadowColor: 气泡阴影颜色。
  • shadowOffset: 气泡阴影偏移量。
  • shadowBlurRadius: 气泡阴影模糊半径。
  • padding: 气泡内边距。
  • margin: 气泡外边距。
NiceChatBubble(
  message: 'Customized chat bubble',
  isSender: true,
  color: Colors.green,
  textColor: Colors.white,
  borderRadius: BorderRadius.circular(20),
  borderColor: Colors.black,
  borderWidth: 2,
  shadowColor: Colors.grey,
  shadowOffset: Offset(2, 2),
  shadowBlurRadius: 5,
  padding: EdgeInsets.all(12),
  margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
),

完整示例

以下是一个完整的示例,展示了如何在 Flutter 应用中使用 nice_chat_bubble 插件:

import 'package:flutter/material.dart';
import 'package:nice_chat_bubble/nice_chat_bubble.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Nice Chat Bubble Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ChatScreen(),
    );
  }
}

class ChatScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Chat Bubble Example'),
      ),
      body: ListView(
        children: [
          NiceChatBubble(
            message: 'Hello, how are you?',
            isSender: true,
            color: Colors.blue,
            textColor: Colors.white,
          ),
          SizedBox(height: 10),
          NiceChatBubble(
            message: 'I\'m fine, thank you!',
            isSender: false,
            color: Colors.grey[300],
            textColor: Colors.black,
          ),
          SizedBox(height: 10),
          NiceChatBubble(
            message: 'Customized chat bubble',
            isSender: true,
            color: Colors.green,
            textColor: Colors.white,
            borderRadius: BorderRadius.circular(20),
            borderColor: Colors.black,
            borderWidth: 2,
            shadowColor: Colors.grey,
            shadowOffset: Offset(2, 2),
            shadowBlurRadius: 5,
            padding: EdgeInsets.all(12),
            margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
          ),
        ],
      ),
    );
  }
}
回到顶部