-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.dart
194 lines (185 loc) · 5.65 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import 'package:flutter/material.dart';
import 'package:gtads/gtads.dart';
import 'package:gtads_example/banner_page.dart';
import 'package:gtads_example/config.dart';
import 'package:gtads_example/native_page.dart';
import 'package:gtads_example/splash_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Index(),
);
}
}
class Index extends StatefulWidget {
@override
_IndexState createState() => _IndexState();
}
class _IndexState extends State<Index> {
List<Map<String, bool>> initAd = [];
@override
void initState() {
super.initState();
init();
}
Future<void> init() async {
//添加广告支持
GTAds.addProviders(Config.providers);
//初始化广告
initAd = await GTAds.init(isDebug: true);
print("广告加载结果 $initAd");
// loadInsertAd();
setState(() {});
}
void loadInsertAd() async{
print("开始加载广告");
await GTAds.insertAd(
codes: Config.insertCodes,
isFull: false,
//超时时间 当广告失败后会依次重试其他广告 直至所有广告均加载失败 设置超时时间可提前取消
timeout: 10,
callBack: GTAdsCallBack(
onShow: (code) {
print("插屏广告显示 ${code.toJson()}");
},
onFail: (code, message) {
print("插屏广告失败 ${code?.toJson()} $message");
},
onClick: (code) {
print("插屏广告点击 ${code.toJson()}");
},
onClose: (code) {
print("插屏广告关闭 ${code.toJson()}");
},
onTimeout: () {
print("插屏广告加载超时");
},
onEnd: () {
print("插屏广告所有广告位都加载失败");
},
));
}
void loadRewardAd() async{
await GTAds.rewardAd(
codes: Config.rewardCodes,
//奖励名称
rewardName: "100金币",
//奖励数量
rewardAmount: 100,
//用户id
userId: "user100",
//扩展参数
customData: "123",
model: GTAdsModel.RANDOM,
//超时时间 当广告失败后会依次重试其他广告 直至所有广告均加载失败 设置超时时间可提前取消
timeout: 6,
callBack: GTAdsCallBack(
onShow: (code) {
print("激励广告显示 ${code.toJson()}");
},
onFail: (code, message) {
print("激励广告失败 ${code?.toJson()} $message");
},
onClick: (code) {
print("激励广告点击 ${code.toJson()}");
},
onClose: (code) {
print("激励广告关闭 ${code.toJson()}");
},
onVerify:
(code, verify, transId, rewardName, rewardAmount) {
print(
"激励广告奖励验证 ${code.toJson()} $verify $transId $rewardName $rewardAmount");
},
onExpand: (code, param) {
print("激励广告自定义参数 ${code.toJson()} $param");
},
onTimeout: () {
print("激励广告加载超时");
},
onEnd: () {
print("激励广告所有广告位都加载失败");
},
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('GTAds聚合广告插件'),
),
body: Center(
child: SingleChildScrollView(
child: Column(
children: [
Text("初始化结果$initAd"),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: const Text('横幅广告'),
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const BannerPage(),
),
);
},
),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: const Text('激励广告'),
onPressed: () async {
loadRewardAd();
},
),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: const Text('插屏广告'),
onPressed: () async {
print("广告位数量 ${Config.insertCodes.length}");
loadInsertAd();
},
),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: const Text('开屏广告'),
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SplashPage(),
),
);
},
),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: const Text('信息流广告'),
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const NativePage(),
),
);
},
),
],
),
),
),
),
);
}
}