yandexads-flutter
115 строк · 2.9 Кб
1import 'package:flutter/foundation.dart';
2import 'package:flutter/material.dart';
3import 'package:flutter/services.dart';
4import 'package:flutter_yandex_ads/pigeons/native.dart';
5
6class YandexAdsNativeWidget extends StatefulWidget {
7YandexAdsNativeWidget({
8Key? key,
9required this.id,
10this.onAdLoaded,
11this.onAdFailedToLoad,
12this.onImpression,
13this.onAdClicked,
14this.onLeftApplication,
15this.onReturnedToApplication,
16this.width = 0,
17this.height = 0,
18}) : super(key: key);
19
20final String id;
21final int width;
22final int height;
23Function? onAdLoaded;
24Function? onAdFailedToLoad;
25Function? onImpression;
26Function? onAdClicked;
27Function? onLeftApplication;
28Function? onReturnedToApplication;
29
30@override
31State<YandexAdsNativeWidget> createState() => _YandexAdsNativeWidgetState();
32}
33
34class _YandexAdsNativeWidgetState extends State<YandexAdsNativeWidget> {
35bool loaded = false;
36
37@override
38void initState() {
39var native = YandexAdsNative();
40
41native.make(widget.id);
42
43if (widget.onAdLoaded != null) {
44native.onAdLoaded(widget.id).then((value) {
45setState(() {
46loaded = true;
47});
48widget.onAdLoaded!();
49});
50}
51
52if (widget.onAdFailedToLoad != null) {
53native.onAdFailedToLoad(widget.id).then((value) {
54widget.onAdFailedToLoad!(value);
55});
56}
57
58if (widget.onImpression != null) {
59native.onImpression(widget.id).then((value) {
60widget.onImpression!(value);
61});
62}
63
64if (widget.onAdClicked != null) {
65native.onAdClicked(widget.id).then((value) {
66widget.onAdClicked!();
67});
68}
69
70if (widget.onLeftApplication != null) {
71native.onLeftApplication(widget.id).then((value) {
72widget.onLeftApplication!();
73});
74}
75
76if (widget.onReturnedToApplication != null) {
77native.onReturnedToApplication(widget.id).then((value) {
78widget.onReturnedToApplication!();
79});
80}
81
82native.load(widget.id, widget.width, widget.height);
83}
84
85Widget build(BuildContext context) {
86const String viewType = 'yandex-ads-native';
87
88final Map<String, dynamic> creationParams = <String, dynamic>{
89'id': widget.id,
90};
91
92if (!loaded) {
93return const Text("loading..");
94} else {
95switch (defaultTargetPlatform) {
96case TargetPlatform.android:
97return AndroidView(
98viewType: viewType,
99layoutDirection: TextDirection.ltr,
100creationParams: creationParams,
101creationParamsCodec: const StandardMessageCodec(),
102);
103case TargetPlatform.iOS:
104return UiKitView(
105viewType: viewType,
106layoutDirection: TextDirection.ltr,
107creationParams: creationParams,
108creationParamsCodec: const StandardMessageCodec(),
109);
110default:
111throw UnsupportedError('Unsupported platform view');
112}
113}
114}
115}
116