From fb927c75ce0d82a76f71c2ce90d19c49bda02e64 Mon Sep 17 00:00:00 2001 From: aravindhippili Date: Sat, 7 Aug 2021 15:35:56 +0530 Subject: [PATCH] changed to getx --- lib/controller/animate.dart | 22 +++++ lib/controller/scan_controller.dart | 35 +++++++ lib/main.dart | 3 +- lib/pages/home.dart | 141 +++++++++------------------- lib/pages/scan_animation.dart | 25 +++++ lib/pages/view_scans.dart | 9 +- pubspec.lock | 7 ++ pubspec.yaml | 1 + 8 files changed, 137 insertions(+), 106 deletions(-) create mode 100644 lib/controller/animate.dart create mode 100644 lib/controller/scan_controller.dart create mode 100644 lib/pages/scan_animation.dart diff --git a/lib/controller/animate.dart b/lib/controller/animate.dart new file mode 100644 index 0000000..950be29 --- /dev/null +++ b/lib/controller/animate.dart @@ -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; + + + @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(); + } +} diff --git a/lib/controller/scan_controller.dart b/lib/controller/scan_controller.dart new file mode 100644 index 0000000..32b4382 --- /dev/null +++ b/lib/controller/scan_controller.dart @@ -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 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(); + } +} diff --git a/lib/main.dart b/lib/main.dart index 5904740..a2fd44c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:get/get.dart'; import 'package:qrcode_scanner/pages/home.dart'; void main() { @@ -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( diff --git a/lib/pages/home.dart b/lib/pages/home.dart index 0f19902..49cd716 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -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 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 _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) { @@ -49,11 +22,10 @@ class _HomePageState extends State 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, @@ -66,23 +38,13 @@ class _HomePageState extends State 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( @@ -90,8 +52,7 @@ class _HomePageState extends State with TickerProviderStateMixin { child: Container( child: ElevatedButton( onPressed: () { - Navigator.push(context, - MaterialPageRoute(builder: (context) => ViewScans())); + Get.to(() => ViewScans()); }, child: Row( children: [ @@ -111,70 +72,56 @@ class _HomePageState extends State 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); } } diff --git a/lib/pages/scan_animation.dart b/lib/pages/scan_animation.dart new file mode 100644 index 0000000..af875e3 --- /dev/null +++ b/lib/pages/scan_animation.dart @@ -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 _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, + ), + ), + ), + )); + } +} diff --git a/lib/pages/view_scans.dart b/lib/pages/view_scans.dart index 784cc5c..4a3a4ed 100644 --- a/lib/pages/view_scans.dart +++ b/lib/pages/view_scans.dart @@ -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 { +class ViewScans extends StatelessWidget { final _random = Random(); Color? listColor() { diff --git a/pubspec.lock b/pubspec.lock index b140732..dfb18c5 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index df3cea1..e170e33 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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.