728x90
채팅상담 솔루션 해피톡을 Flutter에서 적용하는 간단 예제 레퍼런스 남깁니다.
Service
import 'dart:io';
import 'package:flutter/material.dart' as material;
import 'package:flutter/foundation.dart';
import 'package:image/image.dart';
import 'package:path_provider/path_provider.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:image_picker/image_picker.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
class HappyTalkService {
void happyTalkSetting({
required WebViewController controller,
}) async {
if (defaultTargetPlatform == TargetPlatform.android) {
final androidController = controller.platform as AndroidWebViewController;
await androidController.setOnShowFileSelector(_androidFilePicker);
}
controller.setJavaScriptMode(JavaScriptMode.unrestricted);
controller.setBackgroundColor(const material.Color(0xff000000));
/// 해피톡 프로젝트 uri 넣어주세요.
controller.loadRequest(Uri.parse(uri));
}
Future<List<String>> _androidFilePicker(
FileSelectorParams params,
) async {
final picker = ImagePicker();
final photo = await picker.pickImage(
source: ImageSource.gallery,
);
if (photo == null) {
return [];
}
final imageData = await photo.readAsBytes();
final decodedImage = decodeImage(imageData)!;
final scaledImage = copyResize(decodedImage, width: 500);
final jpg = encodeJpg(scaledImage);
final filePath = (await getTemporaryDirectory()).uri.resolve(
'./image_${DateTime.now().microsecondsSinceEpoch}.jpg',
);
final file = await File.fromUri(filePath).create(recursive: true);
await file.writeAsBytes(jpg, flush: true);
return [file.uri.toString()];
}
}
Page
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:women09/services/happy_talk_service.dart';
class HappyTalkWebViewPage extends StatefulWidget {
const HappyTalkWebViewPage({super.key});
@override
State<HappyTalkWebViewPage> createState() => _HappyTalkWebViewPageState();
}
class _HappyTalkWebViewPageState extends State<HappyTalkWebViewPage> {
final WebViewController controller = WebViewController();
final HappyTalkService happyTalkService = HappyTalkService();
@override
void initState() {
super.initState();
happyTalkService.happyTalkSetting(controller: controller);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: WebViewWidget(
controller: controller,
),
);
}
}
'Flutter > 기본' 카테고리의 다른 글
[Flutter] 개발 환경 세팅 - macOS (1) | 2024.08.22 |
---|---|
[Flutter] 정규식 모음 (TextField 적용가능) (0) | 2024.07.16 |
[Flutter] IOS App Tracking Transparency 요청 (0) | 2024.06.26 |
[Flutter] Ios Cupertino DatePicker (0) | 2024.06.24 |
[Flutter] Isolate 설명과 사용방법 (0) | 2024.05.28 |