yandexads-flutter
290 строк · 7.9 Кб
1import 'package:flutter/material.dart';
2import 'package:flutter_yandex_ads/components/appopen.dart';
3import 'package:flutter_yandex_ads/components/banner.dart';
4import 'package:flutter_yandex_ads/components/interstitial.dart';
5import 'package:flutter_yandex_ads/components/rewarded.dart';
6import 'package:flutter_yandex_ads/pigeons/appopen.dart';
7import 'package:flutter_yandex_ads/pigeons/banner.dart';
8import 'package:flutter_yandex_ads/pigeons/interstitial.dart';
9import 'package:flutter_yandex_ads/pigeons/native.dart';
10import 'package:flutter_yandex_ads/pigeons/rewarded.dart';
11import 'package:flutter_yandex_ads/widgets/banner.dart';
12import 'package:flutter_yandex_ads/widgets/native.dart';
13import 'package:flutter_yandex_ads/yandex.dart';
14
15void main() {
16runApp(const App());
17}
18
19class App extends StatefulWidget {
20const App({Key? key}) : super(key: key);
21
22@override
23State<App> createState() => _AppState();
24}
25
26class _AppState extends State<App> {
27@override
28void initState() {
29super.initState();
30
31FlutterYandexAds.initialize();
32
33YandexAdsAppOpenComponent(
34id: "demo-appopenad-yandex",
35onAdLoaded: () {
36print('appopen onAdLoaded');
37},
38onAdFailedToLoad: (InterstitialError err) {
39print('appopen onAdFailedToLoad code: ${err.code}, description: ${err.description}');
40},
41onAdFailedToShow: (InterstitialError err) {
42print('appopen onAdFailedToShow description: ${err.description}');
43},
44onAdDismissed: () {
45print("appopen onAdDismissed");
46},
47onAdShown: () {
48print("appopen onAdShown");
49},
50onImpression: (AppOpenImpression? data) {
51print('appopen onImpression ${data?.data}');
52},
53);
54}
55
56@override
57Widget build(BuildContext context) {
58return MaterialApp(
59home: DefaultTabController(
60length: 4,
61child: SafeArea(
62child: Scaffold(
63appBar: AppBar(
64title: const Text('Yandex ADS'),
65),
66bottomNavigationBar: const TabBar(
67tabs: [
68Tab(child: Text('Banner', style: TextStyle(color: Colors.black54, fontSize: 12))),
69Tab(child: Text('Interstitial', style: TextStyle(color: Colors.black54, fontSize: 12))),
70Tab(child: Text('Native', style: TextStyle(color: Colors.black54, fontSize: 12))),
71Tab(child: Text('Rewarded', style: TextStyle(color: Colors.black54, fontSize: 12))),
72],
73),
74body: const TabBarView(
75children: [
76BannerScreen(),
77InterstitialScreen(),
78NativeScreen(),
79RewardedScreen(),
80],
81),
82),
83),
84),
85);
86}
87}
88
89class BannerScreen extends StatefulWidget {
90const BannerScreen({Key? key}) : super(key: key);
91
92@override
93_BannerScreenState createState() => _BannerScreenState();
94}
95
96class _BannerScreenState extends State<BannerScreen> {
97late YandexAdsBannerComponent banner;
98
99@override
100void initState() {
101super.initState();
102
103banner = YandexAdsBannerComponent(
104width: 320,
105height: 100,
106id: 'demo-banner-yandex',
107onAdLoaded: () {
108print('banner onAdLoaded');
109},
110onAdFailedToLoad: (BannerError err) {
111print('banner onAdFailedToLoad code: ${err.code}, description: ${err.description}');
112},
113onImpression: (BannerImpression? data) {
114print("banner onImpression ${data?.data}");
115},
116onAdClicked: () {
117print('banner onAdClicked');
118},
119);
120
121banner.load();
122}
123
124@override
125Widget build(BuildContext context) {
126return Column(
127mainAxisAlignment: MainAxisAlignment.center,
128children: [
129const Text('Banner'),
130SizedBox(
131height: 100,
132child: YandexAdsBannerWidget(
133banner: banner,
134),
135),
136],
137);
138}
139}
140
141class NativeScreen extends StatefulWidget {
142const NativeScreen({Key? key}) : super(key: key);
143
144@override
145State<NativeScreen> createState() => _NativeScreenState();
146}
147
148class _NativeScreenState extends State<NativeScreen> {
149@override
150Widget build(BuildContext context) {
151return Column(
152mainAxisAlignment: MainAxisAlignment.center,
153children: [
154const Text('Native'),
155SizedBox(
156height: 300,
157child: YandexAdsNativeWidget(
158id: 'demo-native-app-yandex',
159onAdLoaded: () {
160print('native onAdLoaded');
161},
162onAdFailedToLoad: (BannerError err) {
163print('native onAdFailedToLoad code: ${err.code}, description: ${err.description}');
164},
165onImpression: (NativeImpression? data) {
166print("native onImpression ${data?.data}");
167},
168onAdClicked: () {
169print('native onAdClicked');
170},
171),
172),
173],
174);
175}
176}
177
178class InterstitialScreen extends StatefulWidget {
179const InterstitialScreen({Key? key}) : super(key: key);
180
181@override
182_InterstitialScreenState createState() => _InterstitialScreenState();
183}
184
185class _InterstitialScreenState extends State<InterstitialScreen> {
186late YandexAdsInterstitialComponent interstitial;
187
188@override
189void initState() {
190super.initState();
191
192interstitial = YandexAdsInterstitialComponent(
193id: 'demo-interstitial-yandex',
194onAdLoaded: () {
195print('interstitial onAdLoaded');
196},
197onAdFailedToLoad: (InterstitialError err) {
198print('interstitial onAdFailedToLoad code: ${err.code}, description: ${err.description}');
199},
200onAdFailedToShow: (InterstitialError err) {
201print('interstitial onAdFailedToShow description: ${err.description}');
202},
203onAdDismissed: () {
204print("interstitial onAdDismissed");
205},
206onAdShown: () {
207print("interstitial onAdShown");
208},
209onImpression: (InterstitialImpression? data) {
210print('interstitial onImpression ${data?.data}');
211},
212);
213
214interstitial.load();
215}
216
217@override
218Widget build(BuildContext context) {
219return Column(
220mainAxisAlignment: MainAxisAlignment.center,
221children: [
222const Text('Interstitial'),
223ElevatedButton(
224onPressed: () {
225interstitial.show();
226},
227child: const Text('show'),
228),
229],
230);
231}
232}
233
234class RewardedScreen extends StatefulWidget {
235const RewardedScreen({Key? key}) : super(key: key);
236
237@override
238State<RewardedScreen> createState() => _RewardedScreenState();
239}
240
241class _RewardedScreenState extends State<RewardedScreen> {
242late YandexAdsRewardedComponent rewarded;
243
244@override
245void initState() {
246super.initState();
247
248rewarded = YandexAdsRewardedComponent(
249id: 'demo-rewarded-yandex',
250onAdLoaded: () {
251print('rewarded onAdLoaded');
252},
253onAdFailedToLoad: (RewardedError err) {
254print('rewarded onAdFailedToLoad code: ${err.code}, description: ${err.description}');
255},
256onAdFailedToShow: (RewardedError err) {
257print('rewarded onAdFailedToShow description: ${err.description}');
258},
259onAdDismissed: () {
260print("rewarded onAdDismissed");
261},
262onAdShown: () {
263print("rewarded onAdShown");
264},
265onImpression: (RewardedImpression? data) {
266print('rewarded onImpression ${data?.data}');
267},
268onRewarded: (RewardedEvent? data) {
269print('rewarded onRewarded amount ${data?.amount}, type ${data?.type}');
270});
271
272rewarded.load();
273}
274
275@override
276Widget build(BuildContext context) {
277return Column(
278mainAxisAlignment: MainAxisAlignment.center,
279children: [
280const Text('Rewarded'),
281ElevatedButton(
282onPressed: () {
283rewarded.show();
284},
285child: const Text('show'),
286),
287],
288);
289}
290}
291