我正在尝试使用Flutter制作表单,让用户在提交后可以更改答案。表单在TextField
中包含两个AnimatedSwitcher
。当用户单击“发送”按钮时,切换器被告知将TextField
替换为Text
消息,表示'Successfully submitted! (you can still change your answer)'
我再次单击该按钮,我可以正常看到表格,但是当我专注于表单时,我会短暂看到以下错误。
The following assertion was thrown building _ScrollableScope:
'package:flutter/src/rendering/editable.dart': Failed assertion: line 599 pos 14:
'_listenerAttached': is not true.
代码:
import 'dart:core';
import 'package:flutter/material.dart';
class EditableFocus extends StatefulWidget {
@override
_State createState() {
return _State();
}
}
class _State extends State<EditableFocus> with TickerProviderStateMixin {
FocusNode focusNode1 = FocusNode();
FocusNode focusNode2 = FocusNode();
bool success = false;
TextEditingController controller1 = TextEditingController();
TextEditingController controller2 = TextEditingController();
final GlobalKey<FormState> _formKeyOne = GlobalKey<FormState>();
final GlobalKey<FormState> _formKeyTwo = GlobalKey<FormState>();
@override
void initState() {
super.initState();
focusNode1.addListener(_focusListner);
focusNode2.addListener(_focusListner);
controller1.addListener(_changeListner);
controller2.addListener(_changeListner);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0.5,
title: Text(
"Testing views",
style: Theme.of(context).textTheme.title,
),
),
body: _builtLayoutBuilder(),
);
}
Widget _builtLayoutBuilder() {
return Column(
children: <Widget>[
AnimatedSwitcher(
duration: Duration(milliseconds: 200),
child: _buildChild(),
),
RaisedButton(
child: Text('Submit/unsubmitted'),
onPressed: () {
setState(() {
success = !success;
});
})
],
);
}
void _focusListner() {
if (focusNode1.hasFocus) {
print('Focus on 1');
} else if (focusNode2.hasFocus) {
print('Focus on 2');
}
}
void _changeListner() {
if (focusNode1.hasFocus) {
print(controller1.text);
} else if (focusNode2.hasFocus) {
print(controller2.text);
}
}
Widget _buildChild() {
if (success) {
return Text('Successfully submitted! (you can still change your answer)');
}
return Column(
children: <Widget>[
TextFormField(
key: _formKeyOne,
focusNode: focusNode1,
controller: controller1,
style: TextStyle(fontSize: 16.0, color: Colors.black),
decoration: InputDecoration(
hintText: 'Write some text', border: InputBorder.none),
// onChanged: onSearchTextChanged,
),
TextFormField(
key: _formKeyTwo,
focusNode: focusNode2,
controller: controller2,
style: TextStyle(fontSize: 16.0, color: Colors.black),
decoration: InputDecoration(
hintText: 'Write some text', border: InputBorder.none),
// onChanged: onSearchTextChanged,
),
],
);
}
}