import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/services.dart'; import 'common_ui.dart'; class ExpressAdController { VoidCallback? refresh; } //TODO 兼容android class CSJEXpressAd extends StatefulWidget { final String pid; final double width; final double height; final ExpressAdController? controller; final VoidCallback? close; final VoidCallback? loadFail; CSJEXpressAd(this.pid, this.width, this.height, {this.controller, this.close, this.loadFail}); @override State createState() => _CSJEXpressAdState(); } class _CSJEXpressAdState extends State { MethodChannel? _expressAdChannel; @override void initState() { super.initState(); if (widget.controller != null) { widget.controller!.refresh = () { if (_expressAdChannel != null) { _expressAdChannel!.invokeMethod("refresh"); } }; } } @override Widget build(BuildContext context) { return KeepAliveWrapper( child: Platform.isAndroid ? AndroidView( viewType: "ad-csj-express-view", layoutDirection: TextDirection.ltr, onPlatformViewCreated: onPlatformViewCreated, creationParams: { "width": widget.width, "height": widget.height, "pid": widget.pid }, creationParamsCodec: const StandardMessageCodec(), ) : UiKitView( viewType: "ad-csj-express-view", layoutDirection: TextDirection.ltr, onPlatformViewCreated: onPlatformViewCreated, creationParams: { "width": widget.width, "height": widget.height, "pid": widget.pid }, creationParamsCodec: const StandardMessageCodec(), )); } Future onPlatformViewCreated(id) async { _expressAdChannel = MethodChannel("ad-csj-express-view-$id"); _expressAdChannel!.setMethodCallHandler((call) { if ("close" == call.method) { if (widget.close != null) { widget.close!(); } } else if ("loadFail" == call.method) { if (widget.loadFail != null) { widget.loadFail!(); } } return Future.value(""); }); } } class GDTEXpressAd extends StatefulWidget { final String pid; final double width; final double height; final ExpressAdController? controller; final VoidCallback? close; final VoidCallback? loadFail; GDTEXpressAd(this.pid, this.width, this.height, {this.controller, this.close, this.loadFail}); @override State createState() => _GDTEXpressAdState(); } class _GDTEXpressAdState extends State { MethodChannel? _expressAdChannel; @override void initState() { super.initState(); if (widget.controller != null) { widget.controller!.refresh = () { if (_expressAdChannel != null) { _expressAdChannel!.invokeMethod("refresh"); } }; } } @override Widget build(BuildContext context) { return KeepAliveWrapper( child: UiKitView( viewType: "ad-gdt-express-view", layoutDirection: TextDirection.ltr, onPlatformViewCreated: onPlatformViewCreated, creationParams: { "width": widget.width, "height": widget.height, "pid": widget.pid }, creationParamsCodec: const StandardMessageCodec(), )); } Future onPlatformViewCreated(id) async { _expressAdChannel = MethodChannel("ad-gdt-express-view-$id"); _expressAdChannel!.setMethodCallHandler((call) { if ("close" == call.method) { if (widget.close != null) { widget.close!(); } } else if ("loadFail" == call.method) { if (widget.loadFail != null) { widget.loadFail!(); } } return Future.value(""); }); } }