Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed View to use AndroidView Hybrid Mode #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions lib/ad/ad_banner.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

import 'package:facebook_audience_network/constants.dart';
Expand Down Expand Up @@ -90,39 +92,53 @@ class _FacebookBannerAdState extends State<FacebookBannerAd>
@override
Widget build(BuildContext context) {
super.build(context);
final Map<String, dynamic> creationParams = <String, dynamic>{
"id": widget.placementId,
"width": widget.bannerSize.width,
"height": widget.bannerSize.height,
};

if (defaultTargetPlatform == TargetPlatform.android) {
return Container(
height: containerHeight,
color: Colors.transparent,
child: AndroidView(
child: PlatformViewLink(
viewType: BANNER_AD_CHANNEL,
onPlatformViewCreated: _onBannerAdViewCreated,
creationParams: <String, dynamic>{
"id": widget.placementId,
"width": widget.bannerSize.width,
"height": widget.bannerSize.height,
surfaceFactory:
(BuildContext context, PlatformViewController controller) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: const <
Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (PlatformViewCreationParams params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: BANNER_AD_CHANNEL,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: StandardMessageCodec(),
onFocus: () {
params.onFocusChanged(true);
},
)
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..addOnPlatformViewCreatedListener(_onBannerAdViewCreated)
..create();
},
creationParamsCodec: StandardMessageCodec(),
),
);
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return Container(
height: containerHeight,
color: Colors.transparent,
child: Container(
width: widget.bannerSize.width.toDouble(),
child: Center(
child: UiKitView(
viewType: BANNER_AD_CHANNEL,
onPlatformViewCreated: _onBannerAdViewCreated,
creationParams: <String, dynamic>{
"id": widget.placementId,
"width": widget.bannerSize.width,
"height": widget.bannerSize.height,
},
creationParamsCodec: StandardMessageCodec(),
),
),
child: UiKitView(
viewType: BANNER_AD_CHANNEL,
onPlatformViewCreated: _onBannerAdViewCreated,
creationParams: creationParams,
creationParamsCodec: StandardMessageCodec(),
),
);
} else {
Expand All @@ -140,9 +156,8 @@ class _FacebookBannerAdState extends State<FacebookBannerAd>

void _onBannerAdViewCreated(int id) async {
final channel = MethodChannel('${BANNER_AD_CHANNEL}_$id');

channel.setMethodCallHandler((MethodCall call) {

channel.setMethodCallHandler((MethodCall call) {
switch (call.method) {
case ERROR_METHOD:
if (widget.listener != null)
Expand Down
122 changes: 59 additions & 63 deletions lib/ad/ad_native.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

import 'package:facebook_audience_network/constants.dart';
Expand Down Expand Up @@ -182,6 +184,38 @@ class _FacebookNativeAdState extends State<FacebookNativeAd>
}

Widget buildPlatformView(double width) {
final Map<String, dynamic> creationParams = <String, dynamic>{
"id": widget.placementId,
"ad_type": widget.adType.index,
"banner_ad":
widget.adType == NativeAdType.NATIVE_BANNER_AD ? true : false,
"height": widget.adType == NativeAdType.NATIVE_BANNER_AD
? widget.bannerAdSize.height
: widget.height,
"bg_color": widget.backgroundColor == null
? null
: _getHexStringFromColor(widget.backgroundColor!),
"title_color": widget.titleColor == null
? null
: _getHexStringFromColor(widget.titleColor!),
"desc_color": widget.descriptionColor == null
? null
: _getHexStringFromColor(widget.descriptionColor!),
"label_color": widget.labelColor == null
? null
: _getHexStringFromColor(widget.labelColor!),
"button_color": widget.buttonColor == null
? null
: _getHexStringFromColor(widget.buttonColor!),
"button_title_color": widget.buttonTitleColor == null
? null
: _getHexStringFromColor(widget.buttonTitleColor!),
"button_border_color": widget.buttonBorderColor == null
? null
: _getHexStringFromColor(widget.buttonBorderColor!),
"is_media_cover": widget.isMediaCover,
};

if (defaultTargetPlatform == TargetPlatform.android) {
return Container(
width: width,
Expand All @@ -190,38 +224,31 @@ class _FacebookNativeAdState extends State<FacebookNativeAd>
widget.adType == NativeAdType.NATIVE_AD_VERTICAL
? widget.height
: widget.bannerAdSize.height!.toDouble(),
child: AndroidView(
child: PlatformViewLink(
viewType: NATIVE_AD_CHANNEL,
onPlatformViewCreated: _onNativeAdViewCreated,
creationParamsCodec: StandardMessageCodec(),
creationParams: <String, dynamic>{
"id": widget.placementId,
"banner_ad":
widget.adType == NativeAdType.NATIVE_BANNER_AD ? true : false,
// height param is only for Banner Ads. Native Ad's height is
// governed by container.
"height": widget.bannerAdSize.height,
"bg_color": widget.backgroundColor == null
? null
: _getHexStringFromColor(widget.backgroundColor!),
"title_color": widget.titleColor == null
? null
: _getHexStringFromColor(widget.titleColor!),
"desc_color": widget.descriptionColor == null
? null
: _getHexStringFromColor(widget.descriptionColor!),
"label_color": widget.labelColor == null
? null
: _getHexStringFromColor(widget.labelColor!),
"button_color": widget.buttonColor == null
? null
: _getHexStringFromColor(widget.buttonColor!),
"button_title_color": widget.buttonTitleColor == null
? null
: _getHexStringFromColor(widget.buttonTitleColor!),
"button_border_color": widget.buttonBorderColor == null
? null
: _getHexStringFromColor(widget.buttonBorderColor!),
surfaceFactory:
(BuildContext context, PlatformViewController controller) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: const <
Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (PlatformViewCreationParams params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: NATIVE_AD_CHANNEL,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: StandardMessageCodec(),
onFocus: () {
params.onFocusChanged(true);
},
)
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..addOnPlatformViewCreatedListener(_onNativeAdViewCreated)
..create();
},
),
);
Expand All @@ -235,37 +262,7 @@ class _FacebookNativeAdState extends State<FacebookNativeAd>
viewType: _getChannelRegisterId(),
onPlatformViewCreated: _onNativeAdViewCreated,
creationParamsCodec: StandardMessageCodec(),
creationParams: <String, dynamic>{
"id": widget.placementId,
"ad_type": widget.adType.index,
"banner_ad":
widget.adType == NativeAdType.NATIVE_BANNER_AD ? true : false,
"height": widget.adType == NativeAdType.NATIVE_BANNER_AD
? widget.bannerAdSize.height
: widget.height,
"bg_color": widget.backgroundColor == null
? null
: _getHexStringFromColor(widget.backgroundColor!),
"title_color": widget.titleColor == null
? null
: _getHexStringFromColor(widget.titleColor!),
"desc_color": widget.descriptionColor == null
? null
: _getHexStringFromColor(widget.descriptionColor!),
"label_color": widget.labelColor == null
? null
: _getHexStringFromColor(widget.labelColor!),
"button_color": widget.buttonColor == null
? null
: _getHexStringFromColor(widget.buttonColor!),
"button_title_color": widget.buttonTitleColor == null
? null
: _getHexStringFromColor(widget.buttonTitleColor!),
"button_border_color": widget.buttonBorderColor == null
? null
: _getHexStringFromColor(widget.buttonBorderColor!),
"is_media_cover": widget.isMediaCover,
},
creationParams: creationParams,
),
);
} else {
Expand Down Expand Up @@ -327,7 +324,6 @@ class _FacebookNativeAdState extends State<FacebookNativeAd>
}

return Future<dynamic>.value(true);

});
}
}