Skip to content

Commit

Permalink
feat: Improve language selection
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch committed Aug 17, 2024
1 parent 2ce37bf commit 0bb519b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 98 deletions.
6 changes: 5 additions & 1 deletion kitchenowl/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localized_locales/flutter_localized_locales.dart';
import 'package:kitchenowl/cubits/auth_cubit.dart';
import 'package:kitchenowl/cubits/server_info_cubit.dart';
import 'package:kitchenowl/cubits/settings_cubit.dart';
Expand Down Expand Up @@ -158,7 +159,10 @@ class _AppState extends State<App> {
onGenerateTitle: (BuildContext context) =>
AppLocalizations.of(context)!.appTitle,
localizationsDelegates:
AppLocalizations.localizationsDelegates,
AppLocalizations.localizationsDelegates +
[
LocaleNamesLocalizationsDelegate(),
],
supportedLocales: const [Locale('en')] +
AppLocalizations.supportedLocales,
theme: AppThemes.light(lightColorScheme),
Expand Down
88 changes: 88 additions & 0 deletions kitchenowl/lib/widgets/language_bottom_sheet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localized_locales/flutter_localized_locales.dart';
import 'package:kitchenowl/kitchenowl.dart';

class LanguageBottomSheet extends StatefulWidget {
final String title;
final String doneText;
final String? initialLanguage;
final String? nullText;
final Map<String, String>? supportedLanguages;

const LanguageBottomSheet({
super.key,
this.title = "",
this.doneText = "",
this.initialLanguage,
this.nullText,
required this.supportedLanguages,
});

@override
State<LanguageBottomSheet> createState() => _LanguageBottomSheetState();
}

class _LanguageBottomSheetState extends State<LanguageBottomSheet> {
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return SafeArea(
bottom: false,
child: ListView(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: Text(
AppLocalizations.of(context)!.languageSelect,
style: Theme.of(context).textTheme.headlineSmall,
),
),
const Divider(indent: 16, endIndent: 16),
ListTile(
selected: widget.initialLanguage == null,
title: Text(widget.nullText ?? AppLocalizations.of(context)!.none),
onTap: () => Navigator.of(context).pop(Nullable<String>.empty()),
trailing: widget.initialLanguage == null
? const Icon(Icons.check_circle_rounded)
: null,
),
for (final e in (widget.supportedLanguages?.entries
.map((e) => MapEntry(
e.key, LocaleNames.of(context)!.nameOf(e.key) ?? e.value))
.sorted((a, b) {
if (AppLocalizations.of(context)!.localeName == b.key ||
AppLocalizations.of(context)!.localeName == a.key)
return AppLocalizations.of(context)!.localeName == a.key
? -1
: 1;

if (widget.initialLanguage == b.key ||
widget.initialLanguage == a.key)
return widget.initialLanguage == a.key ? -1 : 1;

return a.value.compareTo(b.value);
}) ??
const <MapEntry<String, String>>[]))
ListTile(
selected: widget.initialLanguage == e.key,
title: Text(e.value),
onTap: () => Navigator.of(context).pop(
(widget.supportedLanguages?.containsKey(e.key) ?? false)
? Nullable(e.key)
: null,
),
trailing: widget.initialLanguage == e.key
? const Icon(Icons.check_circle_rounded)
: null,
),
SizedBox(height: MediaQuery.of(context).padding.bottom),
],
),
);
}
}
70 changes: 0 additions & 70 deletions kitchenowl/lib/widgets/language_dialog.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localized_locales/flutter_localized_locales.dart';
import 'package:kitchenowl/cubits/household_add_update/household_add_update_cubit.dart';
import 'package:kitchenowl/enums/views_enum.dart';
import 'package:kitchenowl/kitchenowl.dart';
import 'package:kitchenowl/widgets/language_dialog.dart';
import 'package:kitchenowl/widgets/language_bottom_sheet.dart';
import 'package:reorderables/reorderables.dart';

import 'view_settings_list_tile.dart';
Expand Down Expand Up @@ -83,46 +84,46 @@ class SliverHouseholdFeatureSettings<
previous.supportedLanguages != current.supportedLanguages,
builder: (context, state) {
if (state.language != null && !languageCanBeChanged) {
return Text(state.supportedLanguages?[state.language!] ??
return Text(LocaleNames.of(context)!.nameOf(state.language!) ??
state.supportedLanguages?[state.language!] ??
state.language!);
}

return LoadingElevatedButton(
child: Text(state.supportedLanguages?[state.language] ??
state.language ??
AppLocalizations.of(context)!.set),
child: Text(
LocaleNames.of(context)!.nameOf(state.language ?? "") ??
state.supportedLanguages?[state.language] ??
state.language ??
AppLocalizations.of(context)!.set),
onPressed: () async {
final language = await showDialog<String>(
final language = await showModalBottomSheet<Nullable<String>>(
context: context,
showDragHandle: true,
builder: (BuildContext context) {
return LanguageDialog(
return LanguageBottomSheet(
title: AppLocalizations.of(context)!.language,
doneText: AppLocalizations.of(context)!.set,
initialLanguage: state.language ??
AppLocalizations.of(context)!.localeName,
initialLanguage: state.language,
supportedLanguages: state.supportedLanguages,
);
},
);
if (language == null) {
BlocProvider.of<Cubit>(context).setLanguage(language);
} else {
if (askConfirmation) {
final confirm = await askForConfirmation(
context: context,
confirmText: AppLocalizations.of(context)!.set,
title: Text(
AppLocalizations.of(context)!.addLanguage,
),
content: Text(
AppLocalizations.of(context)!
.addLanguageConfirm(language),
),
);
if (!confirm) return;
}
BlocProvider.of<Cubit>(context).setLanguage(language);
if (language == null) return;
if (askConfirmation) {
final confirm = await askForConfirmation(
context: context,
confirmText: AppLocalizations.of(context)!.set,
title: Text(
AppLocalizations.of(context)!.addLanguage,
),
content: Text(
AppLocalizations.of(context)!
.addLanguageConfirm(language),
),
);
if (!confirm) return;
}
BlocProvider.of<Cubit>(context).setLanguage(language.value);
},
);
},
Expand Down
8 changes: 8 additions & 0 deletions kitchenowl/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_localized_locales:
dependency: "direct main"
description:
name: flutter_localized_locales
sha256: "478d10535edf07292e34cb4c757882edeeaf96d5e3dbb04b42733038bd41dd3f"
url: "https://pub.dev"
source: hosted
version: "2.0.5"
flutter_markdown:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions kitchenowl/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies:

responsive_builder: ^0.7.0
dynamic_color: ^1.7.0
flutter_localized_locales: ^2.0.5

azlistview_plus: ^3.0.0
flutter_markdown: ^0.7.1
Expand Down

0 comments on commit 0bb519b

Please sign in to comment.