Flutter 颤振v1.12.13+;热修复程序.5 FocusNode意外行为

Flutter 颤振v1.12.13+;热修复程序.5 FocusNode意外行为,flutter,flutter-dependencies,Flutter,Flutter Dependencies,我将焦点节点添加到textformfield。在我将flutter版本升级到v1.12.13+热修复程序.5之后,flatterfocusnode出现了意想不到的异常 @override void initState() { super.initState(); focusNode.addListener(() async { if(focusNode.hasFocus) { Navigator.push( context, MaterialPageRoute(

我将焦点节点添加到textformfield。在我将flutter版本升级到v1.12.13+热修复程序.5之后,flatterfocusnode出现了意想不到的异常

  @override
void initState() {
super.initState();

focusNode.addListener(() async {
  if(focusNode.hasFocus)
    {
        Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
    }

    }

}


TextFormField(
  style: TextStyle(color: Theme.of(context).brightness==Brightness.dark?Colors.white:Colors.grey),
  readOnly: true,
  validator: validateField,
  focusNode: focusNode,
  controller: controller,

);
我单击textformfield打开第二页。但当我关闭第二页时,第二页会自动打开。此行为发生在我将版本升级到v1.12.13+修补程序之后。5


v1.12.13+修补程序5中focusNode的真正用法是什么?

您可以复制下面的粘贴运行完整代码
因为从
SecondRoute
返回时
TextFormField
仍有
foucs
,所以需要执行
unfocus

代码片段

focusNode.addListener(() async {
      if (focusNode.hasFocus) {
        await Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => SecondRoute()),
        );
        FocusScope.of(context).requestFocus(new FocusNode());
      }
    });
工作演示

完整代码

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(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  final controller = TextEditingController();
  FocusNode focusNode = new FocusNode();

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  void initState() {
    super.initState();

    focusNode.addListener(() async {
      if (focusNode.hasFocus) {
        await Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => SecondRoute()),
        );
        FocusScope.of(context).requestFocus(new FocusNode());
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextFormField(
              style: TextStyle(
                  color: Theme.of(context).brightness == Brightness.dark
                      ? Colors.white
                      : Colors.grey),
              readOnly: true,
              //validator: validateField,
              focusNode: focusNode,
              controller: controller,
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
最终控制器=文本编辑控制器();
FocusNode FocusNode=新的FocusNode();
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
@凌驾
void initState(){
super.initState();
focusNode.addListener(()异步{
if(focusNode.hasFocus){
等待导航器(
上下文
MaterialPartnerRoute(生成器:(上下文)=>SecondRoute()),
);
FocusScope.of(context).requestFocus(newfocusnode());
}
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
TextFormField(
样式:TextStyle(
颜色:Theme.of(context).brightness==brightness.dark
?颜色:白色
:颜色。灰色),
只读:对,
//验证程序:validateField,
focusNode:focusNode,
控制器:控制器,
),
正文(
“您已经按了这么多次按钮:”,
),
正文(
“$”计数器“,
样式:Theme.of(context).textTheme.display1,
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
}
类SecondRoute扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“第二条路线”),
),
正文:中(
孩子:升起按钮(
已按下:(){
Navigator.pop(上下文);
},
child:Text('Go back!'),
),
),
);
}
}

很乐意帮忙。如果这对您有帮助,请将其标记为答案。谢谢