TextField
に値を設定するにはTextEditingController
を使います。
final controller = TextEditingController();
controller.text = 'xxx';
TextField(
controller: controller,
);
TextField
の値を保存する場合は、onChanged
を使います。onChanged
は変更されたら毎回実行されます。
TextField(
onChanged: (text) {
},
);
上記を利用して、動くコードを書いてみたので、参考にしてください(変更点は質問のコードと差分をとるとわかると思います)
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'tlmemo',
theme: ThemeData(),
home: AppMain(title: 'tlmemo'),
);
}
}
class AppMain extends StatefulWidget {
AppMain({Key key, this.title}) : super(key: key);
final String title;
@override
_AppMainState createState() => _AppMainState();
}
class _AppMainState extends State<AppMain> {
//データ編集用のコントローラ
final redController = TextEditingController();
final yellowController = TextEditingController();
final greenController = TextEditingController();
@override
void initState() {
super.initState();
load();
}
//データロード用関数*3(loadRed, loadYellow, loadGreen)
Future<void> load() async {
final prefs = await SharedPreferences.getInstance();
redController.text = prefs.getString('red');
yellowController.text = prefs.getString('yellow');
greenController.text = prefs.getString('green');
}
//データ保存用関数*3(saveRed, saveYellow, saveGreen)
Future<void> save(key, text) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(key, text);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Text(''),
Text(''),
Text('tlmemo'),
Text(''),
//赤色の部分:緊急/ASAP
TextField(
controller: redController,
onChanged: (text) {
save('red', text);
},
minLines: 3,
maxLines: 3,
maxLength: 150,
maxLengthEnforced: true,
decoration: InputDecoration(
fillColor: Colors.red[200],
filled: true,
border: OutlineInputBorder(),
hintText: '緊急/ASAP',
)),
//黄色の部分:重要/Important
TextField(
controller: yellowController,
onChanged: (text) {
save('yellow', text);
},
minLines: 5,
maxLines: 5,
maxLength: 250,
maxLengthEnforced: true,
decoration: InputDecoration(
fillColor: Colors.yellow[300],
filled: true,
border: OutlineInputBorder(),
hintText: '重要/Important',
)),
//緑色の部分:その他/Other
TextField(
controller: greenController,
onChanged: (text) {
save('green', text);
},
minLines: 7,
maxLines: 7,
maxLength: 350,
maxLengthEnforced: true,
decoration: InputDecoration(
fillColor: Colors.green[300],
filled: true,
border: OutlineInputBorder(),
hintText: 'その他/Other',
)),
Text(''),
Text(''),
],
),
));
}
}
おまけ
上記のコードだと、SharedPreferencesからデータがロードされる前に、画面が描画されて、入力欄がチラつくと思います。ロードの完了isLoaded
までVisibility
を使って非表示にする対応をしたコードも載せておきます。
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'tlmemo',
theme: ThemeData(),
home: AppMain(title: 'tlmemo'),
);
}
}
class AppMain extends StatefulWidget {
AppMain({Key key, this.title}) : super(key: key);
final String title;
@override
_AppMainState createState() => _AppMainState();
}
class _AppMainState extends State<AppMain> {
//データ編集用のコントローラ
final redController = TextEditingController();
final yellowController = TextEditingController();
final greenController = TextEditingController();
var isLoaded = false;
@override
void initState() {
super.initState();
load();
}
//データロード用関数*3(loadRed, loadYellow, loadGreen)
Future<void> load() async {
final prefs = await SharedPreferences.getInstance();
redController.text = prefs.getString('red');
yellowController.text = prefs.getString('yellow');
greenController.text = prefs.getString('green');
setState(() {
isLoaded = true;
});
}
//データ保存用関数*3(saveRed, saveYellow, saveGreen)
Future<void> save(key, text) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(key, text);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Visibility(
visible: isLoaded,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Text(''),
Text(''),
Text('tlmemo'),
Text(''),
//赤色の部分:緊急/ASAP
TextField(
controller: redController,
onChanged: (text) {
save('red', text);
},
minLines: 3,
maxLines: 3,
maxLength: 150,
maxLengthEnforced: true,
decoration: InputDecoration(
fillColor: Colors.red[200],
filled: true,
border: OutlineInputBorder(),
hintText: '緊急/ASAP',
)),
//黄色の部分:重要/Important
TextField(
controller: yellowController,
onChanged: (text) {
save('yellow', text);
},
minLines: 5,
maxLines: 5,
maxLength: 250,
maxLengthEnforced: true,
decoration: InputDecoration(
fillColor: Colors.yellow[300],
filled: true,
border: OutlineInputBorder(),
hintText: '重要/Important',
)),
//緑色の部分:その他/Other
TextField(
controller: greenController,
onChanged: (text) {
save('green', text);
},
minLines: 7,
maxLines: 7,
maxLength: 350,
maxLengthEnforced: true,
decoration: InputDecoration(
fillColor: Colors.green[300],
filled: true,
border: OutlineInputBorder(),
hintText: 'その他/Other',
)),
Text(''),
Text(''),
],
),
),
));
}
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/29 08:04