Skip to content

Commit

Permalink
get ddress by qrCode
Browse files Browse the repository at this point in the history
  • Loading branch information
YannMarti committed May 12, 2022
1 parent d826cf4 commit f010872
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 26 deletions.
21 changes: 11 additions & 10 deletions vidaia/lib/pages/exchange/scan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ class ScanPage extends StatefulWidget {
}

class _ScanPageState extends State<ScanPage> {

@override
Widget build(BuildContext context) {
return MobileScanner(
allowDuplicates: false,
controller: MobileScannerController(
facing: CameraFacing.back, torchEnabled: false),
onDetect: (barcode, args) {
final String code = barcode.rawValue!;
global.qrCode = code;
debugPrint('Barcode found! $code');
Navigator.pop(context);
});
allowDuplicates: false,
controller: MobileScannerController(
facing: CameraFacing.back,
torchEnabled: false,
formats: [BarcodeFormat.qrCode],
),
onDetect: (barcode, args) {
final String code = barcode.rawValue!;
debugPrint('Barcode found! $code');
Navigator.pop(context, code);
});
}
}
56 changes: 40 additions & 16 deletions vidaia/lib/pages/exchange/send/send_page.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:vidaia/main.dart';
import 'package:vidaia/pages/exchange/scan_page.dart';
import 'package:vidaia/repositories/dataRepository.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:vidaia/utils/globals.dart' as global;
import 'package:vidaia/utils/invalid_Address_Exception.dart';
import 'package:vidaia/utils/popups.dart';
import 'package:vidaia/utils/wallet.dart';

import '../../../utils/constants.dart';
Expand All @@ -20,6 +23,18 @@ class _SendPageState extends State<SendPage> {
final addressController = TextEditingController();
final valueController = TextEditingController();

updateAddress(String address) {
setState(() => addressController.text = address);
}

moveToSecondPage() async {
final address = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => ScanPage()),
);
updateAddress(address);
}

@override
void dispose() {
valueController.dispose();
Expand All @@ -43,43 +58,52 @@ class _SendPageState extends State<SendPage> {
style: Theme.of(context).textTheme.subtitle1,
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ScanPage()),
);
moveToSecondPage();
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(

decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: "Receiver's Address",
),
border: OutlineInputBorder(),
hintText: "Receiver's Address",
),
controller: addressController,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Amount',
),
border: OutlineInputBorder(),
hintText: 'Amount',
),
controller: valueController,
),
),
ElevatedButton(
ElevatedButton(
onPressed: () {
addressController.text = '0x00bab3d8de4ebbefb07d53b1ff8c0f2434bd616d';
addressController.text =
'0x00bab3d8de4ebbefb07d53b1ff8c0f2434bd616d';
},
child: Text('set address')),
ElevatedButton(
onPressed: () {
transferVidar(int.parse(valueController.text),
addressController.text, 'https://testnet.veblocks.net');
onPressed: () async {
var res;
try {
res = await transferVidar(int.parse(valueController.text),
addressController.text, 'https://testnet.veblocks.net');
} on InvalidAddressException {
oneButtonPopup(context, Text('Transaction Failed'),
Text('The Address is not valid'));
} finally {
if (res.containsKey('id')) {
txConfirmedAlert(context, res['id']);
} else {
txError(context);
}
}
},
child: Text('Send')),
],
Expand Down
25 changes: 25 additions & 0 deletions vidaia/lib/utils/invalid_Address_Exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import 'package:thor_devkit_dart/crypto/address.dart';

class InvalidAddressException implements Exception {
late String _message;

InvalidAddressException([String message = 'Invalid InvalidAddress']) {
_message = message;
}


@override
String toString() {
return _message;
}

}

void validateAddress(String address) {
if (address == null) {
throw InvalidAddressException();
} else if (!Address.isAddress(address)) {
throw InvalidAddressException("Invalid Address!");
}
}
29 changes: 29 additions & 0 deletions vidaia/lib/utils/popups.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


import 'package:flutter/material.dart';

oneButtonPopup(BuildContext context, Text title, Text content) {
// set up the buttons
Widget cancelButton = TextButton(
child: Text("OK"),
onPressed: () {
Navigator.pop(context);
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: title,
content: content,
actions: [
cancelButton,
],
);
// show the dialog
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
54 changes: 54 additions & 0 deletions vidaia/lib/utils/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:thor_request_dart/connect.dart';
import 'package:thor_request_dart/contract.dart';
import 'package:thor_request_dart/wallet.dart';
import 'package:vidaia/utils/globals.dart' as global;
import 'package:vidaia/utils/invalid_Address_Exception.dart';

createNewWallet() async {
//generate mnemonic phrase and save it on local device
Expand Down Expand Up @@ -66,6 +67,7 @@ getWordButtons() async {
}

Future<Map> transferVidar(int value, String address, String url) async {
validateAddress(address);
final storage = FlutterSecureStorage();
Connect connect = Connect(url);
final String? priv = await storage.read(key: 'privateKey');
Expand Down Expand Up @@ -218,3 +220,55 @@ confirmPurchase(BuildContext context) {
},
);
}

txConfirmedAlert(BuildContext context, String txId) {
// set up the buttons
Widget cancelButton = TextButton(
child: Text("OK"),
onPressed: () {
Navigator.pop(context);
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Transction Successful"),
content: Text("Transaction ID: \n" + txId),
actions: [
cancelButton,
],
);
// show the dialog
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return alert;
},
);
}

txError(BuildContext context) {
// set up the buttons
Widget cancelButton = TextButton(
child: Text("OK"),
onPressed: () {
Navigator.pop(context);
},
);
// set up the AlertDialog
AlertDialog alert = AlertDialog(
title: Text("Transction Failed"),
content: Text("Something went wrong"),
actions: [
cancelButton,
],
);
// show the dialog
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) {
return alert;
},
);
}

0 comments on commit f010872

Please sign in to comment.