flutter_inapp_chat 0.4.0 copy "flutter_inapp_chat: ^0.4.0" to clipboard
flutter_inapp_chat: ^0.4.0 copied to clipboard

A simple, complete chat system for Flutter with ready-to-use UI, image sharing, and view models, backed by Cloud Firestore, Appwrite, or Supabase.

example/lib/main.dart

import 'package:flutter_inapp_chat/inapp_chat.dart';
import 'package:flutter_inapp_chat/backends/firestore_chat_backend.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'firebase_options.dart';

// This demo uses FirestoreChatBackend. To use Appwrite or Supabase instead,
// import 'package:flutter_inapp_chat/backends/appwrite_chat_backend.dart' or
// 'package:flutter_inapp_chat/backends/supabase_chat_backend.dart' and pass an
// AppwriteChatBackend(...) / SupabaseChatBackend(...) as ChatEntity.backend.
final _chatBackend = FirestoreChatBackend();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  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(
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('en', ''), // English
        Locale('ar', ''), // Arabic
      ],
      locale: const Locale('ar'), // Set the initial locale to Arabic
      title: 'Firestore Chat Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      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> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () => openChatFirst(context),
              child: const Text("Open Chat"),
            ),
            const SizedBox(height: 50),
            ElevatedButton(
              onPressed: () => openChatSecond(context),
              child: const Text("Open Chat"),
            ),
          ],
        ),
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  //open via direct method call
  Future<void> openChatFirst(BuildContext context) async {
    final mainUser = PeerUser(id: "1", name: "User 1");
    final peers = {
      "1": mainUser,
      "2": PeerUser(id: "2", name: "User 2"),
      "3": PeerUser(id: "3", name: "User 3"),
      "4": PeerUser(id: "4", name: "User 4"),
      "5": PeerUser(id: "5", name: "User 5"),
    };

    final chatEntity = ChatEntity(
      mainUser: mainUser,
      peers: peers,
      backend: _chatBackend,
      path: "sport/football/2022/worldcup-1",
      title: "FIFA 2022 Games - Photo Sharing - 1",
      supportMedia: true,
      onMessageSent: (String message, ChatEntity chatEntity) {
        //handle when chat has been sent to firestore
        //you can use it to send notification or show a toast/snakbar
      },
    );

    await InappChat().openChatPage(context, chatEntity);
  }

  //open via navigation routing
  Future<void> openChatSecond(BuildContext context) async {
    final mainUser = PeerUser(id: "1", name: "User 1");
    final peers = {
      "1": mainUser,
      "2": PeerUser(id: "2", name: "User 2"),
      "3": PeerUser(id: "3", name: "User 3"),
      "4": PeerUser(id: "4", name: "User 4"),
      "5": PeerUser(id: "5", name: "User 5"),
    };

    final chatEntity = ChatEntity(
      mainUser: mainUser,
      peers: peers,
      backend: _chatBackend,
      path: "sport/football/2022/worldcup",
      title: "FIFA 2022 Games",
      onMessageSent: (String message, ChatEntity chatEntity) {
        //handle when chat has been sent to firestore
        //you can use it to send notification or show a toast/snakbar
      },
    );
    Navigator.push(context, InappChat().chatPageWidget(chatEntity));
  }
}
0
likes
160
points
77
downloads

Documentation

API reference

Publisher

verified publisheredentech.online

Weekly Downloads

A simple, complete chat system for Flutter with ready-to-use UI, image sharing, and view models, backed by Cloud Firestore, Appwrite, or Supabase.

Repository (GitHub)
View/report issues

Topics

#chat #firebase #firestore #supabase #appwrite

License

MIT (license)

Dependencies

appwrite, cloud_firestore, dash_chat_custom, firebase_core, firebase_storage, flutter, image_picker, intl, ionicons, stacked, supabase_flutter, url_launcher

More

Packages that depend on flutter_inapp_chat