【问题标题】:Keep the value of TextFromField between pages在页面之间保持 TextFromField 的值
【发布时间】:2019-11-25 11:27:53
【问题描述】:
作为一个练习,我试图用颤振复制一个应用程序,但我遇到了 TextFromField 的问题。
基本上我应该能够在 TextFromField 中写一个文本,而不是选择颜色(和其他东西..)。要选择颜色,我将打开一个 ShowDialog,选择颜色,然后返回上一页。问题是当我返回页面时,我丢失了 TextFromField 的值,因为基本上我正在重新加载页面。
我需要做到这一点:
正如您在原始应用程序中看到的那样,当我选择颜色时,文本“TEST”仍然存在!我是 Flutter 的新手,我真的不知道该怎么做 :( 帮助。
【问题讨论】:
标签:
android-studio
flutter
dart
textfield
【解决方案1】:
TextFormField 不会为您记住该值,您必须记住该值并将其提供给TextFormField 的控制器。您需要计划一个state management 方法来实现这一点。可以有多种方法来实现这一点。发布您的代码有助于获得实际建议。
【解决方案2】:
将您在 textController 中为该 TextFormField 获得的 VALUE 存储在 Key-Value 存储中。
在开始之前,将 shared_preferences 插件添加到 pubspec.yaml 文件中:
content_copy
dependencies:
flutter:
sdk: flutter
shared_preferences: "<newest version>"
然后像下面的例子那样存储
// obtain shared preferences
final prefs = await SharedPreferences.getInstance();
// set value
prefs.setInt(txtFmFld, VALUE);
它将跨页面持久化