簡體   English   中英

我想在顫振中將數據從一個屏幕傳遞到另一個屏幕並將數據保存到 ListView/ListTiles

[英]I want to pass data from one screen to another screen in flutter and save the data to ListView/ListTiles

我想在顫振中將數據從一個屏幕傳遞到另一個屏幕並將數據保存到 ListView/ListTiles。 我沒有得到預期的結果。 在代碼中,點擊保存按鈕圖標沒有任何反應......我能夠將數據傳遞到所需的屏幕,但點擊保存圖標,它必須保存在主頁上的 ListView 構建器中,點擊主頁上的 ListTile 這個屏幕有打開..我正在使用路徑提供程序但無法實現輸出..如果有人可以提供幫助,我將不勝感激。 請完成以下三個課程,如果您能說出代碼中的錯誤,我將不勝感激。 目標:我能夠使用 data.addContent(_textEditingController.text) 在 Content 字段中獲取文本/數據。 但是在 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)),
        ],
      ),
    ),
  );
}
}

您正在尋找的稱為序列化

這里跟進

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM