Skip to content

Commit f1e60a5

Browse files
Merge pull request #4 from bparrishMines/horizontal
Add horizontal commit from flutter/plugins repo
2 parents b14e593 + 353da9b commit f1e60a5

File tree

11 files changed

+71
-12
lines changed

11 files changed

+71
-12
lines changed

packages/firebase_admob/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.0+4
2+
3+
* Add the ability to horizontally adjust the ads banner location by specifying a pixel offset from the centre.
4+
15
## 0.9.0+3
26

37
* Update google-services Android gradle plugin to 4.3.0 in documentation and examples.

packages/firebase_admob/README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ myBanner
122122
..show(
123123
// Positions the banner ad 60 pixels from the bottom of the screen
124124
anchorOffset: 60.0,
125+
// Positions the banner ad 10 pixels from the center of the screen to the right
126+
horizontalCenterOffset: 10.0,
127+
// Banner Position
128+
anchorType: AnchorType.bottom,
129+
);
130+
```
131+
132+
Ads must be loaded before they're shown.
133+
```dart
134+
myBanner
135+
// typically this happens well before the ad is shown
136+
..load()
137+
..show(
138+
// Positions the banner ad 60 pixels from the bottom of the screen
139+
anchorOffset: 60.0,
140+
// Positions the banner ad 10 pixels from the center of the screen to the left
141+
horizontalCenterOffset: -10.0,
125142
// Banner Position
126143
anchorType: AnchorType.bottom,
127144
);
@@ -133,6 +150,7 @@ myInterstitial
133150
..show(
134151
anchorType: AnchorType.bottom,
135152
anchorOffset: 0.0,
153+
horizontalCenterOffset: 0.0,
136154
);
137155
```
138156

@@ -186,7 +204,6 @@ method.
186204
This is just an initial version of the plugin. There are still some
187205
limitations:
188206

189-
- Banner ads have limited positioning functionality. They can be positioned at the top or the bottom of the screen and at a logical pixel offset from the edge.
190207
- Banner ads cannot be animated into view.
191208
- It's not possible to specify a banner ad's size.
192209
- There's no support for native ads.

packages/firebase_admob/android/src/main/java/io/flutter/plugins/firebaseadmob/FirebaseAdMobPlugin.java

+4
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ private void callShowAd(int id, MethodCall call, Result result) {
151151
if (call.argument("anchorOffset") != null) {
152152
ad.anchorOffset = Double.parseDouble((String) call.argument("anchorOffset"));
153153
}
154+
if (call.argument("horizontalCenterOffset") != null) {
155+
ad.horizontalCenterOffset =
156+
Double.parseDouble((String) call.argument("horizontalCenterOffset"));
157+
}
154158
if (call.argument("anchorType") != null) {
155159
ad.anchorType = call.argument("anchorType").equals("bottom") ? Gravity.BOTTOM : Gravity.TOP;
156160
}

packages/firebase_admob/android/src/main/java/io/flutter/plugins/firebaseadmob/MobileAd.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ abstract class MobileAd extends AdListener {
2828
final int id;
2929
Status status;
3030
double anchorOffset;
31+
double horizontalCenterOffset;
3132
int anchorType;
3233

3334
enum Status {
@@ -44,6 +45,7 @@ private MobileAd(int id, Activity activity, MethodChannel channel) {
4445
this.channel = channel;
4546
this.status = Status.CREATED;
4647
this.anchorOffset = 0.0;
48+
this.horizontalCenterOffset = 0.0;
4749
this.anchorType = Gravity.BOTTOM;
4850
allAds.put(id, this);
4951
}
@@ -160,10 +162,13 @@ void show() {
160162
content.addView(adView);
161163
final float scale = activity.getResources().getDisplayMetrics().density;
162164

165+
int left = horizontalCenterOffset > 0 ? (int) (horizontalCenterOffset * scale) : 0;
166+
int right =
167+
horizontalCenterOffset < 0 ? (int) (Math.abs(horizontalCenterOffset) * scale) : 0;
163168
if (anchorType == Gravity.BOTTOM) {
164-
content.setPadding(0, 0, 0, (int) (anchorOffset * scale));
169+
content.setPadding(left, 0, right, (int) (anchorOffset * scale));
165170
} else {
166-
content.setPadding(0, (int) (anchorOffset * scale), 0, 0);
171+
content.setPadding(left, (int) (anchorOffset * scale), right, 0);
167172
}
168173

169174
activity.addContentView(

packages/firebase_admob/example/lib/main.dart

+8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ class _MyAppState extends State<MyApp> {
9292
..load()
9393
..show();
9494
}),
95+
RaisedButton(
96+
child: const Text('SHOW BANNER WITH OFFSET'),
97+
onPressed: () {
98+
_bannerAd ??= createBannerAd();
99+
_bannerAd
100+
..load()
101+
..show(horizontalCenterOffset: -50, anchorOffset: 100);
102+
}),
95103
RaisedButton(
96104
child: const Text('REMOVE BANNER'),
97105
onPressed: () {

packages/firebase_admob/ios/Classes/FLTMobileAd.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ typedef enum : NSUInteger {
1919
- (FLTMobileAdStatus)status;
2020
- (void)loadWithAdUnitId:(NSString *)adUnitId targetingInfo:(NSDictionary *)targetingInfo;
2121
- (void)show;
22-
- (void)showAtOffset:(double)anchorOffset fromAnchor:(int)anchorType;
22+
- (void)showAtOffset:(double)anchorOffset
23+
hCenterOffset:(double)horizontalCenterOffset
24+
fromAnchor:(int)anchorType;
2325
- (void)dispose;
2426
@end
2527

packages/firebase_admob/ios/Classes/FLTMobileAd.m

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ @implementation FLTMobileAd
1414
FlutterMethodChannel *_channel;
1515
FLTMobileAdStatus _status;
1616
double _anchorOffset;
17+
double _horizontalCenterOffset;
1718
int _anchorType;
1819

1920
+ (void)initialize {
@@ -22,6 +23,7 @@ + (void)initialize {
2223
}
2324
_anchorType = 0;
2425
_anchorOffset = 0;
26+
_horizontalCenterOffset = 0;
2527

2628
if (statusToString == nil) {
2729
statusToString = @{
@@ -53,6 +55,7 @@ - (instancetype)initWithId:(NSNumber *)mobileAdId channel:(FlutterMethodChannel
5355
_channel = channel;
5456
_status = CREATED;
5557
_anchorOffset = 0;
58+
_horizontalCenterOffset = 0;
5659
_anchorType = 0;
5760
allAds[mobileAdId] = self;
5861
}
@@ -67,12 +70,15 @@ - (void)loadWithAdUnitId:(NSString *)adUnitId targetingInfo:(NSDictionary *)targ
6770
// Implemented by the Banner and Interstitial subclasses
6871
}
6972

70-
- (void)showAtOffset:(double)anchorOffset fromAnchor:(int)anchorType {
73+
- (void)showAtOffset:(double)anchorOffset
74+
hCenterOffset:(double)horizontalCenterOffset
75+
fromAnchor:(int)anchorType {
7176
_anchorType = anchorType;
7277
_anchorOffset = anchorOffset;
7378
if (_anchorType == 0) {
7479
_anchorOffset = -_anchorOffset;
7580
}
81+
_horizontalCenterOffset = horizontalCenterOffset;
7682
[self show];
7783
}
7884

@@ -146,7 +152,8 @@ - (void)show {
146152
if (@available(ios 11.0, *)) {
147153
UILayoutGuide *guide = screen.safeAreaLayoutGuide;
148154
[NSLayoutConstraint activateConstraints:@[
149-
[_banner.centerXAnchor constraintEqualToAnchor:guide.centerXAnchor],
155+
[_banner.centerXAnchor constraintEqualToAnchor:guide.centerXAnchor
156+
constant:_horizontalCenterOffset],
150157
[_banner.bottomAnchor
151158
constraintEqualToAnchor:_anchorType == 0 ? guide.bottomAnchor : guide.topAnchor
152159
constant:_anchorOffset]
@@ -161,7 +168,7 @@ - (void)show {
161168

162169
- (void)placeBannerPreIos11 {
163170
UIView *screen = [FLTMobileAd rootViewController].view;
164-
CGFloat x = screen.frame.size.width / 2 - _banner.frame.size.width / 2;
171+
CGFloat x = screen.frame.size.width / 2 - _banner.frame.size.width / 2 + _horizontalCenterOffset;
165172
CGFloat y;
166173
if (_anchorType == 0) {
167174
y = screen.frame.size.height - _banner.frame.size.height + _anchorOffset;

packages/firebase_admob/ios/Classes/FirebaseAdMobPlugin.m

+5-1
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,19 @@ - (void)callShowAd:(NSNumber *)mobileAdId
187187
}
188188

189189
double offset = 0.0;
190+
double horizontalCenterOffset = 0.0;
190191
int type = 0;
191192
if (call.arguments[@"anchorOffset"] != nil) {
192193
offset = [call.arguments[@"anchorOffset"] doubleValue];
193194
}
195+
if (call.arguments[@"horizontalCenterOffset"] != nil) {
196+
horizontalCenterOffset = [call.arguments[@"horizontalCenterOffset"] doubleValue];
197+
}
194198
if (call.arguments[@"anchorType"] != nil) {
195199
type = [call.arguments[@"anchorType"] isEqualToString:@"bottom"] ? 0 : 1;
196200
}
197201

198-
[ad showAtOffset:offset fromAnchor:type];
202+
[ad showAtOffset:offset hCenterOffset:horizontalCenterOffset fromAnchor:type];
199203
result([NSNumber numberWithBool:YES]);
200204
}
201205

packages/firebase_admob/lib/firebase_admob.dart

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import 'dart:async';
88
import 'dart:io' show Platform;
99

10+
import 'package:flutter/foundation.dart';
1011
import 'package:flutter/services.dart';
1112
import 'package:meta/meta.dart';
1213

@@ -221,11 +222,14 @@ abstract class MobileAd {
221222
/// anchorOffset is the logical pixel offset from the edge of the screen (default 0.0)
222223
/// anchorType place advert at top or bottom of screen (default bottom)
223224
Future<bool> show(
224-
{double anchorOffset = 0.0, AnchorType anchorType = AnchorType.bottom}) {
225+
{double anchorOffset = 0.0,
226+
double horizontalCenterOffset = 0.0,
227+
AnchorType anchorType = AnchorType.bottom}) {
225228
return _invokeBooleanMethod("showAd", <String, dynamic>{
226229
'id': id,
227230
'anchorOffset': anchorOffset.toString(),
228-
'anchorType': anchorType == AnchorType.top ? "top" : "bottom"
231+
'horizontalCenterOffset': horizontalCenterOffset.toString(),
232+
'anchorType': describeEnum(anchorType)
229233
});
230234
}
231235

packages/firebase_admob/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Firebase AdMob, supporting
33
banner, interstitial (full-screen), and rewarded video ads
44
author: Flutter Team <[email protected]>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_admob
6-
version: 0.9.0+3
6+
version: 0.9.0+4
77

88
flutter:
99
plugin:

packages/firebase_admob/test/firebase_admob_test.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void main() {
7373
isMethodCall('showAd', arguments: <String, dynamic>{
7474
'id': id,
7575
'anchorOffset': '0.0',
76+
'horizontalCenterOffset': '0.0',
7677
'anchorType': 'bottom',
7778
}),
7879
isMethodCall('disposeAd', arguments: <String, dynamic>{
@@ -92,7 +93,9 @@ void main() {
9293
expect(await interstitial.load(), true);
9394
expect(
9495
await interstitial.show(
95-
anchorOffset: 60.0, anchorType: AnchorType.top),
96+
anchorOffset: 60.0,
97+
horizontalCenterOffset: 10.0,
98+
anchorType: AnchorType.top),
9699
true);
97100
expect(await interstitial.dispose(), true);
98101

@@ -105,6 +108,7 @@ void main() {
105108
isMethodCall('showAd', arguments: <String, dynamic>{
106109
'id': id,
107110
'anchorOffset': '60.0',
111+
'horizontalCenterOffset': '10.0',
108112
'anchorType': 'top',
109113
}),
110114
isMethodCall('disposeAd', arguments: <String, dynamic>{

0 commit comments

Comments
 (0)