1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/ui/routes/settings/application_settings.dart

130 lines
3.6 KiB
Dart
Raw Normal View History

2020-05-12 15:50:05 +00:00
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/ui/widgets/async_screen_widget.dart';
2021-02-12 21:36:22 +00:00
import 'package:comunic/ui/widgets/settings/header_spacer_section.dart';
2020-05-12 15:50:05 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
import 'package:settings_ui/settings_ui.dart';
/// Application settings
///
/// @author Pierre Hubert
2020-05-13 16:12:45 +00:00
class ApplicationSettings extends StatefulWidget {
2020-05-12 15:50:05 +00:00
@override
2020-05-13 16:21:30 +00:00
_ApplicationSettingsState createState() => _ApplicationSettingsState();
2020-05-12 15:50:05 +00:00
}
2020-05-13 16:21:30 +00:00
class _ApplicationSettingsState extends State<ApplicationSettings> {
2020-05-12 15:50:05 +00:00
PreferencesHelper _preferencesHelper;
Future<void> _refresh() async {
_preferencesHelper = await PreferencesHelper.getInstance();
}
@override
Widget build(BuildContext context) => AsyncScreenWidget(
onReload: _refresh,
onBuild: _buildSections,
errorMessage: tr("Could not load settings!"));
Widget _buildSections() {
return SettingsList(
2020-05-13 16:21:30 +00:00
sections: [
2021-02-12 21:36:22 +00:00
HeadSpacerSection(),
2020-05-13 16:21:30 +00:00
_buildAppearanceSection(),
_buildGeneralSection(),
_buildDebugSection()
],
2020-05-12 15:50:05 +00:00
);
}
/// Appearance section
SettingsSection _buildAppearanceSection() => SettingsSection(
title: tr("Appearance"),
tiles: [
_PreferencesSettingsTile(
preferencesKey: PreferencesKeyList.ENABLE_DARK_THEME,
title: tr("Enable dark theme"),
subtitle: null,
onChange: _updatedSettings,
helper: _preferencesHelper,
),
],
);
/// General section
SettingsSection _buildGeneralSection() => SettingsSection(
title: tr("General"),
tiles: [
SettingsTile(
title: tr("About this application"),
subtitle: tr("Learn more about us"),
2021-02-07 16:09:08 +00:00
onPressed: (_) => showAboutAppDialog(context),
2020-05-12 15:50:05 +00:00
)
],
);
2020-05-13 16:21:30 +00:00
/// Debug section
SettingsSection _buildDebugSection() => SettingsSection(
title: tr("Debug features"),
tiles: [
2020-05-13 16:26:21 +00:00
// Force mobile mode
2020-05-13 16:21:30 +00:00
_PreferencesSettingsTile(
preferencesKey: PreferencesKeyList.FORCE_MOBILE_MODE,
title: tr("Force mobile mode"),
subtitle: tr(
"Force the smartphone mode of the application to be used, even when tablet mode could be used."),
onChange: _updatedSettings,
helper: _preferencesHelper,
),
2020-05-13 16:26:21 +00:00
// Show performances overlay
_PreferencesSettingsTile(
preferencesKey: PreferencesKeyList.SHOW_PERFORMANCE_OVERLAY,
title: tr("Show performances overlay"),
subtitle: null,
onChange: _updatedSettings,
helper: _preferencesHelper,
),
2020-05-13 16:21:30 +00:00
],
);
2020-05-12 15:50:05 +00:00
/// Apply new settings
_updatedSettings() {
setState(() {});
applyNewThemeSettings(context);
2020-05-12 15:50:05 +00:00
}
}
class _PreferencesSettingsTile extends SettingsTile {
final PreferencesKeyList preferencesKey;
final String title;
final String subtitle;
final Function onChange;
final PreferencesHelper helper;
const _PreferencesSettingsTile({
@required this.preferencesKey,
@required this.title,
@required this.subtitle,
@required this.onChange,
@required this.helper,
});
@override
Widget build(BuildContext context) {
return SettingsTile.switchTile(
title: title,
subtitle: subtitle,
onToggle: _doChange,
switchValue: helper.getBool(preferencesKey),
);
}
void _doChange(bool value) async {
await helper.setBool(preferencesKey, value);
onChange();
}
}