Флаттер: как редактировать фрагмент текста с помощью TextField - PullRequest
0 голосов
/ 02 мая 2020

Итак, у меня есть TextField, который я использую, чтобы добавить строку «String» в базу данных, а также отредактировать ее позже. Проблема в том, что я не могу понять, каким образом текст уже напечатан на экране и готов к редактированию ко времени открытия новой страницы.

Я нашел этот пост здесь, в котором говорится об использовании контроллера TextField, но я не смог найти четкого ответа: Текстовое поле не редактирует текст во флаттере Любой совет?

enum NoteMode { Editing, Adding }
class EmptyTextFile extends StatefulWidget {
  static const id = "empty_text_file";
  final NoteMode _noteMode;
  final String noteName;
  final String noteText;
  EmptyTextFile(this._noteMode, [this.noteName, this.noteText]);
  EmptyTextFile.toEdit(this._noteMode, this.noteName, this.noteText);

  @override
  _EmptyTextFileState createState() => _EmptyTextFileState();
}

class _EmptyTextFileState extends State<EmptyTextFile> {
  SQLDataBaseHelper helper = SQLDataBaseHelper();

  String newNoteName;
  String newNoteText;

  TextEditingController textEditorText;

  @override
  void initState() {
    newNoteName = widget.noteName;
    newNoteText = widget.noteText;
    // textEditorText.value = textEditorText.value.copyWith(text: newNoteText);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: widget._noteMode == NoteMode.Editing
            ? Text('Edit Note')
            : Text('Add note'), 
        actions: <Widget>[
          FlatButton.icon(
            icon: Icon(
              Icons.save,
            ),
            label: Text('save'),
            onPressed: () {
              if (widget._noteMode == NoteMode.Adding) {
                helper.insertNote(Note(newNoteName, newNoteText, 0));
                Navigator.pop(context);
              } else {
                helper.updateNote(Note(newNoteName, newNoteText, 0));
                Navigator.pop(context);
              }
            },
          ),
        ],
      ),
      body: Column(
        children: <Widget>[
          Padding(
            padding:
                const EdgeInsets.symmetric(horizontal: 20.0, vertical: 15.0),
            child: TextField(
              textAlign: TextAlign.center,
              decoration: InputDecoration(
                hintText: 'Title',
                border: UnderlineInputBorder(
                    borderSide: BorderSide(
                  color: Theme.of(context).primaryColor,
                )),
              ),
              onChanged: (value) => newNoteName = value,
            ),
          ),
          TextField(
              maxLines: null,
              decoration: InputDecoration(
                hintText: 'Note',
                //focusedBorder: InputBorder.none,
                border: InputBorder.none,
              ),
              onChanged: (value) {
                setState(() {
                  //textEditorText.text = value;
                  newNoteName = value;
                  newNoteText = value;
                });
              }),
        ],
      ),
    );
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...