yandexads-flutter

Форк
0
115 строк · 2.9 Кб
1
import 'package:flutter/foundation.dart';
2
import 'package:flutter/material.dart';
3
import 'package:flutter/services.dart';
4
import 'package:flutter_yandex_ads/pigeons/native.dart';
5

6
class YandexAdsNativeWidget extends StatefulWidget {
7
  YandexAdsNativeWidget({
8
    Key? key,
9
    required this.id,
10
    this.onAdLoaded,
11
    this.onAdFailedToLoad,
12
    this.onImpression,
13
    this.onAdClicked,
14
    this.onLeftApplication,
15
    this.onReturnedToApplication,
16
    this.width = 0,
17
    this.height = 0,
18
  }) : super(key: key);
19

20
  final String id;
21
  final int width;
22
  final int height;
23
  Function? onAdLoaded;
24
  Function? onAdFailedToLoad;
25
  Function? onImpression;
26
  Function? onAdClicked;
27
  Function? onLeftApplication;
28
  Function? onReturnedToApplication;
29

30
  @override
31
  State<YandexAdsNativeWidget> createState() => _YandexAdsNativeWidgetState();
32
}
33

34
class _YandexAdsNativeWidgetState extends State<YandexAdsNativeWidget> {
35
  bool loaded = false;
36

37
  @override
38
  void initState() {
39
    var native = YandexAdsNative();
40

41
    native.make(widget.id);
42

43
    if (widget.onAdLoaded != null) {
44
      native.onAdLoaded(widget.id).then((value) {
45
        setState(() {
46
          loaded = true;
47
        });
48
        widget.onAdLoaded!();
49
      });
50
    }
51

52
    if (widget.onAdFailedToLoad != null) {
53
      native.onAdFailedToLoad(widget.id).then((value) {
54
        widget.onAdFailedToLoad!(value);
55
      });
56
    }
57

58
    if (widget.onImpression != null) {
59
      native.onImpression(widget.id).then((value) {
60
        widget.onImpression!(value);
61
      });
62
    }
63

64
    if (widget.onAdClicked != null) {
65
      native.onAdClicked(widget.id).then((value) {
66
        widget.onAdClicked!();
67
      });
68
    }
69

70
    if (widget.onLeftApplication != null) {
71
      native.onLeftApplication(widget.id).then((value) {
72
        widget.onLeftApplication!();
73
      });
74
    }
75

76
    if (widget.onReturnedToApplication != null) {
77
      native.onReturnedToApplication(widget.id).then((value) {
78
        widget.onReturnedToApplication!();
79
      });
80
    }
81

82
    native.load(widget.id, widget.width, widget.height);
83
  }
84

85
  Widget build(BuildContext context) {
86
    const String viewType = 'yandex-ads-native';
87

88
    final Map<String, dynamic> creationParams = <String, dynamic>{
89
      'id': widget.id,
90
    };
91

92
    if (!loaded) {
93
      return const Text("loading..");
94
    } else {
95
      switch (defaultTargetPlatform) {
96
        case TargetPlatform.android:
97
          return AndroidView(
98
            viewType: viewType,
99
            layoutDirection: TextDirection.ltr,
100
            creationParams: creationParams,
101
            creationParamsCodec: const StandardMessageCodec(),
102
          );
103
        case TargetPlatform.iOS:
104
          return UiKitView(
105
            viewType: viewType,
106
            layoutDirection: TextDirection.ltr,
107
            creationParams: creationParams,
108
            creationParamsCodec: const StandardMessageCodec(),
109
          );
110
        default:
111
          throw UnsupportedError('Unsupported platform view');
112
      }
113
    }
114
  }
115
}
116

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.