xc_chat_scroll_list_view 1.0.0 copy "xc_chat_scroll_list_view: ^1.0.0" to clipboard
xc_chat_scroll_list_view: ^1.0.0 copied to clipboard

A list of conversations

example/lib/main.dart

import 'package:xc_chat_scroll_list_view/core/chat_controller.dart';
import 'package:xc_chat_scroll_list_view/models/message_model.dart';
import 'package:xc_chat_scroll_list_view/widget/chat_list_widget.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'chat',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // TRY THIS: Try running your application with "flutter run". You'll see
        // the application has a purple toolbar. Then, without quitting the app,
        // try changing the seedColor in the colorScheme below to Colors.green
        // and then invoke "hot reload" (save your changes or press the "hot
        // reload" button in a Flutter-supported IDE, or press "r" if you used
        // the command line to start the app).
        //
        // Notice that the counter didn't reset back to zero; the application
        // state is not lost during the reload. To reset the state, use hot
        // restart instead.
        //
        // This works for code too, not just values: Most code changes can be
        // tested with just a hot reload.
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  int count = 0;

  late ChatController chatController;

  ScrollController scrollController = ScrollController();

  final List<MessageModel> _messageList = [
    MessageModel(
      ownerName: "assistant",
      ownerType: OwnerType.receiver,
      content: "ChatGpt是由 openAI 研发的聊天机器人",
      createdAt: 1711699180418,
      id: 1,
      avatar: 'https://dog.ceo/img/dog-ceo-zine.jpg'
    ),
  ];



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Column(
        children: [
          Expanded(
            child: ChatList(
              chatController: chatController,
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(onPressed: _onSendMessage, child: const Text("send")),
              ElevatedButton(onPressed: _onloadMore, child: const Text("loadMore"))
            ],
          ),
          const SizedBox(
            height: 20,
          )
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    chatController = ChatController(
        initialMessageList: _messageList,
        scrollController: scrollController,
        timePellet: 60,
    );
  }


  // 发送消息
  void _onSendMessage() async {
    int c = count++;
    chatController.addMessage( MessageModel(
        ownerName: "user",
        ownerType: OwnerType.sender,
        content: "hello $c",
        // 获取毫秒数据
        createdAt: DateTime.now().millisecondsSinceEpoch,
        id: c,
        avatar: 'https://dog.ceo/img/dog-ceo-zine.jpg'
    ));
    await Future.delayed(const Duration(milliseconds: 2000));
    ++c;
    chatController.addMessage(MessageModel(
        ownerName: "assistant",
        ownerType: OwnerType.receiver,
        content: "hello $c",
        // 获取毫秒数据
        createdAt: DateTime.now().millisecondsSinceEpoch,
        id: c,
        avatar: 'https://dog.ceo/img/dog-ceo-zine.jpg'
    ));
  }

  int _counter = 0;
  // 加载数据
  void _onloadMore() {
    int tl = 1711718958602;
    tl = tl - ++_counter*1000000;
    final List<MessageModel> messageList = [
      MessageModel(
          ownerName: "user",
          ownerType: OwnerType.sender,
          content: "hello $_counter*1000000",
          // 获取毫秒数据
          createdAt: DateTime.now().millisecondsSinceEpoch,
          id: _counter*1000000,
          avatar: 'https://dog.ceo/img/dog-ceo-zine.jpg'
      ),
    ];
    chatController.loadMoreData(messageList);
  }
}
1
likes
120
points
0
downloads

Publisher

unverified uploader

Weekly Downloads

A list of conversations

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

bubble, flutter, intl

More

Packages that depend on xc_chat_scroll_list_view