-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
admob_banner.dart
104 lines (92 loc) · 2.82 KB
/
admob_banner.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'admob.dart';
import 'admob_banner_controller.dart';
import 'admob_banner_size.dart';
import 'admob_events.dart';
class AdmobBanner extends StatefulWidget {
final String adUnitId;
final AdmobBannerSize adSize;
final void Function(AdmobAdEvent, Map<String, dynamic>) listener;
final void Function(AdmobBannerController) onBannerCreated;
AdmobBanner({
Key key,
@required this.adUnitId,
@required this.adSize,
this.listener,
this.onBannerCreated,
}) : super(key: key);
@override
_AdmobBannerState createState() => _AdmobBannerState();
}
class _AdmobBannerState extends State<AdmobBanner> {
final UniqueKey _key = UniqueKey();
AdmobBannerController _controller;
Future<Size> adSize;
@override
void initState() {
super.initState();
if (!widget.adSize.hasFixedSize) {
adSize = Admob.bannerSize(widget.adSize);
} else {
adSize = Future.value(Size(
widget.adSize.width.toDouble(),
widget.adSize.height.toDouble(),
));
}
}
@override
Widget build(BuildContext context) {
return FutureBuilder<Size>(
future: adSize,
builder: (context, snapshot) {
final adSize = snapshot.data;
if (adSize == null) {
return SizedBox.shrink();
}
if (defaultTargetPlatform == TargetPlatform.android) {
return SizedBox.fromSize(
size: adSize,
child: AndroidView(
key: _key,
viewType: 'admob_flutter/banner',
creationParams: <String, dynamic>{
'adUnitId': widget.adUnitId,
'adSize': widget.adSize.toMap,
},
creationParamsCodec: const StandardMessageCodec(),
onPlatformViewCreated: _onPlatformViewCreated,
),
);
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return SizedBox.fromSize(
size: adSize,
child: UiKitView(
key: _key,
viewType: 'admob_flutter/banner',
creationParams: <String, dynamic>{
'adUnitId': widget.adUnitId,
'adSize': widget.adSize.toMap,
},
creationParamsCodec: const StandardMessageCodec(),
onPlatformViewCreated: _onPlatformViewCreated,
),
);
}
return Text('$defaultTargetPlatform is not yet supported by the plugin');
},
);
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
void _onPlatformViewCreated(int id) {
_controller = AdmobBannerController(id, widget.listener);
if (widget.onBannerCreated != null) {
widget.onBannerCreated(_controller);
}
}
}