Skip to content

Commit

Permalink
Improve the LanguageBuilder behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
lamnhan066 committed Jan 16, 2024
1 parent 34f60f0 commit e022a10
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
25 changes: 9 additions & 16 deletions lib/src/language_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,30 @@ class _LanguageBuilderState extends State<LanguageBuilder> with UpdateLanguage {
}

@override
void didChangeDependencies() {
void initState() {
final getRoot = _of(
context,
widget.forceRebuild == true ||
(widget.forceRebuild == null && _languageHelper._forceRebuild),
);

if (getRoot == null) {
printDebug(
'Cannot find the root context of this context. Add $this to states');
_languageHelper._states.add(this);

// Because the Widget trees are built from a higher level to a lower level,
// so all the posible `root` widgets have definitely been added to the list
// of the states. So this code is redundant.
//
// } else if (!_languageHelper._states.contains(getRoot)) {
// printDebug('Added root context $getRoot to LanguageHelper states');
// _languageHelper._states.add(getRoot);
// Because the Widget trees are built from a higher level to a lower level,
// so all the posible `root` widgets have definitely been added to the list
// of the states. So we just need to add the state that its' parent is null.
if (getRoot == null && _languageHelper._states.add(this)) {
printDebug('Added $this to the states');
} else {
printDebug('This root context $this was already contained in states');
printDebug('$this was already contained in the states');
}

printDebug('Length of the states: ${_languageHelper._states.length}');

super.didChangeDependencies();
super.initState();
}

@override
void dispose() {
printDebug('Removed $this from LanguageHelper states');
printDebug('Removed $this from the states');
_languageHelper._states.remove(this);
super.dispose();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/language_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LanguageHelper {
static LanguageHelper instance = LanguageHelper._();

/// To control [LanguageBuilder]
final List<UpdateLanguage> _states = [];
final Set<UpdateLanguage> _states = {};

/// Private instance
LanguageHelper._();
Expand Down

0 comments on commit e022a10

Please sign in to comment.