-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4df4307
commit 4427d55
Showing
20 changed files
with
334 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
lib/features/language_setting/bloc/language_setting_cubit.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:public_chat/features/language_support/data/language.dart'; | ||
import 'package:public_chat/repository/database.dart'; | ||
|
||
part 'language_setting_state.dart'; | ||
|
||
class LanguageSettingCubit extends Cubit<String> { | ||
LanguageSettingCubit(super.initialState); | ||
|
||
void chooseLanguage(LanguageSupport dataLanguage) { | ||
emit(dataLanguage.code ?? 'en'); | ||
} | ||
|
||
void saveLanguage() { | ||
Database.instance.saveLanguageUser(state); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
lib/features/language_setting/bloc/language_setting_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
part of 'language_setting_cubit.dart'; | ||
|
||
abstract class LanguageSettingState extends Equatable { | ||
const LanguageSettingState(); | ||
} | ||
|
||
class LanguageSettingInitial extends LanguageSettingState { | ||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
class LanguageSettingSuccess extends LanguageSettingState { | ||
final String language; | ||
|
||
const LanguageSettingSuccess(this.language); | ||
|
||
@override | ||
List<Object?> get props => [language]; | ||
} | ||
|
||
class LanguageSettingFailed extends LanguageSettingState { | ||
final String reason; | ||
|
||
const LanguageSettingFailed(this.reason); | ||
|
||
@override | ||
List<Object?> get props => [reason]; | ||
} |
25 changes: 25 additions & 0 deletions
25
lib/features/language_setting/bloc/user_language_cubit.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:public_chat/repository/database.dart'; | ||
import 'package:public_chat/utils/bloc_extensions.dart'; | ||
|
||
class UserLanguageCubit extends Cubit<String> { | ||
StreamSubscription<String>? _userLanguageListen; | ||
UserLanguageCubit() : super('en'); | ||
|
||
void startListen() { | ||
_userLanguageListen = Database.instance.listenLanguageSettingUser()?.listen( | ||
(event) { | ||
emitSafely(event); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
Future<void> close() { | ||
_userLanguageListen?.cancel(); | ||
_userLanguageListen = null; | ||
return super.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:public_chat/features/language_setting/bloc/language_setting_cubit.dart'; | ||
import 'package:public_chat/features/language_setting/bloc/user_language_cubit.dart'; | ||
import 'package:public_chat/features/language_support/bloc/language_support_cubit.dart'; | ||
import 'package:public_chat/features/language_support/data/language.dart'; | ||
|
||
extension GetLanguageSupport on BuildContext { | ||
LanguageSupport languageUserObject(String language) { | ||
var languageUser = read<LanguageSupportCubit>().state.firstWhere( | ||
(element) => element == LanguageSupport(code: language), | ||
); | ||
return languageUser; | ||
} | ||
|
||
String get languageUser { | ||
return read<UserLanguageCubit>().state; | ||
} | ||
|
||
LanguageSupport get languageTempUser { | ||
var languageTempUser = read<LanguageSettingCubit>().state; | ||
return LanguageSupport(code: languageTempUser); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
lib/features/language_setting/ui/language_setting_screen.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:public_chat/features/language_setting/bloc/user_language_cubit.dart'; | ||
import 'package:public_chat/features/language_support/bloc/language_support_cubit.dart'; | ||
import 'package:public_chat/utils/locale_support.dart'; | ||
|
||
import '../../language_support/data/language.dart'; | ||
import '../bloc/language_setting_cubit.dart'; | ||
|
||
class SettingLanguageScreen extends StatelessWidget { | ||
const SettingLanguageScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) => BlocProvider<LanguageSettingCubit>( | ||
create: (context) => | ||
LanguageSettingCubit(context.read<UserLanguageCubit>().state), | ||
child: const _SettingLangeScreenBody(), | ||
); | ||
} | ||
|
||
class _SettingLangeScreenBody extends StatelessWidget { | ||
const _SettingLangeScreenBody(); | ||
|
||
@override | ||
Widget build(BuildContext context) => Scaffold( | ||
appBar: AppBar( | ||
title: Text(context.locale.settingLanguage), | ||
), | ||
body: BlocBuilder<LanguageSupportCubit, List<LanguageSupport>>( | ||
builder: (context, listLanguage) { | ||
if (listLanguage.isEmpty) { | ||
return Container(); | ||
} | ||
return ListView.builder( | ||
itemBuilder: (context, index) { | ||
var dataLanguage = listLanguage[index]; | ||
return ChildLanguage(dataLanguage); | ||
}, | ||
itemCount: listLanguage.length, | ||
); | ||
}, | ||
), | ||
bottomNavigationBar: ElevatedButton( | ||
onPressed: () { | ||
context.read<LanguageSettingCubit>().saveLanguage(); | ||
Navigator.pop(context); | ||
}, | ||
style: ButtonStyle( | ||
fixedSize: WidgetStateProperty.all(const Size.fromHeight(70)), | ||
shape: WidgetStateProperty.all(const RoundedRectangleBorder( | ||
borderRadius: BorderRadius.zero)), | ||
backgroundColor: WidgetStateProperty.all(Colors.green)), | ||
child: const Text('OK', | ||
style: TextStyle( | ||
color: Colors.white, | ||
fontWeight: FontWeight.bold, | ||
fontSize: 25)), | ||
), | ||
); | ||
} | ||
|
||
class ChildLanguage extends StatelessWidget { | ||
final LanguageSupport dataLanguage; | ||
const ChildLanguage(this.dataLanguage, {super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return GestureDetector( | ||
behavior: HitTestBehavior.translucent, | ||
onTap: () => _chooseLanguage(dataLanguage, context), | ||
child: BlocBuilder<LanguageSettingCubit, String>( | ||
builder: (context, state) { | ||
return RadioListTile<LanguageSupport>( | ||
value: dataLanguage, | ||
onChanged: (choose) => _chooseLanguage(dataLanguage, context), | ||
title: Row( | ||
children: [ | ||
Text(dataLanguage.nativeName ?? ''), | ||
const SizedBox( | ||
width: 10, | ||
), | ||
Text(dataLanguage.flag ?? '', | ||
style: const TextStyle(fontSize: 24)) | ||
], | ||
), | ||
groupValue: LanguageSupport(code: state), | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
|
||
_chooseLanguage(LanguageSupport dataLanguage, BuildContext context) { | ||
context.read<LanguageSettingCubit>().chooseLanguage(dataLanguage); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...features/language_setting/ui/widgets/button_language_setting/button_language_setting.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:public_chat/features/language_setting/bloc/user_language_cubit.dart'; | ||
import 'package:public_chat/features/language_setting/constants.dart'; | ||
import 'package:public_chat/features/language_support/data/language.dart'; | ||
|
||
import '../../language_setting_screen.dart'; | ||
|
||
class ButtonLanguageSetting extends StatelessWidget { | ||
const ButtonLanguageSetting({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return GestureDetector( | ||
behavior: HitTestBehavior.translucent, | ||
onTap: () { | ||
Navigator.push( | ||
context, | ||
MaterialPageRoute( | ||
builder: (context) => const SettingLanguageScreen(), | ||
)); | ||
}, | ||
child: Padding( | ||
padding: const EdgeInsets.only(right: 10.0), | ||
child: BlocBuilder<UserLanguageCubit, String>( | ||
builder: (context, state) { | ||
LanguageSupport languageUser = context.languageUserObject(state); | ||
return Text( | ||
languageUser.flag ?? '', | ||
style: const TextStyle(fontSize: 20), | ||
); | ||
}, | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"login":"Login", | ||
"publicRoomTitle": "Public Room" | ||
"publicRoomTitle": "Public Room", | ||
"settingLanguage": "Setting Language" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"login":"Đăng nhập", | ||
"publicRoomTitle": "Phòng chat công cộng" | ||
|
||
"publicRoomTitle": "Phòng chat công cộng", | ||
"settingLanguage": "Cài đặt ngôn ngữ" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.