Skip to content

Commit 6a97efb

Browse files
committed
refactor(app_configuration): improve text controller initialization and updates
- Extract common controller initialization logic into separate methods - Implement proper cursor positioning when updating text controllers - Avoid unnecessary text updates by comparing current and new values - Apply refactoring to both AppUrlsForm and UpdateConfigForm widgets
1 parent eda3c0d commit 6a97efb

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

lib/app_configuration/widgets/app_urls_form.dart

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,37 @@ class _AppUrlsFormState extends State<AppUrlsForm> {
3434
@override
3535
void initState() {
3636
super.initState();
37-
_termsUrlController = TextEditingController(
38-
text: widget.remoteConfig.app.general.termsOfServiceUrl,
39-
);
40-
_privacyUrlController = TextEditingController(
41-
text: widget.remoteConfig.app.general.privacyPolicyUrl,
42-
);
37+
final generalConfig = widget.remoteConfig.app.general;
38+
_termsUrlController = _createController(generalConfig.termsOfServiceUrl);
39+
_privacyUrlController = _createController(generalConfig.privacyPolicyUrl);
40+
}
41+
42+
TextEditingController _createController(String text) {
43+
return TextEditingController(text: text)
44+
..selection = TextSelection.collapsed(offset: text.length);
45+
}
46+
47+
void _updateControllerText(TextEditingController controller, String text) {
48+
if (controller.text != text) {
49+
controller
50+
..text = text
51+
..selection = TextSelection.collapsed(offset: text.length);
52+
}
4353
}
4454

4555
@override
4656
void didUpdateWidget(covariant AppUrlsForm oldWidget) {
4757
super.didUpdateWidget(oldWidget);
4858
if (widget.remoteConfig.app.general != oldWidget.remoteConfig.app.general) {
49-
_termsUrlController.text =
50-
widget.remoteConfig.app.general.termsOfServiceUrl;
51-
_privacyUrlController.text =
52-
widget.remoteConfig.app.general.privacyPolicyUrl;
59+
final generalConfig = widget.remoteConfig.app.general;
60+
_updateControllerText(
61+
_termsUrlController,
62+
generalConfig.termsOfServiceUrl,
63+
);
64+
_updateControllerText(
65+
_privacyUrlController,
66+
generalConfig.privacyPolicyUrl,
67+
);
5368
}
5469
}
5570

lib/app_configuration/widgets/update_config_form.dart

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,38 @@ class _UpdateConfigFormState extends State<UpdateConfigForm> {
3434
void initState() {
3535
super.initState();
3636
final updateConfig = widget.remoteConfig.app.update;
37-
_latestVersionController = TextEditingController(
38-
text: updateConfig.latestAppVersion,
39-
);
40-
_iosUrlController = TextEditingController(text: updateConfig.iosUpdateUrl);
41-
_androidUrlController = TextEditingController(
42-
text: updateConfig.androidUpdateUrl,
43-
);
37+
_latestVersionController = _createController(updateConfig.latestAppVersion);
38+
_iosUrlController = _createController(updateConfig.iosUpdateUrl);
39+
_androidUrlController = _createController(updateConfig.androidUpdateUrl);
40+
}
41+
42+
TextEditingController _createController(String text) {
43+
return TextEditingController(text: text)
44+
..selection = TextSelection.collapsed(offset: text.length);
45+
}
46+
47+
void _updateControllerText(TextEditingController controller, String text) {
48+
if (controller.text != text) {
49+
controller
50+
..text = text
51+
..selection = TextSelection.collapsed(offset: text.length);
52+
}
4453
}
4554

4655
@override
4756
void didUpdateWidget(covariant UpdateConfigForm oldWidget) {
4857
super.didUpdateWidget(oldWidget);
4958
if (widget.remoteConfig.app.update != oldWidget.remoteConfig.app.update) {
5059
final updateConfig = widget.remoteConfig.app.update;
51-
_latestVersionController.text = updateConfig.latestAppVersion;
52-
_iosUrlController.text = updateConfig.iosUpdateUrl;
53-
_androidUrlController.text = updateConfig.androidUpdateUrl;
60+
_updateControllerText(
61+
_latestVersionController,
62+
updateConfig.latestAppVersion,
63+
);
64+
_updateControllerText(_iosUrlController, updateConfig.iosUpdateUrl);
65+
_updateControllerText(
66+
_androidUrlController,
67+
updateConfig.androidUpdateUrl,
68+
);
5469
}
5570
}
5671

0 commit comments

Comments
 (0)