启用它后,TextFormField未聚焦 [英] TextFormField not focused after enabling it
问题描述
如果启用并聚焦先前禁用的TextFormField,则看不到闪烁的光标.在此示例中,如 _enabled
状态变量所示,最初禁用了TextFormField,并且当您单击启用按钮时,该字段已启用并处于焦点状态,但闪烁的光标不可见.我必须单击TextFormField才能看到闪烁的光标.
I do not see a blinking cursor if I enable and focus a previous disabled TextFormField. In this example, the TextFormField is originally disabled, as indicated by the _enabled
state variable, and when you click on the enable button, the field is enabled and focused but the blinking cursor is not visible. I have to click on the TextFormField in order to see the blinking cursor.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _enabled = false;
FocusNode focusNodeA = FocusNode();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Test'),
),
body: TextFormField(
enabled: _enabled,
focusNode: focusNodeA,
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() => _enabled = true);
FocusScope.of(context).requestFocus(focusNodeA);
},
child: Text('Enable'),
),
);
}
}
如果我按两次启用",则会显示光标.
If I press 'Enable' twice, the cursor is shown.
如果已经启用TextFormField然后将其聚焦,则闪烁的光标可见.
If the TextFormField was already enabled and then focused, the blinking cursor is visible.
推荐答案
1-您将焦点放在setState之后,因此它不起作用.就像下面一样2-在禁用窗口小部件和启用窗口小部件之前,您无法集中显示窗口小部件.当您在ui踏步中同时进行聚焦和启用时,由于它们的渲染时间,请尝试在启用之前进行聚焦.如果您延迟聚焦时间来解决问题,就可以解决.
1- you focused after setState so it not working. just do like below 2- you can't focus widget until disabled and when you enabling widget. when you do focusing and enabling at same time in ui tread it's try focusing before enabling because of their rendering time.if you post some delay to focusing the problem get solving.
setState(() {
Future.delayed(const Duration(milliseconds: 10), ()
{
FocusScope
.of(
context)
.requestFocus(
focusNodeA);
print(FocusScope.of(context).focusedChild.toString());
});
_enabled = true;
});
这篇关于启用它后,TextFormField未聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!