import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; 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 { @override void initState() { super.initState(); fetchDomains(); } List domains = []; Future 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'); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('${widget.wallet} - Domains'), // Add refresh button to the app bar actions: [ IconButton( icon: Icon(Icons.refresh), onPressed: fetchDomains, ), ], ), // Get wallet list from api and display here body: Center( child: LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { return ListView( children: [ ...domains.map((name) { return Card( child: ListTile( title: Text(name.name), subtitle: Text(name.state), trailing: Text(name.value.toString()), ), ); }), ], ); }, ), ), ); } } 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}); }