On my one page I would like my user to start with default text on one variable ( codeDialog) and then I want them to change that text, after that time that text they wrote would be my new default text. Sadly I couldn't get it to work.
Right now when I restart the app and open that screen it resets to null. I think its because of initializing variable ( which is null) but I couldn't find a way to fix that. Any idea?
Thanks.
All of this is inside statefulwidget's state class:
void initState() {
super.initState();
print(codeDialog);
isFirstTime().then((isFirstTime) {
isFirstTime ? defaultText() : readFromShared();
});
setText();
print(codeDialog);
_textEditingController = new TextEditingController(text: codeDialog);
}
Future<bool> isFirstTime() async {
final prefs = await SharedPreferences.getInstance();
var isFirstTime = prefs.getBool('first_time');
if (isFirstTime != null && !isFirstTime) {
prefs.setBool('first_time', false);
return false;
} else {
prefs.setBool('first_time', false);
return true;
}
}
static String codeDialog;
void defaultText(){
print("first time");
codeDialog="This is what i want my default variable to be when my app first opened.";
}
void readFromShared() async{
print("not first");
final prefs = await SharedPreferences.getInstance();
codeDialog = prefs.getString('sharedmessage');
}
void setText() async {
final prefs = await SharedPreferences.getInstance();
prefs.setString('sharedmessage', codeDialog);
}
and then i have AlertDialog with this it works and saves the data.
AlertDialog(
scrollable: true,
contentPadding: EdgeInsets.fromLTRB(20, 20, 20, 0),
content: Column(
children: [
Container(
height: maxLines * 10.0,
child: TextField(
controller: new TextEditingController(text: codeDialog),//_textEditingController,
maxLines: maxLines,
onChanged: (value){
setState(() {
valueText=value;
});
},
decoration: InputDecoration(
hintText: "Text here",
),
),
),
],
),
actions: [
TextButton(
onPressed: Navigator.of(context).pop, child: Text("Cancel")),
TextButton(
onPressed: () {
setState(() {
Navigator.pop(context);
codeDialog = valueText;
print(codeDialog);
setText();
});
},
child: Text("Save"))
],
)
EDIT: Also worth noting to people who may see this question. If you don't change TextField's value and submit it (After app restart for some reason) the value will change to null (i think due to there's no change in string). You can fix it by adding if(value==null) and saving it if its not null.