import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'ad.dart'; /// Widget for banner ad class BannerAdView extends StatelessWidget { final String adId; final double width; final double height; final AdCallback onSjmAdLoaded; final AdCallback onSjmAdShow; final AdCallback onSjmAdClicked; final AdCallback onSjmAdClosed; final AdErrorCallback onSjmAdError; BannerAdView( {Key key, this.adId, this.width, this.height, this.onSjmAdLoaded, this.onSjmAdShow, this.onSjmAdClicked, this.onSjmAdClosed, this.onSjmAdError}) : super(key: key); @override Widget build(BuildContext context) { Widget banner; if (defaultTargetPlatform == TargetPlatform.android) { banner = AndroidView( viewType: 'flutter_adsjm_plugin/banner', creationParams: { "adId": adId, "width": width, "height": height, }, creationParamsCodec: const StandardMessageCodec(), onPlatformViewCreated: _onPlatformViewCreated, ); } else if (defaultTargetPlatform == TargetPlatform.iOS) { banner = UiKitView( viewType: 'flutter_adsjm_plugin/banner', creationParams: { "adId": adId, "width": width, "height": height, }, creationParamsCodec: const StandardMessageCodec(), onPlatformViewCreated: _onPlatformViewCreated, ); } else { banner = Text("Not supported"); } return Container( width: width, height: height, child: banner, ); } void _onPlatformViewCreated(int id) { EventChannel eventChannel = EventChannel("flutter_adsjm_plugin/banner_event_$id"); eventChannel.receiveBroadcastStream().listen((event) { switch (event["event"]) { case "onSjmAdLoaded": onSjmAdLoaded?.call(event["id"]); break; case "onSjmAdShow": onSjmAdShow?.call(event["id"]); break; case "onSjmAdClicked": onSjmAdClicked?.call(event["id"]); break; case "onSjmAdClosed": onSjmAdClosed?.call(event["id"]); break; case "onSjmAdError": onSjmAdError?.call(event["id"], event["code"], event["message"]); break; } }); } }