pacote_sc 0.0.1 pacote_sc: ^0.0.1 copied to clipboard
A new Flutter package project.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:pacote_sc/screens/create_screen.dart';
import 'package:styled_widget/styled_widget.dart';
import 'package:qrscan/qrscan.dart' as scanner;
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(MeuApp());
});
}
final startappId = '';
final admobBId = 'ca-app-pub-3940256099942544/6300978111';
final admobIId = 'ca-app-pub-3940256099942544/1033173712';
final colorPrimary = Colors.green;
class AppController extends GetxController {
final qrCodeString = ''.obs;
InterstitialAd? interstitialAd;
int _numInterstitialLoadAttempts = 0;
int maxFailedLoadAttempts = 3;
AdWidget? adWidgetQad;
final BannerAd myBannerQad = BannerAd(
adUnitId: admobBId,
size: AdSize.mediumRectangle,
request: AdRequest(),
listener: BannerAdListener(),
);
static final AdRequest request = AdRequest();
getQrCodeString() async {
scanner.scan().then((cameraScanResult) {
debugPrint('string $cameraScanResult');
if (cameraScanResult != null) qrCodeString.value = cameraScanResult;
});
}
@override
void onInit() {
super.onInit();
loadInterstialAdMob();
myBannerQad.load();
adWidgetQad = AdWidget(ad: myBannerQad);
}
void _createInterstitialAd() {
InterstitialAd.load(
adUnitId: InterstitialAd.testAdUnitId,
request: request,
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
print('$ad loaded');
interstitialAd = ad;
_numInterstitialLoadAttempts = 0;
},
onAdFailedToLoad: (LoadAdError error) {
print('InterstitialAd failed to load: $error.');
_numInterstitialLoadAttempts += 1;
interstitialAd = null;
if (_numInterstitialLoadAttempts <= maxFailedLoadAttempts) {
_createInterstitialAd();
}
},
));
}
loadInterstialAdMob() {
//loadInterstialAdMob
InterstitialAd.load(
adUnitId: admobIId,
request: AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (InterstitialAd ad) {
// Keep a reference to the ad so you can show it later.
this.interstitialAd = ad;
},
onAdFailedToLoad: (LoadAdError error) {
print('InterstitialAd failed to load: $error');
},
));
}
showAdInterstitial(dynamic router) {
//insertInterstitialCode
if (interstitialAd == null) {
print('Warning: attempt to show interstitial before loaded.');
router();
// if (_numInterstitialLoadAttempts <= maxFailedLoadAttempts) {
// _createInterstitialAd();
// }
return;
}
interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdShowedFullScreenContent: (InterstitialAd ad) =>
print('ad onAdShowedFullScreenContent.'),
onAdDismissedFullScreenContent: (InterstitialAd ad) {
print('$ad onAdDismissedFullScreenContent.');
ad.dispose();
_createInterstitialAd();
router();
},
onAdFailedToShowFullScreenContent: (InterstitialAd ad, AdError error) {
print('$ad onAdFailedToShowFullScreenContent: $error');
ad.dispose();
router();
_createInterstitialAd();
},
);
interstitialAd!.show();
interstitialAd = null;
}
}
class MeuApp extends StatelessWidget {
final Color colorPrimary = Colors.black;
MeuApp();
final controller = Get.put(AppController());
Future<Widget> _cameraButton() async {
return MaterialButton(
onPressed: () {
controller.showAdInterstitial(() async {
var status = await Permission.camera.status;
switch (status) {
case PermissionStatus.granted:
controller.getQrCodeString();
break;
default:
bool isShow = await Permission.camera.shouldShowRequestRationale;
if (isShow)
await Permission.camera.request();
else
Get.defaultDialog(
title: 'Camera Permission',
buttonColor: colorPrimary,
confirmTextColor: Colors.white,
cancelTextColor: Colors.black,
middleText:
'This app needs camera access for the QR code reader.',
textCancel: 'Deny',
textConfirm: 'Open Settings',
onConfirm: () => openAppSettings(),
);
}
});
},
child: <Widget>[
Icon(
Icons.camera,
size: 48,
),
Text('Scan QR Code').padding(vertical: 4),
]
.toColumn()
.padding(
vertical: 16,
horizontal: 16,
)
.padding(vertical: 4),
);
}
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.black,
),
home: Scaffold(
appBar: AppBar(
title: Text('Scan & Reader - QR Code'),
),
body: SingleChildScrollView(
child: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'How do you want to start?',
style: Get.theme.textTheme.headline5,
),
),
Divider(),
<Widget>[
FutureBuilder(
future: _cameraButton(),
builder: (_, AsyncSnapshot<Widget> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(child: Text('Aguardando Permissão'));
default:
return snapshot.data!;
}
}),
MaterialButton(
onPressed: () {
controller.showAdInterstitial(() {
Get.to(
() => CreateScreen(colorPrimary: colorPrimary),
);
});
},
child: <Widget>[
Icon(
Icons.qr_code_2,
size: 48,
),
Text('Create QR Code').padding(vertical: 4),
].toColumn().padding(vertical: 4).padding(vertical: 4),
)
].toRow(mainAxisAlignment: MainAxisAlignment.spaceAround),
SizedBox(height: 12),
Obx(() => AnimatedContainer(
duration: Duration(milliseconds: 1200),
child: controller.qrCodeString.value.length > 0
? Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Card(
elevation: 6,
child: Column(
children: [
Padding(
padding:
const EdgeInsets.only(right: 8, left: 8),
child: Row(
children: [
Text(
'QR Code content:',
style: Get.textTheme.subtitle1,
textAlign: TextAlign.left,
),
Spacer(),
TextButton.icon(
onPressed: () {
controller.qrCodeString.value = '';
},
icon: Icon(Icons.close, size: 18),
label: Text("Close Card"),
)
],
),
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(4),
),
width: double.maxFinite,
margin: EdgeInsets.only(),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
controller.qrCodeString.value,
maxLines: 3,
style: Get.textTheme.bodyText1,
),
),
),
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8.0,
vertical: 4,
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: [
OutlinedButton.icon(
onPressed: () {
// Respond to button press
},
icon: Icon(Icons.copy, size: 18),
label: Text("Copy"),
),
OutlinedButton.icon(
onPressed: () {
// Respond to button press
},
icon: Icon(Icons.open_in_browser,
size: 18),
label: Text("Open Browser"),
),
OutlinedButton.icon(
onPressed: () {
// Respond to button press
},
icon: Icon(Icons.share, size: 18),
label: Text("Share"),
)
],
),
),
],
),
),
)
: Container(width: 0, height: 0),
)),
SizedBox(height: 12),
Container(
alignment: Alignment.center,
child: controller.adWidgetQad,
width: controller.myBannerQad.size.width.toDouble(),
height: controller.myBannerQad.size.height.toDouble(),
),
].toColumn(),
),
),
);
}
}