190 lines
5.6 KiB
Dart
190 lines
5.6 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
import 'package:qr_flutter/qr_flutter.dart';
|
|
|
|
class SendPage extends StatefulWidget {
|
|
SendPage({Key? key, required this.uuid, required this.wallet})
|
|
: super(key: key);
|
|
|
|
final String uuid;
|
|
String wallet;
|
|
|
|
@override
|
|
_SendPageState createState() => _SendPageState();
|
|
}
|
|
|
|
class _SendPageState extends State<SendPage> {
|
|
late double walletBalance = 0;
|
|
bool balanceLoaded = false;
|
|
final TextEditingController addressController = TextEditingController();
|
|
final TextEditingController amountController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// Dispose of the controllers when the widget is disposed
|
|
addressController.dispose();
|
|
amountController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> fetchWalletBalance() async {
|
|
if (widget.wallet.isEmpty) {
|
|
walletBalance = 0;
|
|
return;
|
|
}
|
|
|
|
if (balanceLoaded) {
|
|
return;
|
|
}
|
|
|
|
final response = await http.get(Uri.parse(
|
|
'https://api.firewallet.au/wallet/balance?uuid=${widget.uuid}&name=${widget.wallet}'));
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
print(data);
|
|
|
|
setState(() {
|
|
walletBalance = data['available'];
|
|
});
|
|
} else {
|
|
// Handle error
|
|
print('Failed to load wallet balance');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('${widget.wallet} - Send HNS'),
|
|
),
|
|
// Get wallet list from api and display here
|
|
body: Center(
|
|
child: LayoutBuilder(
|
|
builder: (BuildContext context, BoxConstraints constraints) {
|
|
return ListView(
|
|
padding: const EdgeInsets.all(16),
|
|
children: <Widget>[
|
|
// Display the wallet balance
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
'Max ${(walletBalance - 0.02).toStringAsFixed(2)} HNS'),
|
|
TextButton(
|
|
onPressed: fetchWalletBalance,
|
|
child: const Icon(Icons.refresh)),
|
|
],
|
|
),
|
|
// Address input field
|
|
TextField(
|
|
decoration: const InputDecoration(
|
|
border: OutlineInputBorder(),
|
|
labelText: 'Address',
|
|
),
|
|
keyboardType: TextInputType.text,
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9]')),
|
|
],
|
|
controller: addressController,
|
|
),
|
|
SizedBox(height: 16),
|
|
// Amount input field
|
|
TextField(
|
|
decoration: const InputDecoration(
|
|
border: OutlineInputBorder(),
|
|
labelText: 'Amount',
|
|
suffix: Text('HNS'),
|
|
),
|
|
keyboardType: TextInputType.number,
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.allow(RegExp(r'[0-9.]')),
|
|
],
|
|
controller: amountController,
|
|
),
|
|
TextButton(
|
|
onPressed: () => amountController.text =
|
|
(walletBalance - 0.02).toStringAsFixed(2),
|
|
child: const Text('Max')),
|
|
SizedBox(height: 16),
|
|
// Send button
|
|
ElevatedButton(
|
|
onPressed: sendHNS,
|
|
child: const Text('Send'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void sendHNS() {
|
|
// Send HNS to the address
|
|
String toAddress = addressController.text;
|
|
double amount = double.parse(amountController.text);
|
|
print('Send $amount HNS to $toAddress');
|
|
if (amount > (walletBalance - 0.01)) {
|
|
// Not enough balance
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Not enough balance'),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
bool sendMax = false;
|
|
if (amount >= (walletBalance - 0.02)) {
|
|
sendMax = true;
|
|
}
|
|
|
|
print(sendMax);
|
|
// https://api.firewallet.au/wallet/send?uuid=99879737-0c27-497b-b357-f75720feb32e&name=hot&address=hs1qca9n20ew7ph6l5galfnftmwme6kwmu26mzjgtx&amount=2
|
|
http
|
|
.post(
|
|
Uri.parse(
|
|
'https://api.firewallet.au/wallet/send?uuid=${widget.uuid}&name=${widget.wallet}&address=$toAddress&amount=$amount'),
|
|
headers: <String, String>{
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
},
|
|
body: '{}',
|
|
)
|
|
.then((response) {
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
print(data);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Sent $amount HNS to $toAddress'),
|
|
),
|
|
);
|
|
if (sendMax) {
|
|
amountController.text = '';
|
|
} else {
|
|
amountController.text =
|
|
min(amount, walletBalance - 0.02).toStringAsFixed(2);
|
|
}
|
|
fetchWalletBalance();
|
|
} else {
|
|
// Handle error
|
|
print('Failed to send HNS');
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Failed to send HNS'),
|
|
),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}
|