Files
umbrix/lib/features/profiles/view/profiles_modal.dart

146 lines
4.8 KiB
Dart
Raw Normal View History

2023-07-06 17:18:41 +03:30
import 'package:flutter/material.dart';
2023-07-26 16:42:31 +03:30
import 'package:gap/gap.dart';
import 'package:hiddify/core/core_providers.dart';
import 'package:hiddify/core/router/router.dart';
import 'package:hiddify/domain/enums.dart';
import 'package:hiddify/domain/failures.dart';
import 'package:hiddify/domain/profiles/profiles.dart';
2023-07-24 19:45:58 +03:30
import 'package:hiddify/features/common/common.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/features/profiles/notifier/notifier.dart';
2023-07-26 16:42:31 +03:30
import 'package:hiddify/utils/utils.dart';
2023-07-06 17:18:41 +03:30
import 'package:hooks_riverpod/hooks_riverpod.dart';
class ProfilesModal extends HookConsumerWidget {
const ProfilesModal({
super.key,
this.scrollController,
});
final ScrollController? scrollController;
@override
Widget build(BuildContext context, WidgetRef ref) {
2023-07-26 16:42:31 +03:30
final t = ref.watch(translationsProvider);
2023-07-06 17:18:41 +03:30
final asyncProfiles = ref.watch(profilesNotifierProvider);
2023-07-26 16:42:31 +03:30
return Stack(
children: [
CustomScrollView(
controller: scrollController,
slivers: [
switch (asyncProfiles) {
AsyncData(value: final profiles) => SliverList.builder(
itemBuilder: (context, index) {
final profile = profiles[index];
return ProfileTile(profile: profile);
},
itemCount: profiles.length,
),
AsyncError(:final error) => SliverErrorBodyPlaceholder(
2023-08-26 17:01:51 +03:30
t.printError(error),
2023-07-26 16:42:31 +03:30
),
AsyncLoading() => const SliverLoadingBodyPlaceholder(),
_ => const SliverToBoxAdapter(),
},
const SliverGap(48),
],
),
Positioned.fill(
child: Align(
alignment: Alignment.bottomCenter,
child: ButtonBar(
alignment: MainAxisAlignment.center,
children: [
FilledButton.icon(
onPressed: () {
const AddProfileRoute().push(context);
},
icon: const Icon(Icons.add),
2023-09-07 01:56:59 +03:30
label: Text(t.profile.add.shortBtnTxt),
2023-07-26 16:42:31 +03:30
),
FilledButton.icon(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return const ProfilesSortModal();
},
);
},
2023-07-26 19:59:34 +03:30
icon: const Icon(Icons.sort),
2023-09-07 01:56:59 +03:30
label: Text(t.general.sort),
2023-07-26 16:42:31 +03:30
),
],
),
),
),
],
);
}
}
class ProfilesSortModal extends HookConsumerWidget {
const ProfilesSortModal({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
return AlertDialog(
2023-09-07 01:56:59 +03:30
title: Text(t.general.sortBy),
2023-07-26 16:42:31 +03:30
content: Consumer(
builder: (context, ref, child) {
final sort = ref.watch(profilesSortNotifierProvider);
return SingleChildScrollView(
child: Column(
children: [
...ProfilesSort.values.map(
(e) {
final selected = sort.by == e;
final double arrowTurn =
sort.mode == SortMode.ascending ? 0 : 0.5;
return ListTile(
title: Text(e.present(t)),
onTap: () {
if (selected) {
ref
.read(profilesSortNotifierProvider.notifier)
.toggleMode();
} else {
ref
.read(profilesSortNotifierProvider.notifier)
.changeSort(e);
}
},
selected: selected,
2023-07-26 20:10:43 +03:30
leading: Icon(e.icon),
2023-07-26 16:42:31 +03:30
trailing: selected
? IconButton(
onPressed: () {
ref
.read(profilesSortNotifierProvider.notifier)
.toggleMode();
},
icon: AnimatedRotation(
turns: arrowTurn,
duration: const Duration(milliseconds: 100),
2023-09-12 00:05:44 +03:30
child: Icon(
Icons.arrow_upward,
semanticLabel: sort.mode.name,
),
2023-07-26 16:42:31 +03:30
),
)
: null,
);
},
),
],
),
);
},
2023-07-06 17:18:41 +03:30
),
);
}
}