Raid_Manager/app/lib/disk_page.dart

83 lines
2.4 KiB
Dart
Raw Normal View History

2022-12-06 23:19:10 +00:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
2022-12-06 23:19:10 +00:00
import 'package:raid_manager/api/request.dart';
import 'package:raid_manager/breadcrumb_page/breadcrumb_page.dart';
2022-12-06 23:19:10 +00:00
import 'package:raid_manager/types/disk.dart';
import 'package:raid_manager/utils/disk_info_page.dart';
2022-12-06 23:19:10 +00:00
import 'package:raid_manager/utils/file_formatter.dart';
import 'breadcrumb_page/breadcrumb_controller.dart';
2022-12-06 23:19:10 +00:00
class DiskPage extends StatefulWidget {
const DiskPage({Key? key}) : super(key: key);
@override
State<DiskPage> createState() => _DiskPageState();
}
class _DiskPageState extends State<DiskPage> {
Future<List<Disk>> fetchDisks() async {
return ((await getJson('/api/disks')) as List)
2022-12-06 23:19:10 +00:00
.map((e) => Disk.fromJson(e))
.toList(growable: false);
}
@override
void initState() {
super.initState();
myFetch = fetchDisks();
}
late Future<List<Disk>> myFetch;
Widget _buildMainPage() {
return Expanded(
child: FutureBuilder(
future: myFetch,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const CircularProgressIndicator();
}
if (snapshot.hasError) {
print(snapshot.error);
return const Text("errored");
} else if (snapshot.hasData) {
final data = snapshot.data!;
return ListView.builder(
itemCount: data.length,
physics: const AlwaysScrollableScrollPhysics(),
itemBuilder: (context, idx) {
return ListTile(
title: Text(
data[idx].name,
style: Theme.of(context).textTheme.headlineMedium,
),
subtitle: Text(
data[idx].size.readableFileSize(),
style: Theme.of(context).textTheme.labelMedium,
),
onTap: () {
Provider.of<BreadCrumbController>(context, listen: false)
.pushPage(const DiskInfoPage(), data[idx].name);
},
);
},
);
} else {
return const Text("loading...");
}
},
),
);
}
2022-12-06 23:19:10 +00:00
@override
Widget build(BuildContext context) {
return BreadCrumbPage(
mainPage: _buildMainPage(),
rootName: "Disks",
2022-12-06 23:19:10 +00:00
);
}
}