helpdesk_package 0.0.4
helpdesk_package: ^0.0.4 copied to clipboard
A new Flutter packagefor helpdesk
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:helpdesk_package/firebase_options.dart';
import 'package:helpdesk_package/simple_chat_sdk.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize the package (which will init Firebase)
await SimpleChatService.instance.init(options: DefaultFirebaseOptions.currentPlatform);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example User Chat',
home: const ChatHome(),
);
}
}
class ChatHome extends StatefulWidget {
const ChatHome({super.key});
@override
State<ChatHome> createState() => _ChatHomeState();
}
class _ChatHomeState extends State<ChatHome> {
String appId = 'com.example.myapp'; // choose the appId you registered in admin
String userId = 'user_${DateTime.now().millisecondsSinceEpoch}';
final TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('User Chat'),
actions: [
IconButton(
onPressed: () => setState(() {
// rotate userId (demo)
userId = 'user_${DateTime.now().millisecondsSinceEpoch}';
}),
icon: const Icon(Icons.refresh),
tooltip: 'Change userId',
)
],
),
body: Column(
children: [
Expanded(
child: StreamBuilder<List<ChatMessage>>(
stream: SimpleChatService.instance.streamMessages(appId: appId, userId: userId),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
final messages = snapshot.data ?? [];
if (messages.isEmpty) {
return const Center(child: Text('No messages yet.'));
}
return ListView.builder(
itemCount: messages.length,
itemBuilder: (context, index) {
final m = messages[index];
final isMe = m.sender == 'user';
return ListTile(
title: Align(
alignment: isMe ? Alignment.centerRight : Alignment.centerLeft,
child: Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: isMe ? Colors.blue[100] : Colors.grey[300],
borderRadius: BorderRadius.circular(8),
),
child: Text('${m.content}\n', style: const TextStyle(fontSize: 16)),
),
),
subtitle: Align(
alignment: isMe ? Alignment.centerRight : Alignment.centerLeft,
child: Text('${m.sender} • ${m.timestamp.toDate()}',
style: const TextStyle(fontSize: 11),
),
),
);
},
);
},
),
),
SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
child: Row(
children: [
Expanded(
child: TextField(controller: _controller, decoration: const InputDecoration(hintText: 'Write message...')),
),
IconButton(
icon: const Icon(Icons.send),
onPressed: () async {
final text = _controller.text.trim();
if (text.isEmpty) return;
await SimpleChatService.instance.sendMessage(
appId: appId,
userId: userId,
content: text,
);
_controller.clear();
},
)
],
),
),
)
],
),
);
}
}