我想将数据从一个屏幕传递到另一个屏幕,然后将数据保存到ListView / ListTiles。我没有得到 预期结果。在代码中,单击“保存”按钮图标没有任何反应...我能够将数据传递到所需的屏幕,但是单击“保存”图标时,它必须保存在主页上的ListView构建器中,然后单击主页上的ListTile即可打开。我正在使用路径提供程序,但无法实现输出。如果有人可以帮助我,我将非常感激。请仔细阅读以下三节课,如果您能告诉我们代码中有什么错误,我将不胜感激。 目的: 我可以使用data.addContent(_textEditingController.text)在“内容”字段中获取文本/数据。但是在OnPressed的“保存图标”按钮中,我尝试打印(内容)..控制台中什么都没有显示。.如果存在内容,则单击“保存图标”按钮后它将保存并应保存到主屏幕。
class Data extends ChangeNotifier {
String title = '';
String content = '';
Map<String, List<String>> notes = {
'titles': [],
'contents': [],
'timeSnapShots': [],
};
// void addTitle(String value) => title = value;
void addContent(String value) {
content = value;
print('here the messages');
print(content);
notifyListeners();
}
/// Create a new note by the given title and content.
void addNote() {
var now = DateTime.now();
String hours = now.hour < 10 ? '0${now.hour}' : '${now.hour}';
String minutes = now.minute < 10 ? '0${now.minute}' : '${now.minute}';
String currentTime = '$hours:$minutes';
// notes['titles'].add(title);
notes['contents'].add(content);
notes['timeSnapShots'].add(currentTime);
title = '';
content=content;
notifyListeners();
writeFile();
}
void removeNote(int index) {
// notes['titles'].remove(notes['titles'][index]);
notes['contents'].remove(notes['contents'][index]);
notes['timeSnapShots'].remove(notes['timeSnapShots'][index]);
notifyListeners();
writeFile();
}
Future<String> appPath() async {
final document = await getApplicationDocumentsDirectory();
return document.path;
}
Future<File> appFile() async {
final path = await appPath();
return File('$path/data.txt');
}
Future writeFile() async {
final file = await appFile();
/// The vertical bar help us split data when reading the file
Future saveData = file.writeAsString(
'${notes['titles']}|${notes['contents']}|${notes['timeSnapShots']}');
return saveData;
}
Future readFile() async {
final file = await appFile();
List data = (await file.readAsString()).split('|');
/// I called the (replaceAll) here because when we save list as
/// string the '[' and ']' of lists also saved with it.
notes['titles'] =
data[0].replaceAll('[', '').replaceAll(']', '').split(',');
notes['contents'] =
data[1].replaceAll('[', '').replaceAll(']', '').split(',');
notes['timeSnapShots'] =
data[2].replaceAll('[', '').replaceAll(']', '').split(',');
notifyListeners();
}
}
class AddingTextField extends StatefulWidget {
final int maxLines;
final String hintText;
final String text;
AddingTextField({this.maxLines,this.hintText, this.text});
@override
_AddingTextFieldState createState() => _AddingTextFieldState();
}
class _AddingTextFieldState extends State<AddingTextField> {
TextEditingController _textEditingController;
@override
void initState() {
_textEditingController = TextEditingController();
_textEditingController.text = widget.text;
super.initState();
}
@override
Widget build(BuildContext context) {
var data = context.watch<Data>();
return Padding(
padding: const EdgeInsets.all(15),
child: TextFormField(
controller: _textEditingController,
maxLines: widget.maxLines,
decoration: InputDecoration(hintText: widget.hintText),
onFieldSubmitted: (input) {
if(input != null) {
if(widget.hintText == 'Title'){
// data.addTitle(input);
print(input);
}
else
// data.addContent(input);
data.addContent(_textEditingController.text);
// data.addContent(input);
}
},
),
);
}
}
class AddNoteScreen extends StatelessWidget {
final String text;
AddNoteScreen({this.text});
@override
Widget build(BuildContext context) {
var data = context.watch<Data>();
return Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
CustomAppBar(
title: 'Add Note',
icon: Icons.save,
onPressed: () {
// data.addNote();
// Navigator.push(context);
// print(data.title);
print(data.content);
// if (data.title != '' && data.content != '')
if (data.content != ''&& data.content != null)
{
data.addNote();
// Navigator.pop(context);
}
},
),
// AddingTextField(maxLines: 1, hintText: 'Title'),
Flexible(child: AddingTextField(maxLines: 500, text: text)),
],
),
),
);
}
}