import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io' show Platform;
import 'package:flutter/services.dart';
import 'package:adpopcornssp_flutter/adpopcornssp_flutter.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late final WebViewController _webViewController;
@override
void initState() {
super.initState();
_initializeAdPopcorn();
_setupWebViewController();
}
void _initializeAdPopcorn() {
if (Platform.isAndroid) {
AdPopcornSSP.init('436579265');
} else if (Platform.isIOS) {
AdPopcornSSP.init('537048423');
}
AdPopcornSSP.setUserId('TEST_USN');
}
void _setupWebViewController() {
_webViewController = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..addJavaScriptChannel(
'AdPopcornSSPBridge',
onMessageReceived: (JavaScriptMessage message) {
_handleJsMessage(message.message);
},
)
..loadHtmlString('''
<!DOCTYPE html>
<html>
<head>
<title>Ad Trigger</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h3>WebView에서 팝콘텐츠 호출</h3>
<button onclick="AdPopcornSSPBridge.postMessage('openPopContents')">팝콘텐츠 광고 보기</button>
</body>
</html>
''');
}
void _handleJsMessage(String message) {
print('WebView JS message: $message');
if (message == 'openPopContents') {
if (Platform.isAndroid) {
AdPopcornSSP.openPopContents('436579265', 'ZlOXGQbuuX');
}
else if (Platform.isIOS) {
AdPopcornSSP.openPopContents('537048423', 'bjJoq2QZcQ');
}
}
}
Widget _webViewWithBridge() {
return WebViewWidget(controller: _webViewController);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('AdPopcornSSP flutter WebView Demo'),
),
body: ListView(
children: [
SizedBox(height: 500, child: _webViewWithBridge()),
const Divider(),
],
),
),
);
}
}