Skip to content

Commit 077b269

Browse files
committed
add languages to the app
1 parent 7de3033 commit 077b269

13 files changed

+316
-73
lines changed

lib/Pages/home_page.dart

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:lottie/lottie.dart';
99
import 'package:text_to_image_gen/Pages/settings_page.dart';
1010
import 'package:text_to_image_gen/bloc/image_cubit.dart';
1111
import 'package:path/path.dart' as xp;
12+
import 'package:text_to_image_gen/utils/app_language.dart';
1213

1314
import '../bloc/app_directory_cubit.dart';
1415
import '../utils/strings.dart';
@@ -61,7 +62,7 @@ class _HomePageState extends State<HomePage> {
6162
await file.writeAsBytes(canvas).whenComplete(() {
6263
ScaffoldMessenger.of(context).showSnackBar(
6364
SnackBar(
64-
content: Text('Image was Downloaded in $filePath'),
65+
content: Text('${translation(context).imageWasSaved} : $filePath'),
6566
elevation: 10,
6667
),
6768
);
@@ -127,6 +128,9 @@ class _HomePageState extends State<HomePage> {
127128
FocusScope.of(context).unfocus();
128129
}
129130
scaffoldKey.currentState?.openDrawer();
131+
if (FocusScope.of(context).hasFocus) {
132+
FocusScope.of(context).unfocus();
133+
}
130134
},
131135
child: Icon(
132136
Iconsax.element_plus,
@@ -173,8 +177,8 @@ class _HomePageState extends State<HomePage> {
173177
autofocus: false,
174178
controller: _textEditingController,
175179
decoration: InputDecoration(
176-
hintText: "Enter Anything in Your Mind",
177-
labelText: "Enter Anything in Your Mind",
180+
hintText: translation(context).putAnythingInYourMind,
181+
labelText: translation(context).putAnythingInYourMind,
178182
border: const OutlineInputBorder(),
179183
suffixIcon: IconButton(
180184
onPressed: () {
@@ -237,7 +241,7 @@ class _HomePageState extends State<HomePage> {
237241
padding: EdgeInsets.symmetric(vertical: 14.0),
238242
child: Icon(
239243
Icons.gesture,
240-
size: 30,
244+
size: 32,
241245
),
242246
),
243247
),
@@ -250,18 +254,28 @@ class _HomePageState extends State<HomePage> {
250254
builder: (context, state) {
251255
if (state is ImageLoading) {
252256
return Center(
253-
child: SizedBox(
254-
height: 300,
255-
width: 300,
256-
child: Padding(
257-
padding: const EdgeInsets.all(100),
258-
child: Lottie.asset(
259-
'assets/animations/loading.json',
260-
frameRate: FrameRate(120),
261-
repeat: true,
262-
animate: true,
257+
child: Column(
258+
mainAxisAlignment: MainAxisAlignment.center,
259+
crossAxisAlignment: CrossAxisAlignment.center,
260+
children: [
261+
SizedBox(
262+
height: 300,
263+
width: 300,
264+
child: Padding(
265+
padding: const EdgeInsets.all(100),
266+
child: Lottie.asset(
267+
'assets/animations/loading.json',
268+
frameRate: FrameRate(120),
269+
repeat: true,
270+
animate: true,
271+
),
272+
),
263273
),
264-
),
274+
Text(
275+
translation(context).loading,
276+
style: const TextStyle(fontSize: 18),
277+
)
278+
],
265279
),
266280
);
267281
}
@@ -329,7 +343,7 @@ class _HomePageState extends State<HomePage> {
329343
),
330344
child: Center(
331345
child: Text(
332-
'Download',
346+
translation(context).download,
333347
style: TextStyle(
334348
color: Theme.of(context)
335349
.colorScheme
@@ -407,7 +421,7 @@ class _HomePageState extends State<HomePage> {
407421
),
408422
child: Center(
409423
child: Text(
410-
'Download',
424+
translation(context).download,
411425
style: TextStyle(
412426
color: Theme.of(context)
413427
.colorScheme
@@ -488,7 +502,7 @@ class _HomePageState extends State<HomePage> {
488502
),
489503
child: Center(
490504
child: Text(
491-
'Download',
505+
translation(context).download,
492506
style: TextStyle(
493507
color: Theme.of(context)
494508
.colorScheme
@@ -568,7 +582,7 @@ class _HomePageState extends State<HomePage> {
568582
),
569583
child: Center(
570584
child: Text(
571-
'Download',
585+
translation(context).download,
572586
style: TextStyle(
573587
color: Theme.of(context)
574588
.colorScheme
@@ -593,8 +607,11 @@ class _HomePageState extends State<HomePage> {
593607
}
594608
if (state is ImageError) {
595609
final error = state.error;
610+
if (kDebugMode) {
611+
print(error);
612+
}
596613
return Center(
597-
child: Text(error),
614+
child: Text("${translation(context).failed} .${translation(context).noResultFound}"),
598615
);
599616
}
600617
return Container();
@@ -613,7 +630,7 @@ class _HomePageState extends State<HomePage> {
613630
context: context,
614631
builder: (BuildContext context) {
615632
return SimpleDialog(
616-
title: const Text('Choose style for the image :'),
633+
title: Text('${translation(context).chooseStyle} :'),
617634
children: <Widget>[
618635
Padding(
619636
padding: const EdgeInsets.all(10.0),
@@ -655,13 +672,12 @@ class _HomePageState extends State<HomePage> {
655672
context: context,
656673
builder: (BuildContext context) {
657674
return AlertDialog(
658-
title: const Text('Choose Directory :'),
659-
content: const Text(
660-
'Do you want to Choose Directory to make the app can save images in your Device?'),
675+
title: Text('${translation(context).chooseDirectory} :'),
676+
content: Text(translation(context).confirmDirectory),
661677
actions: <Widget>[
662678
TextButton(
663679
onPressed: () => Navigator.of(context).pop(),
664-
child: const Text('No'),
680+
child: Text(translation(context).no),
665681
),
666682
TextButton(
667683
onPressed: () {
@@ -673,7 +689,7 @@ class _HomePageState extends State<HomePage> {
673689
),
674690
);
675691
},
676-
child: const Text('Yes'),
692+
child: Text(translation(context).yes),
677693
),
678694
],
679695
);

lib/Pages/settings_page.dart

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import 'package:flutter/material.dart';
77
import 'package:flutter_bloc/flutter_bloc.dart';
88
import 'package:permission_handler/permission_handler.dart';
99
import 'package:text_to_image_gen/bloc/app_directory_cubit.dart';
10+
import 'package:text_to_image_gen/bloc/app_language_cubit.dart';
1011

1112
import '../bloc/app_mode_cubit.dart';
1213
import '../bloc/app_theme_cubit.dart';
14+
import '../utils/app_language.dart';
15+
import '../utils/language.dart';
1316
import '../utils/strings.dart';
1417
import '../widgets/app_theme.dart';
1518

@@ -21,15 +24,20 @@ class SettingsPage extends StatefulWidget {
2124
}
2225

2326
class _SettingsPageState extends State<SettingsPage> {
27+
final FilePicker filePicker = FilePicker.platform;
28+
2429
late AppModeCubit _appModeCubit;
2530
late AppThemeCubit _appThemeCubit;
26-
final List<AppTheme> _themes = appThemes.keys.toList();
2731
late AppDirectoryCubit _appDirectoryCubit;
28-
final FilePicker filePicker = FilePicker.platform;
32+
late AppLanguageCubit _appLanguageCubit;
33+
34+
final List<AppTheme> _themes = appThemes.keys.toList();
35+
final List<Language> _languages = Language.languageList();
2936

3037
@override
3138
void initState() {
3239
super.initState();
40+
_appLanguageCubit = context.read<AppLanguageCubit>()..loadLanguage();
3341
_appDirectoryCubit = context.read<AppDirectoryCubit>()..loadPath();
3442
_appModeCubit = context.read<AppModeCubit>()..loadMode();
3543
_appThemeCubit = context.read<AppThemeCubit>()..loadTheme();
@@ -114,7 +122,7 @@ class _SettingsPageState extends State<SettingsPage> {
114122
Widget build(BuildContext context) {
115123
return Scaffold(
116124
appBar: AppBar(
117-
title: const Text('Settings'),
125+
title: Text(translation(context).settingsPage),
118126
),
119127
body: SingleChildScrollView(
120128
physics: const BouncingScrollPhysics(),
@@ -128,15 +136,60 @@ class _SettingsPageState extends State<SettingsPage> {
128136
padding: const EdgeInsets.all(8.0),
129137
child: ListTile(
130138
leading: const Icon(Icons.folder),
131-
title: const Text('Main Directory'),
139+
title: Text(translation(context).mainDirectory),
132140
subtitle: Text(state.path),
133141
trailing: ElevatedButton(
134142
onPressed: _pickDirectory,
135-
child: const Text('Change'),
143+
child: Text(translation(context).change),
136144
),
137145
),
138146
);
139147
}),
148+
BlocBuilder<AppLanguageCubit, AppLanguageState>(
149+
builder: (context, state) {
150+
return Padding(
151+
padding:
152+
const EdgeInsets.symmetric(vertical: 16, horizontal: 16),
153+
child: Column(
154+
mainAxisAlignment: MainAxisAlignment.center,
155+
crossAxisAlignment: CrossAxisAlignment.start,
156+
children: [
157+
Padding(
158+
padding: const EdgeInsets.symmetric(horizontal: 8.0),
159+
child: Text(
160+
'${translation(context).changeLanguage} :',
161+
style: const TextStyle(
162+
fontSize: 18,
163+
),
164+
),
165+
),
166+
ListView.builder(
167+
physics: const BouncingScrollPhysics(),
168+
shrinkWrap: true,
169+
itemCount: _languages.length,
170+
itemBuilder: (BuildContext context, int index) {
171+
Language language = _languages[index];
172+
return Padding(
173+
padding: const EdgeInsets.all(8.0),
174+
child: Container(
175+
decoration: BoxDecoration(
176+
color: Theme.of(context).splashColor,
177+
),
178+
child: CheckboxListTile(
179+
title: Text(language.name),
180+
value: state.locale.languageCode == _languages[index].languageCode,
181+
onChanged: (value) {
182+
_appLanguageCubit
183+
.changeLanguage(language.languageCode);
184+
},
185+
),
186+
),
187+
);
188+
},
189+
),
190+
]),
191+
);
192+
}),
140193
BlocBuilder<AppModeCubit, AppModeState>(
141194
builder: (context, state) {
142195
return Padding(
@@ -145,11 +198,11 @@ class _SettingsPageState extends State<SettingsPage> {
145198
mainAxisAlignment: MainAxisAlignment.center,
146199
crossAxisAlignment: CrossAxisAlignment.start,
147200
children: [
148-
const Padding(
149-
padding: EdgeInsets.symmetric(horizontal: 8.0),
201+
Padding(
202+
padding: const EdgeInsets.symmetric(horizontal: 8.0),
150203
child: Text(
151-
'App Mode ',
152-
style: TextStyle(
204+
"${translation(context).appMode} :",
205+
style: const TextStyle(
153206
fontSize: 18,
154207
),
155208
),
@@ -161,7 +214,7 @@ class _SettingsPageState extends State<SettingsPage> {
161214
color: Theme.of(context).splashColor,
162215
),
163216
child: CheckboxListTile(
164-
title: const Text('System Mode'),
217+
title: Text(translation(context).systemMode),
165218
value: state.mode == system,
166219
onChanged: (value) {
167220
if (value == true) {
@@ -178,7 +231,7 @@ class _SettingsPageState extends State<SettingsPage> {
178231
color: Theme.of(context).splashColor,
179232
),
180233
child: CheckboxListTile(
181-
title: const Text('Light Mode'),
234+
title: Text(translation(context).lightMode),
182235
value: state.mode == light,
183236
onChanged: (value) {
184237
if (value == true) {
@@ -195,7 +248,7 @@ class _SettingsPageState extends State<SettingsPage> {
195248
color: Theme.of(context).splashColor,
196249
),
197250
child: CheckboxListTile(
198-
title: const Text('Dark Mode'),
251+
title: Text(translation(context).darkMode),
199252
value: state.mode == dark,
200253
onChanged: (value) {
201254
if (value == true) {
@@ -219,11 +272,11 @@ class _SettingsPageState extends State<SettingsPage> {
219272
mainAxisAlignment: MainAxisAlignment.center,
220273
crossAxisAlignment: CrossAxisAlignment.start,
221274
children: [
222-
const Padding(
223-
padding: EdgeInsets.symmetric(horizontal: 8.0),
275+
Padding(
276+
padding: const EdgeInsets.symmetric(horizontal: 8.0),
224277
child: Text(
225-
'Themes',
226-
style: TextStyle(
278+
"${translation(context).themes} :",
279+
style: const TextStyle(
227280
fontSize: 18,
228281
),
229282
),

lib/bloc/app_language_cubit.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'dart:ui';
2+
3+
import 'package:equatable/equatable.dart';
4+
import 'package:flutter_bloc/flutter_bloc.dart';
5+
import 'package:text_to_image_gen/utils/app_language.dart';
6+
7+
part 'app_language_state.dart';
8+
9+
class AppLanguageCubit extends Cubit<AppLanguageState> {
10+
AppLanguageCubit(Locale locale) : super(AppLanguageInitial(locale)){
11+
loadLanguage();
12+
}
13+
14+
Future<void> loadLanguage() async {
15+
final selectedLanguage = await LanguageSharedPreferences.getLocale();
16+
emit(AppLanguageUpdate(selectedLanguage));
17+
}
18+
19+
Future<void> changeLanguage(String value) async {
20+
await LanguageSharedPreferences.setLocale(value).then((locale) {
21+
emit(AppLanguageUpdate(locale));
22+
});
23+
}
24+
}

lib/bloc/app_language_state.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
part of 'app_language_cubit.dart';
2+
3+
abstract class AppLanguageState extends Equatable {
4+
final Locale locale;
5+
6+
const AppLanguageState(this.locale);
7+
8+
@override
9+
List<Object> get props => [locale];
10+
}
11+
12+
class AppLanguageInitial extends AppLanguageState {
13+
const AppLanguageInitial(super.locale);
14+
}
15+
16+
class AppLanguageUpdate extends AppLanguageState {
17+
const AppLanguageUpdate(super.locale);
18+
}

lib/bloc/app_mode_state.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ abstract class AppModeState extends Equatable {
55

66
const AppModeState(this.mode);
77

8-
98
@override
109
List<Object?> get props => [mode];
1110
}
1211

1312
class AppModeInitial extends AppModeState {
14-
const AppModeInitial(super.mode);
13+
const AppModeInitial(super.mode);
1514
}
15+
1616
class AppModeUpdate extends AppModeState {
1717
const AppModeUpdate(super.mode);
1818
}

0 commit comments

Comments
 (0)