|
| 1 | +import 'dart:io'; |
| 2 | + |
1 | 3 | import 'package:chatwoot_sdk/chatwoot_sdk.dart';
|
2 | 4 | import 'package:flutter/material.dart';
|
| 5 | +import 'package:flutter/services.dart'; |
| 6 | +import 'package:image/image.dart' as image; |
| 7 | +import 'package:image_picker/image_picker.dart' as image_picker; |
| 8 | +import 'package:path_provider/path_provider.dart'; |
3 | 9 |
|
4 | 10 | void main() {
|
5 | 11 | runApp(MyApp());
|
@@ -31,75 +37,66 @@ class MyHomePage extends StatefulWidget {
|
31 | 37 | class _MyHomePageState extends State<MyHomePage> {
|
32 | 38 | @override
|
33 | 39 | void initState() {
|
34 |
| - // TODO: implement initState |
35 | 40 | super.initState();
|
36 | 41 | }
|
37 | 42 |
|
38 |
| - _showChatwootDialog() { |
39 |
| - ChatwootChatDialog.show( |
40 |
| - context, |
41 |
| - baseUrl: "https://app.chatwoot.com", |
42 |
| - inboxIdentifier: "xxxxxxxxxxxxxxxxxxx", |
43 |
| - title: "Chatwoot Support", |
44 |
| - user: ChatwootUser( |
45 |
| - |
46 |
| - name: "Tester test", |
47 |
| - |
48 |
| - ), |
49 |
| - ); |
50 |
| - } |
51 |
| - |
52 | 43 | @override
|
53 | 44 | Widget build(BuildContext context) {
|
54 |
| - return ChatwootChat( |
55 |
| - baseUrl: "https://app.chatwoot.com", |
56 |
| - inboxIdentifier: "xxxxxxxxxxxxxxxxxxx", |
57 |
| - user: ChatwootUser( |
58 |
| - |
59 |
| - name: "Tester test1", |
60 |
| - |
61 |
| - ), |
| 45 | + return Scaffold( |
62 | 46 | appBar: AppBar(
|
63 |
| - title: Text( |
64 |
| - "Chatwoot", |
65 |
| - style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold), |
66 |
| - ), |
67 |
| - leading: InkWell( |
68 |
| - onTap: () => _showChatwootDialog(), |
69 |
| - child: Padding( |
70 |
| - padding: const EdgeInsets.all(8.0), |
71 |
| - child: Image.asset("assets/chatwoot_logo.png"), |
72 |
| - ), |
| 47 | + title: Text("Chatwoot Example"), |
| 48 | + ), |
| 49 | + body: ChatwootWidget( |
| 50 | + websiteToken: "websiteToken", |
| 51 | + baseUrl: "https://app.chatwoot.com", |
| 52 | + user: ChatwootUser( |
| 53 | + |
| 54 | + name: "Tester test", |
| 55 | + |
73 | 56 | ),
|
74 |
| - backgroundColor: Colors.white, |
| 57 | + locale: "en", |
| 58 | + closeWidget: () { |
| 59 | + if (Platform.isAndroid) { |
| 60 | + SystemNavigator.pop(); |
| 61 | + } else if (Platform.isIOS) { |
| 62 | + exit(0); |
| 63 | + } |
| 64 | + }, |
| 65 | + //attachment only works on android for now |
| 66 | + onAttachFile: _androidFilePicker, |
| 67 | + onLoadStarted: () { |
| 68 | + print("loading widget"); |
| 69 | + }, |
| 70 | + onLoadProgress: (int progress) { |
| 71 | + print("loading... ${progress}"); |
| 72 | + }, |
| 73 | + onLoadCompleted: () { |
| 74 | + print("widget loaded"); |
| 75 | + }, |
75 | 76 | ),
|
76 |
| - onWelcome: () { |
77 |
| - print("Welcome event received"); |
78 |
| - }, |
79 |
| - onPing: () { |
80 |
| - print("Ping event received"); |
81 |
| - }, |
82 |
| - onConfirmedSubscription: () { |
83 |
| - print("Confirmation event received"); |
84 |
| - }, |
85 |
| - onMessageDelivered: (_) { |
86 |
| - print("Message delivered event received"); |
87 |
| - }, |
88 |
| - onMessageSent: (_) { |
89 |
| - print("Message sent event received"); |
90 |
| - }, |
91 |
| - onConversationIsOffline: () { |
92 |
| - print("Conversation is offline event received"); |
93 |
| - }, |
94 |
| - onConversationIsOnline: () { |
95 |
| - print("Conversation is online event received"); |
96 |
| - }, |
97 |
| - onConversationStoppedTyping: () { |
98 |
| - print("Conversation stopped typing event received"); |
99 |
| - }, |
100 |
| - onConversationStartedTyping: () { |
101 |
| - print("Conversation started typing event received"); |
102 |
| - }, |
103 | 77 | );
|
104 | 78 | }
|
| 79 | + |
| 80 | + Future<List<String>> _androidFilePicker() async { |
| 81 | + final picker = image_picker.ImagePicker(); |
| 82 | + final photo = |
| 83 | + await picker.pickImage(source: image_picker.ImageSource.gallery); |
| 84 | + |
| 85 | + if (photo == null) { |
| 86 | + return []; |
| 87 | + } |
| 88 | + |
| 89 | + final imageData = await photo.readAsBytes(); |
| 90 | + final decodedImage = image.decodeImage(imageData); |
| 91 | + final scaledImage = image.copyResize(decodedImage, width: 500); |
| 92 | + final jpg = image.encodeJpg(scaledImage, quality: 90); |
| 93 | + |
| 94 | + final filePath = (await getTemporaryDirectory()).uri.resolve( |
| 95 | + './image_${DateTime.now().microsecondsSinceEpoch}.jpg', |
| 96 | + ); |
| 97 | + final file = await File.fromUri(filePath).create(recursive: true); |
| 98 | + await file.writeAsBytes(jpg, flush: true); |
| 99 | + |
| 100 | + return [file.uri.toString()]; |
| 101 | + } |
105 | 102 | }
|
0 commit comments