Skip to content

Commit

Permalink
changed to getx
Browse files Browse the repository at this point in the history
  • Loading branch information
AravindhIppili committed Aug 7, 2021
1 parent 867c835 commit fb927c7
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 106 deletions.
22 changes: 22 additions & 0 deletions lib/controller/animate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class Animate extends GetxController with SingleGetTickerProviderMixin{
late Rx<AnimationController> animationController;


@override
void onInit() {
super.onInit();
animationController =
AnimationController(duration: const Duration(seconds: 1), vsync: this).obs;
animationController.value.repeat(reverse: true);
}

@override
void dispose() {
super.dispose();
animationController.value.dispose();
}
}
35 changes: 35 additions & 0 deletions lib/controller/scan_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:qrcode_scanner/data/scan_data.dart';

class ScanController extends GetxController {
var controller;
var barcodeResult;
var flashIcon = Icons.flash_off.obs;

Rx<int> barcodefound = 0.obs;

var camPermission = true.obs;

void updateContoller(QRViewController cont) {
controller = cont.obs;
controller.value.scannedDataStream.listen((scanData) {
if (scannedData.indexWhere((element) => element.text == scanData.code) ==
-1 ||
scannedData.length == 0) {
scannedData.add(ScannedData(
text: scanData.code,
time: DateTime.now().toString().split(" ")[1].substring(0, 8)));
}
barcodeResult = scanData;
barcodefound(barcodefound.value+1);
});
}

@override
void dispose() {
super.dispose();
controller?.dispose();
}
}
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:qrcode_scanner/pages/home.dart';

void main() {
Expand All @@ -8,7 +9,7 @@ void main() {
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: 'QR code Scanner',
theme: ThemeData(
Expand Down
141 changes: 44 additions & 97 deletions lib/pages/home.dart
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:qrcode_scanner/data/scan_data.dart';
import 'package:qrcode_scanner/controller/scan_controller.dart';
import 'package:qrcode_scanner/pages/scan_animation.dart';
import 'package:qrcode_scanner/pages/view_scans.dart';

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);

@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
Barcode? barcodeResult;
class HomePage extends StatelessWidget {
final GlobalKey qrScanner = GlobalKey(debugLabel: 'QR');
QRViewController? controller;
IconData flashIcon = Icons.flash_off;
late AnimationController _animationController;
Tween<Offset> _tween = Tween(begin: Offset(0, -7), end: Offset(0, 7));
bool camPermission = true;

@override
void initState() {
super.initState();
_animationController =
AnimationController(duration: const Duration(seconds: 1), vsync: this);
_animationController.repeat(reverse: true);
}

@override
void reassemble() {
super.reassemble();
if (Platform.isAndroid) {
controller!.pauseCamera();
}
}
final stateContoller = Get.put(ScanController());

@override
Widget build(BuildContext context) {
Expand All @@ -49,11 +22,10 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
onQRViewCreated: _onQRViewCreated,
onPermissionSet: (controller, isSet) {
if (isSet == false) {
camPermission = false;
stateContoller.camPermission(false);
} else {
camPermission = true;
stateContoller.camPermission(true);
}
setState(() {});
},
overlay: QrScannerOverlayShape(
cutOutSize: MediaQuery.of(context).size.width * 0.7,
Expand All @@ -66,32 +38,21 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
top: 100,
child: Container(
child: Text(
camPermission ? "" : "Give Camera Permission!",
stateContoller.camPermission.value
? ""
: "Give Camera Permission!",
style: TextStyle(fontSize: 25, color: Colors.red),
)),
),
Container(
padding: EdgeInsets.all(10),
child: SlideTransition(
position: _tween.animate(CurvedAnimation(
parent: _animationController, curve: Curves.easeInOut)),
child: Container(
width: 240,
child: Divider(
thickness: 2,
color: Colors.red,
),
),
)),
ScanAnimation(),
buildScannedOutput(),
buildFlashButton(),
Positioned(
bottom: 30,
child: Container(
child: ElevatedButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => ViewScans()));
Get.to(() => ViewScans());
},
child: Row(
children: [
Expand All @@ -111,70 +72,56 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
));
}

Positioned buildFlashButton() {
return Positioned(
top: 20,
child: Container(
child: IconButton(
icon: Icon(
flashIcon,
color: Colors.white,
size: 40,
),
onPressed: () async {
setState(() {
controller?.toggleFlash();
if (flashIcon == Icons.flash_on) {
flashIcon = Icons.flash_off;
Widget buildFlashButton() {
return Obx(
() => Positioned(
top: 20,
child: Container(
child: IconButton(
icon: Icon(
stateContoller.flashIcon.value,
color: Colors.white,
size: 40,
),
onPressed: () async {
stateContoller.controller.value?.toggleFlash();
if (stateContoller.flashIcon.value == Icons.flash_on) {
stateContoller.flashIcon(Icons.flash_off);
} else {
flashIcon = Icons.flash_on;
stateContoller.flashIcon(Icons.flash_on);
}
});
},
},
),
),
),
);
}

Positioned buildScannedOutput() {
return Positioned(
bottom: 100,
left: 0,
right: 0,
child: Container(
Widget buildScannedOutput() {
return Obx(
() => Positioned(
bottom: 100,
left: 0,
right: 0,
child: Container(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
barcodeResult != null ? barcodeResult!.code : "Scan Something",
stateContoller.barcodefound.value > 0
? stateContoller.barcodeResult.code
: "Scan Something",
maxLines: 2,
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white, fontSize: 20),
),
)),
),
),
),
);
}

void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
controller.scannedDataStream.listen((scanData) {
if (scannedData.indexWhere((element) => element.text == scanData.code) ==
-1 ||
scannedData.length == 0) {
scannedData.add(ScannedData(
text: scanData.code,
time: DateTime.now().toString().split(" ")[1].substring(0, 8)));
}
setState(() {
barcodeResult = scanData;
});
});
}

@override
void dispose() {
controller?.dispose();
_animationController.dispose();
super.dispose();
stateContoller.updateContoller(controller);
}
}
25 changes: 25 additions & 0 deletions lib/pages/scan_animation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:qrcode_scanner/controller/animate.dart';

class ScanAnimation extends StatelessWidget {
final Tween<Offset> _tween = Tween(begin: Offset(0, -7), end: Offset(0, 7));
final animateController = Get.put(Animate());
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(10),
child: Obx(() => SlideTransition(
position: _tween.animate(CurvedAnimation(
parent: animateController.animationController.value, curve: Curves.easeInOut)),
child: Container(
width: 240,
child: Divider(
thickness: 2,
color: Colors.red,
),
),
),
));
}
}
9 changes: 1 addition & 8 deletions lib/pages/view_scans.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:qrcode_scanner/data/scan_data.dart';

class ViewScans extends StatefulWidget {
const ViewScans({Key? key}) : super(key: key);

@override
_ViewScansState createState() => _ViewScansState();
}

class _ViewScansState extends State<ViewScans> {
class ViewScans extends StatelessWidget {
final _random = Random();

Color? listColor() {
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
get:
dependency: "direct main"
description:
name: get
url: "https://pub.dartlang.org"
source: hosted
version: "4.3.4"
js:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:
sdk: flutter

qr_code_scanner: ^0.5.2
get:

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down

0 comments on commit fb927c7

Please sign in to comment.