From 1ea2096214a575a4f1ff2105bab49c247991c4c2 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Thu, 28 Nov 2019 17:24:29 -0300 Subject: [PATCH] Add local_auth to auth with fingerprint --- android/app/src/main/AndroidManifest.xml | 1 + .../ar/com/p39/como_gasto/MainActivity.kt | 4 +- lib/graph_widget.dart | 2 - lib/login_state.dart | 1 - lib/pages/add_page.dart | 199 +++++++++++------- lib/pages/login_page.dart | 1 - lib/pages/ui/day_expense_list_tile.dart | 1 - pubspec.lock | 7 + pubspec.yaml | 1 + 9 files changed, 131 insertions(+), 86 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 06f5bfa..ef9e979 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ In most cases you can leave this as-is, but you if you want to provide additional functionality it is fine to subclass or reimplement FlutterApplication and put your custom class here. --> + with SingleTickerProviderStateMixin { File _selectedPicture; + LocalAuthentication _localAth; + bool _isBiometricAvailable = false; + @override void initState() { super.initState(); + _localAth = LocalAuthentication(); + _localAth.canCheckBiometrics.then((b) { + setState(() { + _isBiometricAvailable = b; + }); + }); + _controller = AnimationController( vsync: this, duration: Duration(milliseconds: 750), @@ -59,17 +70,15 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { @override Widget build(BuildContext context) { - var h = MediaQuery - .of(context) - .size - .height; + var h = MediaQuery.of(context).size.height; return Stack( children: [ Transform.translate( offset: Offset(0, h * (1 - _pageAnimation.value)), child: Scaffold( appBar: AppBar( - leading: BackButton( + leading: IconButton( + icon: BackButtonIcon(), color: Colors.grey, onPressed: () { _controller.reverse(); @@ -92,13 +101,15 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { showDatePicker( context: context, initialDate: DateTime.now(), - firstDate: DateTime.now().subtract(Duration(hours: 24*30)), + firstDate: + DateTime.now().subtract(Duration(hours: 24 * 30)), lastDate: DateTime.now(), ).then((newDate) { if (newDate != null) { setState(() { date = newDate; - dateStr = "${date.year.toString()}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')}"; + dateStr = + "${date.year.toString()}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}"; }); } }); @@ -108,7 +119,8 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { icon: Icon(Icons.camera_alt), color: Colors.grey, onPressed: () async { - var image = await ImagePicker.pickImage(source: ImageSource.camera); + var image = + await ImagePicker.pickImage(source: ImageSource.camera); setState(() { _selectedPicture = image; @@ -126,10 +138,7 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { } Widget _body() { - var h = MediaQuery - .of(context) - .size - .height; + var h = MediaQuery.of(context).size.height; return Column( children: [ _categorySelector(), @@ -211,63 +220,60 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { return Expanded( child: LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { - var height = constraints.biggest.height / 4; + var height = constraints.biggest.height / 4; - return Table( - border: TableBorder.all( - color: Colors.grey, - width: 1.0, - ), - children: [ - TableRow(children: [ - _num("1", height), - _num("2", height), - _num("3", height), - ]), - TableRow(children: [ - _num("4", height), - _num("5", height), - _num("6", height), - ]), - TableRow(children: [ - _num("7", height), - _num("8", height), - _num("9", height), - ]), - TableRow(children: [ - _num(",", height), - _num("0", height), - GestureDetector( - onTap: () { - setState(() { - value = value ~/ 10; - }); - }, - child: Container( - height: height, - child: Center( - child: Icon( - Icons.backspace, - color: Colors.grey, - size: 40, - ), - ), + return Table( + border: TableBorder.all( + color: Colors.grey, + width: 1.0, + ), + children: [ + TableRow(children: [ + _num("1", height), + _num("2", height), + _num("3", height), + ]), + TableRow(children: [ + _num("4", height), + _num("5", height), + _num("6", height), + ]), + TableRow(children: [ + _num("7", height), + _num("8", height), + _num("9", height), + ]), + TableRow(children: [ + _num(",", height), + _num("0", height), + GestureDetector( + onTap: () { + setState(() { + value = value ~/ 10; + }); + }, + child: Container( + height: height, + child: Center( + child: Icon( + Icons.backspace, + color: Colors.grey, + size: 40, ), ), - ]), - ], - ); - }), + ), + ), + ]), + ], + ); + }), ); } Widget _submit() { if (_controller.value < 1) { var buttonWidth = widget.buttonRect.right - widget.buttonRect.left; - var w = MediaQuery - .of(context) - .size - .width; + var w = MediaQuery.of(context).size.width; return Positioned( left: widget.buttonRect.left * (1 - _buttonAnimation.value), @@ -277,11 +283,8 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { top: widget.buttonRect.top, //<-- Margin from top bottom: - (MediaQuery - .of(context) - .size - .height - widget.buttonRect.bottom) * - (1 - _buttonAnimation.value), + (MediaQuery.of(context).size.height - widget.buttonRect.bottom) * + (1 - _buttonAnimation.value), //<-- Margin from bottom child: Container( width: double.infinity, @@ -292,6 +295,7 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { color: Colors.blueAccent, ), child: MaterialButton( + onPressed: () {}, child: Text( "Add expense", style: TextStyle( @@ -312,24 +316,56 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { return Container( decoration: BoxDecoration(color: Colors.blueAccent), child: MaterialButton( - child: Text( - "Add expense", - style: TextStyle( - color: Colors.white, - fontSize: 20.0, - ), + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + "Add expense", + style: TextStyle( + color: Colors.white, + fontSize: 20.0, + ), + ), + if (_isBiometricAvailable) + Icon( + Icons.fingerprint, + color: Colors.white, + size: 30.0, + ) + ], ), - onPressed: () { + onPressed: () async { var db = Provider.of(context); if (value > 0 && category != "") { - db.add(category, value / 100.0, date, _selectedPicture); - - _controller.reverse(); + if (_isBiometricAvailable) { + bool didAuthenticate = await _localAth.authenticateWithBiometrics( + localizedReason: "Please identify yourself!", + ); + if (didAuthenticate) { + _saveAndBack(db); + } else { + showDialog( + context: context, + builder: (context) => AlertDialog( + content: Text( + "You need to identify yourself."), + actions: [ + FlatButton( + child: Text('Ok'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + ], + )); + } + } else { + _saveAndBack(db); + } } else { showDialog( context: context, - builder: (context) => - AlertDialog( + builder: (context) => AlertDialog( content: Text( "You need to select a category and a value greater than zero."), actions: [ @@ -340,8 +376,7 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { }, ), ], - ) - ); + )); } }, ), @@ -350,4 +385,10 @@ class _AddPageState extends State with SingleTickerProviderStateMixin { ); } } + + _saveAndBack(ExpensesRepository db) { + db.add(category, value / 100.0, date, _selectedPicture); + + _controller.reverse(); + } } diff --git a/lib/pages/login_page.dart b/lib/pages/login_page.dart index 95f1970..5dd092f 100644 --- a/lib/pages/login_page.dart +++ b/lib/pages/login_page.dart @@ -2,7 +2,6 @@ import 'package:flare_flutter/flare_actor.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -import 'package:flare_dart/actor.dart'; import '../login_state.dart'; diff --git a/lib/pages/ui/day_expense_list_tile.dart b/lib/pages/ui/day_expense_list_tile.dart index b952cee..b7e1e1f 100644 --- a/lib/pages/ui/day_expense_list_tile.dart +++ b/lib/pages/ui/day_expense_list_tile.dart @@ -1,5 +1,4 @@ import 'package:cloud_firestore/cloud_firestore.dart'; -import 'package:firebase_storage/firebase_storage.dart'; import 'package:flutter/material.dart'; class DayExpenseListTile extends StatelessWidget { diff --git a/pubspec.lock b/pubspec.lock index 77fe313..ce416b9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -186,6 +186,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.15.8" + local_auth: + dependency: "direct main" + description: + name: local_auth + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.1" logging: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index a74247e..cc4e23a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,6 +25,7 @@ dependencies: firebase_storage: ^3.0.8 image_picker: ^0.6.1+11 uuid: + local_auth: ^0.6.1 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.