Skip to content

Instantly share code, notes, and snippets.

@LouisParkin
Last active April 22, 2020 07:16
Show Gist options
  • Save LouisParkin/9e669b533f404c940d256fdb9af87afe to your computer and use it in GitHub Desktop.
Save LouisParkin/9e669b533f404c940d256fdb9af87afe to your computer and use it in GitHub Desktop.
Implementation of a firebase Admob banner in flutter
import 'package:firebase_admob/firebase_admob.dart';
import 'package:flutter/material.dart';
import 'package:sabnzbdplusrewrite/main.dart';
import 'package:sabnzbdplusrewrite/retrofit/models/history.dart';
import 'package:sabnzbdplusrewrite/screens/history_item_activity.dart';
import 'package:sabnzbdplusrewrite/utils/banner_util.dart';
import 'dart:io' show Platform;
import 'package:sabnzbdplusrewrite/consts/admob.dart';
class HistoryFragment extends StatefulWidget {
@override
State<StatefulWidget> createState() => _HistoryFragmentState();
}
class _HistoryFragmentState extends State<HistoryFragment> {
int _currentIndex = 0;
GlobalKey mKey;
RenderBox mRenderBox;
Offset mUpperBotNavBarOffset;
BannerAd historyBanner;
@override
void initState() {
try {
mHistory = HistoryModel().getHistoryData();
} catch (Exception) {}
mKey = new GlobalKey();
super.initState();
WidgetsBinding.instance.addPostFrameCallback(
(_) {
populateBox(context);
// historyBanner = doBanner(
// context, historyBanner, targetingInfo, BannerAd.testAdUnitId, 2);
if (Platform.isAndroid) {
// Android-specific code
historyBanner = doBanner(
context, historyBanner, targetingInfo, getAndroidAdUnit("history-banner"), 2);
} else if (Platform.isIOS) {
// iOS-specific code
historyBanner = doBanner(
context, historyBanner, targetingInfo, getiOsAdUnit("history-banner"), 2);
} else {
historyBanner = doBanner(
context, historyBanner, targetingInfo, BannerAd.testAdUnitId, 2);
}
},
);
}
void populateBox(BuildContext context) {
mRenderBox = mKey.currentContext.findRenderObject();
mUpperBotNavBarOffset = mRenderBox.localToGlobal(Offset.zero);
upperBotNavCoordinateX = mUpperBotNavBarOffset.dx;
upperBotNavCoordinateY = mUpperBotNavBarOffset.dy;
}
@override
void dispose() {
disposeBanner(historyBanner);
super.dispose();
}
@override
Widget build(BuildContext context) {
double w = MediaQuery.of(context).size.width;
double w2 = w * 0.25;
double w3 = w * 0.65;
double w4 = w2 * 0.25;
return Center(
child: RefreshIndicator(
child: Scaffold(
body: Center(
child: FutureBuilder<History>(
future: mHistory,
builder: (context, snapshot) {
if (snapshot.hasData) {
// Assign to main
mHistoryData = snapshot.data;
return Padding(
child: Column(
children: <Widget>[
Expanded(
child: new ListView.builder(
itemCount: snapshot.data.slots.length,
itemBuilder: (BuildContext ctxt, int index) {
return Card(
child: InkWell(
splashColor: Colors.deepOrange.withAlpha(30),
onTap: () {
_navigateToDownloadedItem(context, index);
},
child: Row(
children: <Widget>[
Container(
width: w4 * 2,
height: w4,
child: _buildImage(snapshot
.data.slots[index].status ==
Status.QUEUED ||
snapshot.data.slots[index].status ==
Status.COMPLETED ||
snapshot.data.slots[index].status ==
Status.DOWNLOADING),
),
Container(
margin: EdgeInsets.only(
right: 10, left: 10),
height: 80,
width: w3,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
'${snapshot.data.slots[index].name}'))),
],
),
),
);
},
),
)
],
),
padding: EdgeInsets.only(
bottom: 50.0,
),
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
),
bottomNavigationBar: BottomNavigationBar(
key: mKey,
onTap: onTabTapped, // new
currentIndex: _currentIndex, // new
items: [
new BottomNavigationBarItem(
icon: new Icon(Icons.skip_previous),
title: new Text('Previous Page'),
backgroundColor: Colors.orangeAccent,
),
new BottomNavigationBarItem(
icon: new Icon(Icons.delete),
title: new Text('Clear History'),
backgroundColor: Colors.orangeAccent,
),
new BottomNavigationBarItem(
icon: Icon(Icons.skip_next),
backgroundColor: Colors.orangeAccent,
title: Text('Next Page')),
],
),
),
onRefresh: () {
setState(
() {
mHistory = HistoryModel().getHistoryData();
},
);
return mHistory;
},
),
);
}
void onTabTapped(int index) {
setState(
() {
_currentIndex = index;
},
);
}
_navigateToDownloadedItem(BuildContext context, int index) async {
// Navigator.push returns a Future that completes after calling
// Navigator.pop on the Selection Screen.
disposeBannerBlock(historyBanner);
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => HistoryItemActivity(
itemIdx: index,
),
),
);
setState(
() {
mHistory = HistoryModel().getHistoryData();
// historyBanner = doBanner(
// context, historyBanner, targetingInfo, BannerAd.testAdUnitId, 2);
if (Platform.isAndroid) {
// Android-specific code
historyBanner = doBanner(
context, historyBanner, targetingInfo, getAndroidAdUnit("history-banner"), 2);
} else if (Platform.isIOS) {
// iOS-specific code
historyBanner = doBanner(
context, historyBanner, targetingInfo, getiOsAdUnit("history-banner"), 2);
} else {
historyBanner = doBanner(
context, historyBanner, targetingInfo, BannerAd.testAdUnitId, 2);
}
},
);
}
}
Widget _buildImage(bool success) {
if (success) {
return Image(
image: AssetImage('assets/icons/circle_green_alpha.png'),
height: 30,
width: 30,
);
}
return Image(
image: AssetImage('assets/icons/circle_red_alpha.png'),
height: 30,
width: 30,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment