wt1.1a flutter_bloc
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:aitrainer_app/localization/app_language.dart';
|
||||
import 'package:aitrainer_app/localization/app_localization.dart';
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
part 'settings_event.dart';
|
||||
part 'settings_state.dart';
|
||||
|
||||
class SettingsBloc extends Bloc<SettingsEvent, SettingsState> {
|
||||
String language;
|
||||
Locale _locale;
|
||||
BuildContext context;
|
||||
|
||||
SettingsBloc({this.context}) : super(SettingsInitial());
|
||||
|
||||
@override
|
||||
Stream<SettingsState> mapEventToState(
|
||||
SettingsEvent event,
|
||||
) async* {
|
||||
if (event is SettingsChangeLanguage) {
|
||||
yield SettingsLoading();
|
||||
await _changeLang( event.language);
|
||||
yield SettingsReady(_locale);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _changeLang( String lang ) async{
|
||||
|
||||
switch ( lang ) {
|
||||
case "English":
|
||||
case "Angol":
|
||||
_locale = Locale('en');
|
||||
break;
|
||||
case "Hungarian":
|
||||
case "Magyar":
|
||||
_locale = Locale('hu');
|
||||
break;
|
||||
}
|
||||
this.language = lang;
|
||||
await loadLang();
|
||||
}
|
||||
|
||||
Future<void> loadLang() async{
|
||||
final AppLanguage appLanguage = AppLanguage();
|
||||
appLanguage.changeLanguage(_locale);
|
||||
if ( context != null ) {
|
||||
AppLocalizations.of(context).setLocale(_locale);
|
||||
await AppLocalizations.of(context).load();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
part of 'settings_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class SettingsEvent extends Equatable {
|
||||
const SettingsEvent();
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class SettingsChangeLanguage extends SettingsEvent {
|
||||
final String language;
|
||||
const SettingsChangeLanguage({this.language});
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
part of 'settings_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class SettingsState extends Equatable {
|
||||
const SettingsState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
// ignore: must_be_immutable
|
||||
class SettingsInitial extends SettingsState {
|
||||
Locale locale;
|
||||
SettingsInitial();
|
||||
|
||||
setLocale(locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [locale];
|
||||
}
|
||||
|
||||
class SettingsLoading extends SettingsState {
|
||||
final Locale locale;
|
||||
const SettingsLoading({this.locale});
|
||||
|
||||
@override
|
||||
List<Object> get props => [locale];
|
||||
}
|
||||
|
||||
class SettingsReady extends SettingsState {
|
||||
final Locale locale;
|
||||
const SettingsReady(this.locale);
|
||||
|
||||
@override
|
||||
List<Object> get props => [locale];
|
||||
}
|
||||
|
||||
class SettingsError extends SettingsState {
|
||||
final String message;
|
||||
const SettingsError(this.message);
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
||||
Reference in New Issue
Block a user