Add local_auth to auth with fingerprint

This commit is contained in:
Ricardo Markiewicz
2019-11-28 17:24:29 -03:00
parent f89104fffc
commit 1ea2096214
9 changed files with 131 additions and 86 deletions
+1
View File
@@ -6,6 +6,7 @@
In most cases you can leave this as-is, but you if you want to provide 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 additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. --> FlutterApplication and put your custom class here. -->
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<application <application
android:name="io.flutter.app.FlutterApplication" android:name="io.flutter.app.FlutterApplication"
android:label="como_gasto" android:label="como_gasto"
@@ -2,10 +2,10 @@ package ar.com.p39.como_gasto
import android.os.Bundle import android.os.Bundle
import io.flutter.app.FlutterActivity import io.flutter.app.FlutterFragmentActivity
import io.flutter.plugins.GeneratedPluginRegistrant import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() { class MainActivity: FlutterFragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this) GeneratedPluginRegistrant.registerWith(this)
-2
View File
@@ -1,5 +1,3 @@
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart'; import 'package:charts_flutter/flutter.dart';
-1
View File
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart'; import 'package:google_sign_in/google_sign_in.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:flare_dart/actor.dart';
class LoginState with ChangeNotifier { class LoginState with ChangeNotifier {
final GoogleSignIn _googleSignIn = GoogleSignIn(); final GoogleSignIn _googleSignIn = GoogleSignIn();
+120 -79
View File
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:local_auth/local_auth.dart';
import '../expenses_repository.dart'; import '../expenses_repository.dart';
@@ -30,10 +31,20 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
File _selectedPicture; File _selectedPicture;
LocalAuthentication _localAth;
bool _isBiometricAvailable = false;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_localAth = LocalAuthentication();
_localAth.canCheckBiometrics.then((b) {
setState(() {
_isBiometricAvailable = b;
});
});
_controller = AnimationController( _controller = AnimationController(
vsync: this, vsync: this,
duration: Duration(milliseconds: 750), duration: Duration(milliseconds: 750),
@@ -59,17 +70,15 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var h = MediaQuery var h = MediaQuery.of(context).size.height;
.of(context)
.size
.height;
return Stack( return Stack(
children: [ children: [
Transform.translate( Transform.translate(
offset: Offset(0, h * (1 - _pageAnimation.value)), offset: Offset(0, h * (1 - _pageAnimation.value)),
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(
leading: BackButton( leading: IconButton(
icon: BackButtonIcon(),
color: Colors.grey, color: Colors.grey,
onPressed: () { onPressed: () {
_controller.reverse(); _controller.reverse();
@@ -92,13 +101,15 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
showDatePicker( showDatePicker(
context: context, context: context,
initialDate: DateTime.now(), initialDate: DateTime.now(),
firstDate: DateTime.now().subtract(Duration(hours: 24*30)), firstDate:
DateTime.now().subtract(Duration(hours: 24 * 30)),
lastDate: DateTime.now(), lastDate: DateTime.now(),
).then((newDate) { ).then((newDate) {
if (newDate != null) { if (newDate != null) {
setState(() { setState(() {
date = newDate; 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<AddPage> with SingleTickerProviderStateMixin {
icon: Icon(Icons.camera_alt), icon: Icon(Icons.camera_alt),
color: Colors.grey, color: Colors.grey,
onPressed: () async { onPressed: () async {
var image = await ImagePicker.pickImage(source: ImageSource.camera); var image =
await ImagePicker.pickImage(source: ImageSource.camera);
setState(() { setState(() {
_selectedPicture = image; _selectedPicture = image;
@@ -126,10 +138,7 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
} }
Widget _body() { Widget _body() {
var h = MediaQuery var h = MediaQuery.of(context).size.height;
.of(context)
.size
.height;
return Column( return Column(
children: <Widget>[ children: <Widget>[
_categorySelector(), _categorySelector(),
@@ -211,63 +220,60 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
return Expanded( return Expanded(
child: LayoutBuilder( child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) { builder: (BuildContext context, BoxConstraints constraints) {
var height = constraints.biggest.height / 4; var height = constraints.biggest.height / 4;
return Table( return Table(
border: TableBorder.all( border: TableBorder.all(
color: Colors.grey, color: Colors.grey,
width: 1.0, width: 1.0,
), ),
children: [ children: [
TableRow(children: [ TableRow(children: [
_num("1", height), _num("1", height),
_num("2", height), _num("2", height),
_num("3", height), _num("3", height),
]), ]),
TableRow(children: [ TableRow(children: [
_num("4", height), _num("4", height),
_num("5", height), _num("5", height),
_num("6", height), _num("6", height),
]), ]),
TableRow(children: [ TableRow(children: [
_num("7", height), _num("7", height),
_num("8", height), _num("8", height),
_num("9", height), _num("9", height),
]), ]),
TableRow(children: [ TableRow(children: [
_num(",", height), _num(",", height),
_num("0", height), _num("0", height),
GestureDetector( GestureDetector(
onTap: () { onTap: () {
setState(() { setState(() {
value = value ~/ 10; value = value ~/ 10;
}); });
}, },
child: Container( child: Container(
height: height, height: height,
child: Center( child: Center(
child: Icon( child: Icon(
Icons.backspace, Icons.backspace,
color: Colors.grey, color: Colors.grey,
size: 40, size: 40,
),
),
), ),
), ),
]), ),
], ),
); ]),
}), ],
);
}),
); );
} }
Widget _submit() { Widget _submit() {
if (_controller.value < 1) { if (_controller.value < 1) {
var buttonWidth = widget.buttonRect.right - widget.buttonRect.left; var buttonWidth = widget.buttonRect.right - widget.buttonRect.left;
var w = MediaQuery var w = MediaQuery.of(context).size.width;
.of(context)
.size
.width;
return Positioned( return Positioned(
left: widget.buttonRect.left * (1 - _buttonAnimation.value), left: widget.buttonRect.left * (1 - _buttonAnimation.value),
@@ -277,11 +283,8 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
top: widget.buttonRect.top, top: widget.buttonRect.top,
//<-- Margin from top //<-- Margin from top
bottom: bottom:
(MediaQuery (MediaQuery.of(context).size.height - widget.buttonRect.bottom) *
.of(context) (1 - _buttonAnimation.value),
.size
.height - widget.buttonRect.bottom) *
(1 - _buttonAnimation.value),
//<-- Margin from bottom //<-- Margin from bottom
child: Container( child: Container(
width: double.infinity, width: double.infinity,
@@ -292,6 +295,7 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
color: Colors.blueAccent, color: Colors.blueAccent,
), ),
child: MaterialButton( child: MaterialButton(
onPressed: () {},
child: Text( child: Text(
"Add expense", "Add expense",
style: TextStyle( style: TextStyle(
@@ -312,24 +316,56 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
return Container( return Container(
decoration: BoxDecoration(color: Colors.blueAccent), decoration: BoxDecoration(color: Colors.blueAccent),
child: MaterialButton( child: MaterialButton(
child: Text( child: Row(
"Add expense", mainAxisAlignment: MainAxisAlignment.center,
style: TextStyle( children: <Widget>[
color: Colors.white, Text(
fontSize: 20.0, "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<ExpensesRepository>(context); var db = Provider.of<ExpensesRepository>(context);
if (value > 0 && category != "") { if (value > 0 && category != "") {
db.add(category, value / 100.0, date, _selectedPicture); if (_isBiometricAvailable) {
bool didAuthenticate = await _localAth.authenticateWithBiometrics(
_controller.reverse(); localizedReason: "Please identify yourself!",
);
if (didAuthenticate) {
_saveAndBack(db);
} else {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text(
"You need to identify yourself."),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
));
}
} else {
_saveAndBack(db);
}
} else { } else {
showDialog( showDialog(
context: context, context: context,
builder: (context) => builder: (context) => AlertDialog(
AlertDialog(
content: Text( content: Text(
"You need to select a category and a value greater than zero."), "You need to select a category and a value greater than zero."),
actions: <Widget>[ actions: <Widget>[
@@ -340,8 +376,7 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
}, },
), ),
], ],
) ));
);
} }
}, },
), ),
@@ -350,4 +385,10 @@ class _AddPageState extends State<AddPage> with SingleTickerProviderStateMixin {
); );
} }
} }
_saveAndBack(ExpensesRepository db) {
db.add(category, value / 100.0, date, _selectedPicture);
_controller.reverse();
}
} }
-1
View File
@@ -2,7 +2,6 @@ import 'package:flare_flutter/flare_actor.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:flare_dart/actor.dart';
import '../login_state.dart'; import '../login_state.dart';
-1
View File
@@ -1,5 +1,4 @@
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
class DayExpenseListTile extends StatelessWidget { class DayExpenseListTile extends StatelessWidget {
+7
View File
@@ -186,6 +186,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.15.8" 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: logging:
dependency: transitive dependency: transitive
description: description:
+1
View File
@@ -25,6 +25,7 @@ dependencies:
firebase_storage: ^3.0.8 firebase_storage: ^3.0.8
image_picker: ^0.6.1+11 image_picker: ^0.6.1+11
uuid: uuid:
local_auth: ^0.6.1
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.