yandexads-flutter

Форк
0
290 строк · 7.9 Кб
1
import 'package:flutter/material.dart';
2
import 'package:flutter_yandex_ads/components/appopen.dart';
3
import 'package:flutter_yandex_ads/components/banner.dart';
4
import 'package:flutter_yandex_ads/components/interstitial.dart';
5
import 'package:flutter_yandex_ads/components/rewarded.dart';
6
import 'package:flutter_yandex_ads/pigeons/appopen.dart';
7
import 'package:flutter_yandex_ads/pigeons/banner.dart';
8
import 'package:flutter_yandex_ads/pigeons/interstitial.dart';
9
import 'package:flutter_yandex_ads/pigeons/native.dart';
10
import 'package:flutter_yandex_ads/pigeons/rewarded.dart';
11
import 'package:flutter_yandex_ads/widgets/banner.dart';
12
import 'package:flutter_yandex_ads/widgets/native.dart';
13
import 'package:flutter_yandex_ads/yandex.dart';
14

15
void main() {
16
  runApp(const App());
17
}
18

19
class App extends StatefulWidget {
20
  const App({Key? key}) : super(key: key);
21

22
  @override
23
  State<App> createState() => _AppState();
24
}
25

26
class _AppState extends State<App> {
27
  @override
28
  void initState() {
29
    super.initState();
30

31
    FlutterYandexAds.initialize();
32

33
    YandexAdsAppOpenComponent(
34
      id: "demo-appopenad-yandex",
35
      onAdLoaded: () {
36
        print('appopen onAdLoaded');
37
      },
38
      onAdFailedToLoad: (InterstitialError err) {
39
        print('appopen onAdFailedToLoad code: ${err.code}, description: ${err.description}');
40
      },
41
      onAdFailedToShow: (InterstitialError err) {
42
        print('appopen onAdFailedToShow description: ${err.description}');
43
      },
44
      onAdDismissed: () {
45
        print("appopen onAdDismissed");
46
      },
47
      onAdShown: () {
48
        print("appopen onAdShown");
49
      },
50
      onImpression: (AppOpenImpression? data) {
51
        print('appopen onImpression ${data?.data}');
52
      },
53
    );
54
  }
55

56
  @override
57
  Widget build(BuildContext context) {
58
    return MaterialApp(
59
      home: DefaultTabController(
60
        length: 4,
61
        child: SafeArea(
62
          child: Scaffold(
63
            appBar: AppBar(
64
              title: const Text('Yandex ADS'),
65
            ),
66
            bottomNavigationBar: const TabBar(
67
              tabs: [
68
                Tab(child: Text('Banner', style: TextStyle(color: Colors.black54, fontSize: 12))),
69
                Tab(child: Text('Interstitial', style: TextStyle(color: Colors.black54, fontSize: 12))),
70
                Tab(child: Text('Native', style: TextStyle(color: Colors.black54, fontSize: 12))),
71
                Tab(child: Text('Rewarded', style: TextStyle(color: Colors.black54, fontSize: 12))),
72
              ],
73
            ),
74
            body: const TabBarView(
75
              children: [
76
                BannerScreen(),
77
                InterstitialScreen(),
78
                NativeScreen(),
79
                RewardedScreen(),
80
              ],
81
            ),
82
          ),
83
        ),
84
      ),
85
    );
86
  }
87
}
88

89
class BannerScreen extends StatefulWidget {
90
  const BannerScreen({Key? key}) : super(key: key);
91

92
  @override
93
  _BannerScreenState createState() => _BannerScreenState();
94
}
95

96
class _BannerScreenState extends State<BannerScreen> {
97
  late YandexAdsBannerComponent banner;
98

99
  @override
100
  void initState() {
101
    super.initState();
102

103
    banner = YandexAdsBannerComponent(
104
      width: 320,
105
      height: 100,
106
      id: 'demo-banner-yandex',
107
      onAdLoaded: () {
108
        print('banner onAdLoaded');
109
      },
110
      onAdFailedToLoad: (BannerError err) {
111
        print('banner onAdFailedToLoad code: ${err.code}, description: ${err.description}');
112
      },
113
      onImpression: (BannerImpression? data) {
114
        print("banner onImpression ${data?.data}");
115
      },
116
      onAdClicked: () {
117
        print('banner onAdClicked');
118
      },
119
    );
120

121
    banner.load();
122
  }
123

124
  @override
125
  Widget build(BuildContext context) {
126
    return Column(
127
      mainAxisAlignment: MainAxisAlignment.center,
128
      children: [
129
        const Text('Banner'),
130
        SizedBox(
131
          height: 100,
132
          child: YandexAdsBannerWidget(
133
            banner: banner,
134
          ),
135
        ),
136
      ],
137
    );
138
  }
139
}
140

141
class NativeScreen extends StatefulWidget {
142
  const NativeScreen({Key? key}) : super(key: key);
143

144
  @override
145
  State<NativeScreen> createState() => _NativeScreenState();
146
}
147

148
class _NativeScreenState extends State<NativeScreen> {
149
  @override
150
  Widget build(BuildContext context) {
151
    return Column(
152
      mainAxisAlignment: MainAxisAlignment.center,
153
      children: [
154
        const Text('Native'),
155
        SizedBox(
156
          height: 300,
157
          child: YandexAdsNativeWidget(
158
            id: 'demo-native-app-yandex',
159
            onAdLoaded: () {
160
              print('native onAdLoaded');
161
            },
162
            onAdFailedToLoad: (BannerError err) {
163
              print('native onAdFailedToLoad code: ${err.code}, description: ${err.description}');
164
            },
165
            onImpression: (NativeImpression? data) {
166
              print("native onImpression ${data?.data}");
167
            },
168
            onAdClicked: () {
169
              print('native onAdClicked');
170
            },
171
          ),
172
        ),
173
      ],
174
    );
175
  }
176
}
177

178
class InterstitialScreen extends StatefulWidget {
179
  const InterstitialScreen({Key? key}) : super(key: key);
180

181
  @override
182
  _InterstitialScreenState createState() => _InterstitialScreenState();
183
}
184

185
class _InterstitialScreenState extends State<InterstitialScreen> {
186
  late YandexAdsInterstitialComponent interstitial;
187

188
  @override
189
  void initState() {
190
    super.initState();
191

192
    interstitial = YandexAdsInterstitialComponent(
193
      id: 'demo-interstitial-yandex',
194
      onAdLoaded: () {
195
        print('interstitial onAdLoaded');
196
      },
197
      onAdFailedToLoad: (InterstitialError err) {
198
        print('interstitial onAdFailedToLoad code: ${err.code}, description: ${err.description}');
199
      },
200
      onAdFailedToShow: (InterstitialError err) {
201
        print('interstitial onAdFailedToShow description: ${err.description}');
202
      },
203
      onAdDismissed: () {
204
        print("interstitial onAdDismissed");
205
      },
206
      onAdShown: () {
207
        print("interstitial onAdShown");
208
      },
209
      onImpression: (InterstitialImpression? data) {
210
        print('interstitial onImpression ${data?.data}');
211
      },
212
    );
213

214
    interstitial.load();
215
  }
216

217
  @override
218
  Widget build(BuildContext context) {
219
    return Column(
220
      mainAxisAlignment: MainAxisAlignment.center,
221
      children: [
222
        const Text('Interstitial'),
223
        ElevatedButton(
224
          onPressed: () {
225
            interstitial.show();
226
          },
227
          child: const Text('show'),
228
        ),
229
      ],
230
    );
231
  }
232
}
233

234
class RewardedScreen extends StatefulWidget {
235
  const RewardedScreen({Key? key}) : super(key: key);
236

237
  @override
238
  State<RewardedScreen> createState() => _RewardedScreenState();
239
}
240

241
class _RewardedScreenState extends State<RewardedScreen> {
242
  late YandexAdsRewardedComponent rewarded;
243

244
  @override
245
  void initState() {
246
    super.initState();
247

248
    rewarded = YandexAdsRewardedComponent(
249
        id: 'demo-rewarded-yandex',
250
        onAdLoaded: () {
251
          print('rewarded onAdLoaded');
252
        },
253
        onAdFailedToLoad: (RewardedError err) {
254
          print('rewarded onAdFailedToLoad code: ${err.code}, description: ${err.description}');
255
        },
256
        onAdFailedToShow: (RewardedError err) {
257
          print('rewarded onAdFailedToShow description: ${err.description}');
258
        },
259
        onAdDismissed: () {
260
          print("rewarded onAdDismissed");
261
        },
262
        onAdShown: () {
263
          print("rewarded onAdShown");
264
        },
265
        onImpression: (RewardedImpression? data) {
266
          print('rewarded onImpression ${data?.data}');
267
        },
268
        onRewarded: (RewardedEvent? data) {
269
          print('rewarded onRewarded amount ${data?.amount}, type ${data?.type}');
270
        });
271

272
    rewarded.load();
273
  }
274

275
  @override
276
  Widget build(BuildContext context) {
277
    return Column(
278
      mainAxisAlignment: MainAxisAlignment.center,
279
      children: [
280
        const Text('Rewarded'),
281
        ElevatedButton(
282
          onPressed: () {
283
            rewarded.show();
284
          },
285
          child: const Text('show'),
286
        ),
287
      ],
288
    );
289
  }
290
}
291

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

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

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

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