2024-12-15 17:04:35 +00:00
|
|
|
part of 'server_settings.dart';
|
2022-01-25 17:00:47 +00:00
|
|
|
|
|
|
|
final List<Location> locations = timeZoneDatabase.locations.values.toList()
|
2022-06-05 22:40:34 +00:00
|
|
|
..sort(
|
|
|
|
(final l1, final l2) =>
|
|
|
|
l1.currentTimeZone.offset.compareTo(l2.currentTimeZone.offset),
|
|
|
|
);
|
2022-01-25 17:00:47 +00:00
|
|
|
|
2024-12-15 17:04:35 +00:00
|
|
|
@RoutePage()
|
|
|
|
class SelectTimezonePage extends StatefulWidget {
|
|
|
|
const SelectTimezonePage({super.key});
|
2022-01-25 17:00:47 +00:00
|
|
|
|
|
|
|
@override
|
2024-12-15 17:04:35 +00:00
|
|
|
State<SelectTimezonePage> createState() => _SelectTimezonePageState();
|
2022-01-25 17:00:47 +00:00
|
|
|
}
|
|
|
|
|
2024-12-15 17:04:35 +00:00
|
|
|
class _SelectTimezonePageState extends State<SelectTimezonePage> {
|
2022-10-06 14:46:29 +00:00
|
|
|
final ScrollController scrollController = ScrollController();
|
|
|
|
final TextEditingController searchController = TextEditingController();
|
|
|
|
|
|
|
|
String? timezoneFilterValue;
|
2022-10-20 19:23:55 +00:00
|
|
|
bool isSearching = false;
|
2022-01-25 17:00:47 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2022-05-16 20:30:14 +00:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
|
2022-10-06 14:46:29 +00:00
|
|
|
searchController.addListener(() {
|
|
|
|
setState(() {
|
|
|
|
timezoneFilterValue =
|
|
|
|
searchController.text.isNotEmpty ? searchController.text : null;
|
|
|
|
});
|
|
|
|
});
|
2022-01-25 17:00:47 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
void _afterLayout(final _) {
|
|
|
|
final t = DateTime.now().timeZoneOffset;
|
|
|
|
final index = locations.indexWhere(
|
|
|
|
(final element) =>
|
|
|
|
Duration(milliseconds: element.currentTimeZone.offset) == t,
|
|
|
|
);
|
2022-02-08 21:01:08 +00:00
|
|
|
print(t);
|
|
|
|
|
2022-01-25 17:00:47 +00:00
|
|
|
if (index >= 0) {
|
2022-10-06 14:46:29 +00:00
|
|
|
scrollController.animateTo(
|
2022-06-05 22:40:34 +00:00
|
|
|
60.0 * index,
|
|
|
|
duration: const Duration(milliseconds: 300),
|
|
|
|
curve: Curves.easeIn,
|
|
|
|
);
|
2022-01-25 17:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-10-06 14:46:29 +00:00
|
|
|
scrollController.dispose();
|
|
|
|
searchController.dispose();
|
2022-01-25 17:00:47 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-03-27 17:02:44 +00:00
|
|
|
Widget build(final BuildContext context) {
|
|
|
|
final isDesktop = Breakpoints.mediumAndUp.isActive(context);
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
automaticallyImplyLeading: false,
|
|
|
|
title: (isDesktop || isSearching)
|
|
|
|
? TextField(
|
|
|
|
readOnly: false,
|
|
|
|
textAlign: TextAlign.start,
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
enabled: true,
|
|
|
|
controller: searchController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
errorText: null,
|
|
|
|
hintText: 'server.timezone_search_bar'.tr(),
|
2022-10-20 19:23:55 +00:00
|
|
|
),
|
2023-03-27 17:02:44 +00:00
|
|
|
)
|
|
|
|
: Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 4.0),
|
|
|
|
child: Text('server.select_timezone'.tr()),
|
2022-10-20 19:23:55 +00:00
|
|
|
),
|
2023-03-27 17:02:44 +00:00
|
|
|
leading: !isDesktop
|
|
|
|
? IconButton(
|
|
|
|
icon: const Icon(Icons.arrow_back),
|
|
|
|
onPressed: isSearching
|
|
|
|
? () => setState(() => isSearching = false)
|
|
|
|
: () => Navigator.of(context).pop(),
|
|
|
|
)
|
|
|
|
: null,
|
|
|
|
actions: [
|
|
|
|
if (!isSearching && !isDesktop)
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(Icons.search),
|
|
|
|
onPressed: () => setState(() => isSearching = true),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
body: SafeArea(
|
|
|
|
child: ListView(
|
|
|
|
controller: scrollController,
|
|
|
|
children: locations
|
|
|
|
.where(
|
|
|
|
(final Location location) => timezoneFilterValue == null
|
|
|
|
? true
|
|
|
|
: location.name
|
|
|
|
.toLowerCase()
|
|
|
|
.contains(timezoneFilterValue!) ||
|
|
|
|
Duration(
|
|
|
|
milliseconds: location.currentTimeZone.offset,
|
|
|
|
)
|
2023-07-02 15:24:07 +00:00
|
|
|
.toTimezoneOffsetFormat()
|
2023-03-27 17:02:44 +00:00
|
|
|
.contains(timezoneFilterValue!),
|
|
|
|
)
|
|
|
|
.toList()
|
|
|
|
.asMap()
|
|
|
|
.map(
|
|
|
|
(final key, final value) => locationToListTile(key, value),
|
|
|
|
)
|
|
|
|
.values
|
|
|
|
.toList(),
|
2022-06-05 22:40:34 +00:00
|
|
|
),
|
2023-03-27 17:02:44 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-10-06 14:46:29 +00:00
|
|
|
|
2023-03-27 17:02:44 +00:00
|
|
|
MapEntry<int, ListTile> locationToListTile(
|
2022-10-26 16:26:09 +00:00
|
|
|
final int key,
|
|
|
|
final Location location,
|
|
|
|
) {
|
2022-10-06 14:46:29 +00:00
|
|
|
final duration = Duration(milliseconds: location.currentTimeZone.offset);
|
|
|
|
final area = location.currentTimeZone.abbreviation
|
|
|
|
.replaceAll(RegExp(r'[\d+()-]'), '');
|
|
|
|
|
|
|
|
return MapEntry(
|
|
|
|
key,
|
2023-03-27 17:02:44 +00:00
|
|
|
ListTile(
|
|
|
|
title: Text(
|
|
|
|
location.name,
|
2022-10-06 14:46:29 +00:00
|
|
|
),
|
2023-03-27 17:02:44 +00:00
|
|
|
subtitle: Text(
|
2023-07-02 15:24:07 +00:00
|
|
|
'GMT ${duration.toTimezoneOffsetFormat()} ${area.isNotEmpty ? '($area)' : ''}',
|
2022-10-06 14:46:29 +00:00
|
|
|
),
|
2023-03-27 17:02:44 +00:00
|
|
|
onTap: () {
|
2024-02-20 16:33:24 +00:00
|
|
|
context.read<JobsCubit>().addJob(
|
|
|
|
ChangeServerTimezoneJob(
|
|
|
|
timezone: location.name,
|
|
|
|
),
|
2023-03-27 17:02:44 +00:00
|
|
|
);
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
2022-10-06 14:46:29 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-01-25 17:00:47 +00:00
|
|
|
}
|