Skip to content

Commit 66ceaf3

Browse files
author
Bhavik Makwana
committed
Minor code refactoring,
Added banner ad
1 parent b436733 commit 66ceaf3

File tree

12 files changed

+112
-18
lines changed

12 files changed

+112
-18
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ build/
2323
/android/misc/api-8857670977859340350-9644-c1e382e20443.json
2424
.flutter-plugins-dependencies
2525
.dart_tool/
26+
/android/app/flutter_playground.jks
27+
/upload_certificate.pem
28+
/lib/ads/ad_helper.dart
29+
/android/app/release/
30+
/ios/Flutter/flutter_export_environment.sh

android/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
/captures
1010
GeneratedPluginRegistrant.java
1111
/misc/flutterplayground.jks
12-
/key.properties
12+
/key.properties
13+
/app/src/main/res/values/strings.xml

android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ android {
4646
defaultConfig {
4747
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
4848
applicationId "com.ibhavikmakwana.flutterplayground"
49-
minSdkVersion 20
49+
minSdkVersion 21
5050
targetSdkVersion 30
5151
versionCode flutterVersionCode.toInteger()
5252
versionName flutterVersionName

android/app/src/main/AndroidManifest.xml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
-->
88
<uses-permission android:name="android.permission.INTERNET"/>
99

10+
<queries>
11+
<intent>
12+
<action android:name="android.intent.action.VIEW" />
13+
<data android:scheme="https" />
14+
</intent>
15+
<intent>
16+
<action android:name="android.intent.action.DIAL" />
17+
<data android:scheme="tel" />
18+
</intent>
19+
<intent>
20+
<action android:name="android.intent.action.SEND" />
21+
<data android:mimeType="*/*" />
22+
</intent>
23+
</queries>
24+
1025
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
1126
calls FlutterMain.startInitialization(this); in its onCreate method.
1227
In most cases you can leave this as-is, but you if you want to provide
@@ -41,8 +56,9 @@
4156
<category android:name="android.intent.category.LAUNCHER"/>
4257
</intent-filter>
4358
</activity>
44-
<!--<meta-data-->
45-
<!--android:name="com.google.android.gms.ads.APPLICATION_ID"-->
46-
<!--android:value="[ADMOB_APP_ID]" />-->
59+
60+
<meta-data
61+
android:name="com.google.android.gms.ads.APPLICATION_ID"
62+
android:value="@string/ad_app_pub"/>
4763
</application>
4864
</manifest>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<!--<string name="ad_app_pub">ca-app-pub-3940256099942544~3347511713</string>-->
4+
</resources>

assets/images/ic_github.svg

Lines changed: 1 addition & 0 deletions
Loading

lib/ads/test_ad_helper.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'dart:io';
2+
3+
class AdHelper {
4+
static String get bannerAdUnitId {
5+
if (Platform.isAndroid) {
6+
return "ca-app-pub-3940256099942544/6300978111";
7+
} else {
8+
throw new UnsupportedError("Unsupported platform");
9+
}
10+
}
11+
}

lib/main.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
import 'package:firebase_core/firebase_core.dart';
66
import 'package:flutter/material.dart';
7-
import 'package:flutter_playground/store/theme_store.dart';
8-
import 'package:provider/provider.dart';
9-
107
import 'package:flutter_playground/my_app.dart';
8+
import 'package:flutter_playground/store/theme_store.dart';
119
import 'package:flutter_playground/values/imports.dart';
10+
import 'package:provider/provider.dart';
1211

1312
Future<void> main() async {
1413
WidgetsFlutterBinding.ensureInitialized();

lib/ui/staggeredanimation/StaggerDemo.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class StaggerAnimation extends StatelessWidget {
101101
final Animation<double> width;
102102
final Animation<double> height;
103103
final Animation<EdgeInsets> padding;
104-
final Animation<BorderRadius> borderRadius;
104+
final Animation<BorderRadius?> borderRadius;
105105
final Animation<Color?> color;
106106

107107
// This function is called each time the controller "ticks" a frame.

lib/ui_ux/home_page/my_home_page.dart

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
import 'dart:developer';
2+
13
import 'package:flutter/material.dart';
4+
import 'package:flutter_playground/ads/ad_helper.dart';
25
import 'package:flutter_playground/store/theme_store.dart';
6+
import 'package:flutter_playground/ui_ux/home_page/my_home_page_store.dart';
37
import 'package:flutter_playground/ui_ux/open_source_licenses.dart';
48
import 'package:flutter_playground/ui_ux/search_widget.dart';
59
import 'package:flutter_playground/values/assets.dart';
610
import 'package:flutter_playground/values/imports.dart';
11+
import 'package:flutter_svg/flutter_svg.dart';
12+
import 'package:google_mobile_ads/google_mobile_ads.dart';
713
import 'package:provider/provider.dart';
814
import 'package:url_launcher/url_launcher.dart';
915

10-
import 'package:flutter_playground/ui_ux/home_page/my_home_page_store.dart';
11-
1216
class MyHomePage extends StatefulWidget {
1317
const MyHomePage({Key? key, required this.title}) : super(key: key);
1418
final String title;
@@ -19,12 +23,37 @@ class MyHomePage extends StatefulWidget {
1923

2024
class _MyHomePageState extends State<MyHomePage> {
2125
MyHomePageStore? store;
26+
late BannerAd _ad;
27+
bool _isAdLoaded = false;
2228

2329
@override
2430
void initState() {
2531
super.initState();
32+
_initGoogleMobileAds();
33+
_ad = BannerAd(
34+
adUnitId: AdHelper.bannerAdUnitId,
35+
size: AdSize.banner,
36+
request: const AdRequest(),
37+
listener: BannerAdListener(
38+
onAdLoaded: (_) {
39+
setState(() {
40+
_isAdLoaded = true;
41+
});
42+
},
43+
onAdFailedToLoad: (ad, error) {
44+
// Releases an ad resource when it fails to load
45+
ad.dispose();
46+
47+
log('Ad load failed (code=${error.code} message=${error.message})');
48+
},
49+
),
50+
);
51+
_ad.load();
2652
}
2753

54+
Future<InitializationStatus> _initGoogleMobileAds() =>
55+
MobileAds.instance.initialize();
56+
2857
@override
2958
void didChangeDependencies() {
3059
store ??= Provider.of<MyHomePageStore>(context);
@@ -33,6 +62,7 @@ class _MyHomePageState extends State<MyHomePage> {
3362

3463
@override
3564
void dispose() {
65+
_ad.dispose();
3666
super.dispose();
3767
}
3868

@@ -72,9 +102,23 @@ class _MyHomePageState extends State<MyHomePage> {
72102
SafeArea(
73103
child: Container(
74104
margin: const EdgeInsets.all(16),
75-
child: Text(
76-
widget.title,
77-
style: Theme.of(context).textTheme.headline6,
105+
child: Row(
106+
children: [
107+
Expanded(
108+
child: Text(
109+
widget.title,
110+
style: Theme.of(context).textTheme.headline6,
111+
),
112+
),
113+
IconButton(
114+
onPressed: () {
115+
_launchURL(
116+
'https://github.com/ibhavikmakwana/FlutterPlayground',
117+
);
118+
},
119+
icon: SvgPicture.asset(Assets.icGithub),
120+
),
121+
],
78122
),
79123
),
80124
),
@@ -92,6 +136,15 @@ class _MyHomePageState extends State<MyHomePage> {
92136
),
93137
),
94138
),
139+
if (_isAdLoaded)
140+
Center(
141+
child: Container(
142+
width: _ad.size.width.toDouble(),
143+
height: 72.0,
144+
alignment: Alignment.center,
145+
child: AdWidget(ad: _ad),
146+
),
147+
),
95148
Flexible(
96149
child: _buildExampleItemsWidget(),
97150
),
@@ -141,7 +194,9 @@ class _MyHomePageState extends State<MyHomePage> {
141194
const Divider(),
142195
ListTile(
143196
title: const Text('Privacy Policy'),
144-
onTap: _launchURL,
197+
onTap: () => _launchURL(
198+
'https://flutter-playground.flycricket.io/privacy.html',
199+
),
145200
),
146201
],
147202
),
@@ -163,8 +218,7 @@ class _MyHomePageState extends State<MyHomePage> {
163218
);
164219
}
165220

166-
Future<void> _launchURL() async {
167-
const url = 'https://flutter-playground.flycricket.io/privacy.html';
221+
Future<void> _launchURL(String url) async {
168222
if (await canLaunch(url)) {
169223
await launch(url);
170224
} else {

0 commit comments

Comments
 (0)