import 'package:flutter/material.dart'; import 'package:kulinar_app/constants.dart'; import 'package:kulinar_app/util/file_handler.dart'; import 'package:kulinar_app/widgets/custom_drawer_widget.dart'; import 'package:kulinar_app/models/data/recipe_data_class.dart'; import 'package:kulinar_app/models/data/settings_data_class.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; class SettingsView extends StatefulWidget { const SettingsView({Key? key}) : super(key: key); @override _SettingsViewState createState() => _SettingsViewState(); } class _SettingsViewState extends State { final TextEditingController _controller = TextEditingController(); final FocusNode _focusNode = FocusNode(); @override void initState() { super.initState(); _controller.text = SettingsData.settings["serverURL"]!; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(AppLocalizations.of(context)!.category8)), drawer: CustomDrawer(initalIndex: 5), body: Builder( builder: (BuildContext context) => ListView( children: [ _buildTinyTitle(AppLocalizations.of(context)!.settingTitle1), ListTile( subtitle: Text(AppLocalizations.of(context)!.explanation1, style: cDetailsTextStyle), title: Text(AppLocalizations.of(context)!.setting11, style: cDefaultTextStyle), trailing: _buildShowPhotosDropDownButton(), ), _buildTinyTitle(AppLocalizations.of(context)!.settingTitle2), ListTile( subtitle: Text(AppLocalizations.of(context)!.explanation2, style: cDetailsTextStyle), title: Text(AppLocalizations.of(context)!.setting21, style: cDefaultTextStyle), trailing: _buildPhotoSourceDropDownButton(), ), /* ListTile( title: Text(AppLocalizations.of(context)!.setting22, style: cDefaultTextStyle), trailing: _buildServerIPInput(), ), */ ListTile( title: Text(AppLocalizations.of(context)!.setting22, style: cDefaultTextStyle), subtitle: Text(AppLocalizations.of(context)!.explanation3, style: cDetailsTextStyle), trailing: IconButton( icon: Icon(Icons.arrow_forward_rounded), onPressed: null, ), ), _buildTinyTitle(AppLocalizations.of(context)!.settingTitle3), ListTile( title: Text(AppLocalizations.of(context)!.setting31, style: cDefaultTextStyle), subtitle: Text(AppLocalizations.of(context)!.explanation4, style: cDetailsTextStyle), trailing: ElevatedButton( child: Text(AppLocalizations.of(context)!.option311, style: cOptionTextStyle.copyWith(color: cIconColor)), onPressed: () async { final data = { "recipes": RecipeData.encode(), "settings": SettingsData.encode(), }; await FileHandler.serializeFile(context, data); setState(() {}); }, ), ), ListTile( title: Text(AppLocalizations.of(context)!.setting32, style: cDefaultTextStyle), subtitle: Text(AppLocalizations.of(context)!.explanation5, style: cDetailsTextStyle), trailing: ElevatedButton( child: Text(AppLocalizations.of(context)!.option312, style: cOptionTextStyle.copyWith(color: cIconColor)), onPressed: () async { final _file = await FileHandler.pickDeserializableFile(context); if (_file == null) return; await FileHandler.deserializeFile(context, _file); setState(() {}); }, ), ), ], ), ), ); } void _updateSettings(String? text) { _focusNode.unfocus(); if (text != null) { SettingsData.settings["serverURL"] = text; SettingsData.save(); } } /* // TODO: Use later in direct settings? Widget _buildServerIPInput() { if (MediaQuery.of(context).viewInsets.bottom == 0) _updateSettings(null); return Container( width: 150.0, child: TextFormField( keyboardType: TextInputType.url, controller: _controller, style: cRecipeTextStyle, focusNode: _focusNode, cursorColor: cPrimaryColor, onEditingComplete: () { _updateSettings(_controller.text); }, ), ); } */ Widget _buildPhotoSourceDropDownButton() { return DropdownButton( value: int.parse(SettingsData.settings["photoSource"]!), items: [ DropdownMenuItem( child: Text(AppLocalizations.of(context)!.option111, style: cOptionTextStyle), value: 0, ), DropdownMenuItem( child: Text(AppLocalizations.of(context)!.option112, style: cOptionTextStyle), value: 1, ), ], onChanged: (value) { SettingsData.settings["photoSource"] = value.toString(); SettingsData.save(); setState(() {}); }, ); } Widget _buildShowPhotosDropDownButton() { return DropdownButton( value: int.parse(SettingsData.settings["showPhotos"]!), items: [ DropdownMenuItem( child: Text(AppLocalizations.of(context)!.option211, style: cOptionTextStyle), value: 0, ), DropdownMenuItem( child: Text(AppLocalizations.of(context)!.option212, style: cOptionTextStyle), value: 1, ), DropdownMenuItem( child: Text(AppLocalizations.of(context)!.option213, style: cOptionTextStyle), value: 2, ), DropdownMenuItem( child: Text(AppLocalizations.of(context)!.option214, style: cOptionTextStyle), value: 3, ), ], onChanged: (value) { SettingsData.settings["showPhotos"] = value.toString(); SettingsData.save(); setState(() {}); }, ); } Widget _buildTinyTitle(String title) { return Padding( padding: const EdgeInsets.only(top: 15.0, left: 15.0), child: Text(title, style: cTinyTitleStyle), ); } }