actions-test/lib/screens/home.dart
Jakob Meier 5b9b48cd24
Migrated to GoRouter
Reasons for migration:
- buggy behaviour from old router
- GoRouter is a recommended flutter plugin
- ShellRoutes allow exposing Providers
  to a limited scope of routes
- GoRoutes provides named routes,
  and the navigator allows us to provide parameters directly.
2023-03-28 14:54:39 +02:00

167 lines
4.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:outbag_app/backend/permissions.dart';
import 'package:outbag_app/backend/request.dart';
import 'package:outbag_app/backend/user.dart';
import 'package:outbag_app/tools/fetch_wrapper.dart';
import 'package:provider/provider.dart';
import 'package:go_router/go_router.dart';
import '../backend/room.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<Room> rooms = [];
@override
void initState() {
super.initState();
// wait for background room changes
Room.listen((_) async {
try {
final newRooms = await Room.listRooms();
setState(() {
rooms = newRooms;
});
} catch (_) {}
});
WidgetsBinding.instance.addPostFrameCallback((_) => fetchList());
}
void fetchList() async {
final sm = ScaffoldMessenger.of(context);
final user = context.read<User>();
// load cached rooms
try {
final newRooms = await Room.listRooms();
setState(() {
rooms = newRooms;
});
} catch (_) {}
doNetworkRequest(
sm,
req: () => postWithCreadentials(
path: 'listRooms', credentials: user, target: user.server, body: {}),
onOK: (body) async {
final List<Room> list = body['data'].map<Room>((json) {
return Room.fromJSON(json);
}).toList();
for (Room r in list) {
await r.toDisk();
}
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Outbag"),
actions: [
IconButton(
icon: const Icon(Icons.search),
tooltip: "Search",
onPressed: () {
// show searchbar
// NOTE: location currently unknown
},
),
MenuAnchor(
builder: (ctx, controller, child) {
return IconButton(
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
icon: const Icon(Icons.more_vert),
);
},
menuChildren: [
MenuItemButton(
leadingIcon: const Icon(Icons.settings),
child: const Text('Settings'),
onPressed: () {
// show settings screen
context.goNamed('settings');
}),
...(context.watch<AccountMeta?>() != null &&
(context.watch<AccountMeta?>()?.permissions)! &
ServerPermission.allManagement !=
0)
? [
MenuItemButton(
leadingIcon: const Icon(Icons.dns),
child: const Text('Server Dashboard'),
onPressed: () {
// show settings screen
context.goNamed('dash');
}),
]
: [],
MenuItemButton(
leadingIcon: const Icon(Icons.info_rounded),
child: const Text('About'),
onPressed: () {
// show about screen
context.goNamed('about');
}),
],
)
],
),
body: ListView.builder(
itemCount: rooms.length,
itemBuilder: (ctx, i) {
final room = rooms[i];
return Card(
margin: const EdgeInsets.all(8.0),
clipBehavior: Clip.antiAliasWithSaveLayer,
semanticContainer: true,
child: InkWell(
onTap: () {
// open room
context.goNamed('room',
params: {'server': room.serverTag, 'id': room.id});
},
onLongPress: () {
// open bottom sheet
// NOTE: feature yet to be confirmed
},
child: Container(
padding: const EdgeInsets.fromLTRB(10, 5, 5, 10),
child: ListTile(
title: Text(room.name),
visualDensity: const VisualDensity(vertical: 3),
subtitle: Text(room.description),
leading: AspectRatio(
aspectRatio: 1 / 1,
child: SvgPicture.asset("${room.icon?.img}"),
),
hoverColor: Colors.transparent,
))));
},
),
floatingActionButton: FloatingActionButton.extended(
label: const Text('Add Room'),
icon: const Icon(Icons.add),
onPressed: () {
// create new room
context.goNamed('add-room');
},
tooltip: 'Add new Room',
),
);
}
}