Skip to content

Commit

Permalink
* 优化banner展示
Browse files Browse the repository at this point in the history
  • Loading branch information
gstory0404 committed Feb 14, 2025
1 parent 10da73e commit 9dd7b6e
Showing 1 changed file with 45 additions and 46 deletions.
91 changes: 45 additions & 46 deletions lib/bannerad/BannerAdView.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class FlutterUnionadBannerView extends StatefulWidget {
///
FlutterUnionadBannerView(
{Key? key,
required this.androidCodeId,
required this.iosCodeId,
required this.width,
required this.height,
this.callBack})
required this.androidCodeId,
required this.iosCodeId,
required this.width,
required this.height,
this.callBack})
: super(key: key);

@override
Expand All @@ -42,7 +42,6 @@ class _BannerAdViewState extends State<FlutterUnionadBannerView> {

//广告是否显示
bool _isShowAd = true;
late StatefulWidget _bannerView;

//宽高
double _width = 0;
Expand All @@ -54,49 +53,48 @@ class _BannerAdViewState extends State<FlutterUnionadBannerView> {
_isShowAd = true;
_width = widget.width;
_height = widget.height;
_bannerView = _loadBannerView();
}

_loadBannerView(){
@override
Widget build(BuildContext context) {
if (!_isShowAd) {
return Container();
}
if (defaultTargetPlatform == TargetPlatform.android) {
return AndroidView(
viewType: _viewType,
creationParams: {
"androidCodeId": widget.androidCodeId,
"width": widget.width,
"height": widget.height,
},
onPlatformViewCreated: _registerChannel,
creationParamsCodec: const StandardMessageCodec(),
return Container(
width: _width,
height: _height,
child: AndroidView(
viewType: _viewType,
creationParams: {
"androidCodeId": widget.androidCodeId,
"width": widget.width,
"height": widget.height,
},
onPlatformViewCreated: _registerChannel,
creationParamsCodec: const StandardMessageCodec(),
),
);
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
viewType: _viewType,
creationParams: {
"iosCodeId": widget.iosCodeId,
"width": widget.width,
"height": widget.height,
},
onPlatformViewCreated: _registerChannel,
creationParamsCodec: const StandardMessageCodec(),
return Container(
width: _width,
height: _height,
child: UiKitView(
viewType: _viewType,
creationParams: {
"iosCodeId": widget.iosCodeId,
"width": widget.width,
"height": widget.height,
},
onPlatformViewCreated: _registerChannel,
creationParamsCodec: const StandardMessageCodec(),
),
);
} else {
return Container();
}
}

@override
Widget build(BuildContext context) {
if (!_isShowAd) {
return Container();
}
return Container(
width: _width,
height: _height,
child: _bannerView,
);
}

//注册cannel
void _registerChannel(int id) {
_channel = MethodChannel("${_viewType}_$id");
Expand All @@ -106,19 +104,20 @@ class _BannerAdViewState extends State<FlutterUnionadBannerView> {
//监听原生view传值
Future<dynamic> _platformCallHandler(MethodCall call) async {
switch (call.method) {
//显示广告
//显示广告
case FlutterUnionadMethod.onShow:
Map map = call.arguments;
print(map);
_isShowAd = true;
_width = (map["width"]).toDouble();
_height = (map["height"]).toDouble();
if (mounted) {
setState(() {});
setState(() {
_isShowAd = true;
_width = (map["width"]).toDouble();
_height = (map["height"]).toDouble();
});
}
widget.callBack?.onShow!();
break;
//广告加载失败
//广告加载失败
case FlutterUnionadMethod.onFail:
if (mounted) {
setState(() {
Expand All @@ -127,7 +126,7 @@ class _BannerAdViewState extends State<FlutterUnionadBannerView> {
}
widget.callBack?.onFail!(call.arguments);
break;
//广告不感兴趣
//广告不感兴趣
case FlutterUnionadMethod.onDislike:
if (mounted) {
setState(() {
Expand All @@ -143,4 +142,4 @@ class _BannerAdViewState extends State<FlutterUnionadBannerView> {
break;
}
}
}
}

0 comments on commit 9dd7b6e

Please sign in to comment.