我需要偶尔禁用TextFormField。我无法在窗口小部件中找到标志,或者只是将控制器设置为只读或禁用。 做什么是最好的方法?禁用文本编辑字段晃动
2
A
回答
4
这不是框架当前提供的功能,但您可以使用FocusScope
防止TextFormField
请求关注。
下面是禁用时的样子。
这里是什么样子时的启用它像。
代码这是以下:
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new HomePage(),
));
}
class HomePage extends StatefulWidget {
HomePageState createState() => new HomePageState();
}
class HomePageState extends State<HomePage> {
TextEditingController _controller = new TextEditingController();
bool _enabled = false;
@override
Widget build(BuildContext context) {
ThemeData theme = Theme.of(context);
return new Scaffold(
appBar: new AppBar(
title: new Text('Disabled Text'),
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.free_breakfast),
onPressed:() {
setState(() {
_enabled = !_enabled;
});
}
),
body: new Center(
child: new Container(
margin: const EdgeInsets.all(10.0),
child: _enabled ?
new TextFormField(controller: _controller) :
new FocusScope(
node: new FocusScopeNode(),
child: new TextFormField(
controller: _controller,
style: theme.textTheme.subhead.copyWith(
color: theme.disabledColor,
),
decoration: new InputDecoration(
hintText: _enabled ? 'Type something' : 'You cannot focus me',
),
),
),
),
),
);
}
}
1
还有另一种方法可以做到这一点也不会引起this issue。希望这可能有助于某人。
创建AlwaysDisabledFocusNode
并将其传递给focusNode
属性TextField
。
class AlwaysDisabledFocusNode extends FocusNode {
@override
bool get hasFocus => false;
}
然后
new TextField(
focusNode: new AlwaysDisabledFocusNode(),
...
...
)
相关问题
- 1. 可编辑文本字段
- 2. 多编辑文本字段
- 3. 禁用文本编辑UIActivityViewController
- 4. 禁用在Sitecore页面编辑器中的文本字段编辑
- 5. 在Android中禁用编辑文本字段30秒
- 6. 禁用文本字段编辑而不阻止清除按钮
- 7. 编辑文本字段为空时禁用按钮
- 8. 禁用启用文本字段动态
- 9. 启用禁用动态文本字段
- 10. 输入视图设置为文本字段时禁用文本编辑
- 11. Php文本字段编辑文件
- 12. 文本字段使可编辑,不可编辑使用jquery
- 13. 如果处于编辑模式,OpenERP禁用字段编辑
- 14. 在Selenium中编辑文本字段
- 15. (html)wysiwyg编辑文本字段在phpmyadmin
- 16. 制作文本编辑字段
- 17. XNA可编辑文本字段
- 18. Django可编辑文本字段
- 19. 在extjs中编辑文本字段
- 20. Tapestry5不可编辑的文本字段
- 21. Android设备上编辑文本字段
- 22. 带有文本编辑字段的UIView
- 23. 自动移动到下一个编辑文本字段android
- 24. 禁用编辑文本的Swype键盘
- 25. 在Django中禁止编辑字段
- 26. 使用jquery禁用文本字段中的文本拖动
- 27. 手动编辑字段后的javascript编辑字段
- 28. 编辑配置文件joomla中禁用的字段名称
- 29. 禁用手动编辑application.exe.config文件
- 30. 不同酥料饼的编辑和非编辑文本字段
提起https://github.com/flutter/flutter/issues/10645 –