firewallet-mobile/lib/domains.dart

131 lines
3.2 KiB
Dart
Raw Normal View History

2024-06-03 14:08:57 +10:00
import 'package:flutter/material.dart';
2024-06-03 17:06:45 +10:00
import 'package:http/http.dart' as http;
import 'dart:convert';
2024-06-03 14:08:57 +10:00
class DomainsPage extends StatefulWidget {
DomainsPage({Key? key, required this.uuid, required this.wallet})
: super(key: key);
final String uuid;
String wallet;
@override
_DomainsPageState createState() => _DomainsPageState();
}
class _DomainsPageState extends State<DomainsPage> {
@override
void initState() {
super.initState();
2024-06-03 17:06:45 +10:00
fetchDomains();
}
List<Domain> domains = [];
Future<void> fetchDomains() async {
// Fetch domains from api
final response = await http.get(Uri.parse(
'https://api.firewallet.au/wallet/domains?uuid=${widget.uuid}&name=${widget.wallet}'));
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
if (data is Map && data.containsKey('error')) {
print('Error: ${data['error']}');
return;
}
// Return if empty
if (data.isEmpty) {
setState(() {
domains = [
Domain(
height: 0,
highest: 0,
name: 'No domains found',
state: 'CLOSED',
stats: DomainStats(unParsed: 'No domains found'),
value: 0)
];
});
return;
}
final domainsData = data as List;
setState(() {
domains = domainsData
.map((domain) => Domain(
name: domain['name'],
state: domain['state'],
height: domain['height'],
highest: domain['highest'],
stats: DomainStats(unParsed: domain['stats'].toString()),
value: domain['value'],
))
.toList();
});
} else {
// Handle error
print('Failed to load wallet names');
}
2024-06-03 14:08:57 +10:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2024-06-04 15:20:39 +10:00
title: Text('${widget.wallet} - Domains'),
// Add refresh button to the app bar
actions: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
onPressed: fetchDomains,
),
],
2024-06-03 14:08:57 +10:00
),
// Get wallet list from api and display here
body: Center(
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
2024-06-03 17:06:45 +10:00
return ListView(
2024-06-03 14:08:57 +10:00
children: <Widget>[
2024-06-03 17:06:45 +10:00
...domains.map((name) {
return Card(
child: ListTile(
title: Text(name.name),
subtitle: Text(name.state),
trailing: Text(name.value.toString()),
),
);
}),
2024-06-03 14:08:57 +10:00
],
);
},
),
),
);
}
}
2024-06-03 17:06:45 +10:00
class Domain {
final String name;
final String state;
final int height;
final double highest;
final DomainStats stats;
final double value;
Domain({
required this.name,
required this.state,
required this.height,
required this.highest,
required this.stats,
required this.value,
});
}
class DomainStats {
final String unParsed;
DomainStats({required this.unParsed});
}