mirror of
https://github.com/lifegpc/eh_downloader_flutter.git
synced 2026-07-08 01:30:28 +08:00
Add user managemant page
This commit is contained in:
@@ -13,6 +13,7 @@ class HomeDrawer extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context)!;
|
||||
return Drawer(
|
||||
child: ListView(
|
||||
children: <Widget>[
|
||||
@@ -26,7 +27,7 @@ class HomeDrawer extends StatelessWidget {
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.collections),
|
||||
title: Text(AppLocalizations.of(context)!.galleries),
|
||||
title: Text(i18n.galleries),
|
||||
onTap: () {
|
||||
Scaffold.of(context).closeDrawer();
|
||||
context.push("/galleries");
|
||||
@@ -35,7 +36,7 @@ class HomeDrawer extends StatelessWidget {
|
||||
auth.isAdmin == true
|
||||
? ListTile(
|
||||
leading: const Icon(Icons.admin_panel_settings),
|
||||
title: Text(AppLocalizations.of(context)!.serverSettings),
|
||||
title: Text(i18n.serverSettings),
|
||||
onTap: () {
|
||||
Scaffold.of(context).closeDrawer();
|
||||
context.push("/server_settings");
|
||||
@@ -45,13 +46,22 @@ class HomeDrawer extends StatelessWidget {
|
||||
auth.canManageTasks == true
|
||||
? ListTile(
|
||||
leading: const Icon(Icons.task),
|
||||
title: Text(AppLocalizations.of(context)!.taskManager),
|
||||
title: Text(i18n.taskManager),
|
||||
onTap: () {
|
||||
Scaffold.of(context).closeDrawer();
|
||||
context.push("/task_manager");
|
||||
},
|
||||
)
|
||||
: Container(),
|
||||
auth.isAdmin == true
|
||||
? ListTile(
|
||||
leading: const Icon(Icons.manage_accounts),
|
||||
title: Text(i18n.userManagemant),
|
||||
onTap: () {
|
||||
Scaffold.of(context).closeDrawer();
|
||||
context.push("/users");
|
||||
})
|
||||
: Container(),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.settings),
|
||||
title: Text(AppLocalizations.of(context)!.settings),
|
||||
|
||||
156
lib/pages/users.dart
Normal file
156
lib/pages/users.dart
Normal file
@@ -0,0 +1,156 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import '../api/user.dart';
|
||||
import '../components/user_card.dart';
|
||||
import '../globals.dart';
|
||||
|
||||
final _log = Logger("UsersPage");
|
||||
|
||||
class UsersPage extends StatefulWidget {
|
||||
const UsersPage({super.key});
|
||||
|
||||
static const String routeName = '/users';
|
||||
|
||||
@override
|
||||
State<UsersPage> createState() => _UsersPage();
|
||||
}
|
||||
|
||||
class _UsersPage extends State<UsersPage> with ThemeModeWidget, IsTopWidget2 {
|
||||
List<BUser>? _users;
|
||||
bool _isLoading = false;
|
||||
CancelToken? _cancel;
|
||||
Object? _error;
|
||||
Future<void> _fetchData() async {
|
||||
try {
|
||||
_cancel = CancelToken();
|
||||
_isLoading = true;
|
||||
final users = (await api.getUsers(all: true)).unwrap();
|
||||
if (!_cancel!.isCancelled) {
|
||||
setState(() {
|
||||
_users = users;
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
if (!_cancel!.isCancelled) {
|
||||
_log.severe("Failed to load user list:", e);
|
||||
setState(() {
|
||||
_error = e;
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!tryInitApi(context)) {
|
||||
return Container();
|
||||
}
|
||||
if (auth.isAdmin == false) {
|
||||
SchedulerBinding.instance.addPostFrameCallback((_) {
|
||||
context.go("/");
|
||||
});
|
||||
return Container();
|
||||
}
|
||||
final isLoading = _users == null && _error == null;
|
||||
if (isLoading && !_isLoading) _fetchData();
|
||||
final i18n = AppLocalizations.of(context)!;
|
||||
final th = Theme.of(context);
|
||||
if (isTop(context)) {
|
||||
setCurrentTitle(i18n.userManagemant, th.primaryColor.value);
|
||||
}
|
||||
return Scaffold(
|
||||
appBar: _users == null
|
||||
? AppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () {
|
||||
context.canPop() ? context.pop() : context.go("/");
|
||||
},
|
||||
),
|
||||
title: Text(i18n.userManagemant),
|
||||
actions: [
|
||||
buildThemeModeIcon(context),
|
||||
buildMoreVertSettingsButon(context),
|
||||
],
|
||||
)
|
||||
: null,
|
||||
body: isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _users != null
|
||||
? _buildMain(context)
|
||||
: Center(
|
||||
child: Text("Error: $_error"),
|
||||
));
|
||||
}
|
||||
|
||||
Widget _buildMain(BuildContext context) {
|
||||
final size = MediaQuery.of(context).size;
|
||||
return Stack(children: [
|
||||
_buildUserList(context),
|
||||
Positioned(
|
||||
bottom: size.height / 10,
|
||||
right: size.width / 10,
|
||||
child: _buildIconList(context)),
|
||||
]);
|
||||
}
|
||||
|
||||
Widget _buildAddIcon(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context)!;
|
||||
return IconButton(
|
||||
onPressed: () {
|
||||
context.push("/dialog/user/new");
|
||||
},
|
||||
tooltip: i18n.create,
|
||||
icon: const Icon(Icons.add));
|
||||
}
|
||||
|
||||
Widget _buildIconList(BuildContext context) {
|
||||
return Row(children: [
|
||||
_buildAddIcon(context),
|
||||
]);
|
||||
}
|
||||
|
||||
Widget _buildUserList(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context)!;
|
||||
return CustomScrollView(slivers: [
|
||||
SliverAppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () {
|
||||
context.canPop() ? context.pop() : context.go("/");
|
||||
},
|
||||
),
|
||||
title: Text(i18n.userManagemant),
|
||||
actions: [
|
||||
buildThemeModeIcon(context),
|
||||
buildMoreVertSettingsButon(context),
|
||||
],
|
||||
floating: true,
|
||||
),
|
||||
_buildSliverGrid(context),
|
||||
]);
|
||||
}
|
||||
|
||||
Widget _buildSliverGrid(BuildContext context) {
|
||||
return SliverGrid(
|
||||
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
||||
maxCrossAxisExtent: 360.0,
|
||||
mainAxisSpacing: 10.0,
|
||||
crossAxisSpacing: 10.0,
|
||||
childAspectRatio: 4.0,
|
||||
),
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(BuildContext context, int index) {
|
||||
return UserCard(_users![index]!);
|
||||
},
|
||||
childCount: _users!.length,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user