颤动:将键盘对焦在背面导航上

问题描述 投票:2回答:1

我想在用户使用导航栏中的后退按钮导航回来后关注textField。你是怎样做的?我尝试了autofocus小部件的TextField属性。但这仅在第一次创建窗口小部件时向前导航时有效。在iOS上有viewDidAppear方法,在Flutter中有类似的东西吗?

谢谢!

dart uinavigationbar flutter viewdidappear
1个回答
3
投票

你将需要提供你的TextFieldFocusNode,然后你可以await用户返回并在导航发生时设置FocusScope.of(context).requestFocus(myNode)

简单演示:

enter image description here

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => new _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  FocusNode n = new FocusNode();
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title:new Text("First Page")),
      body: new Center(
        child: new Column(
          children: <Widget>[
            new TextField(
              focusNode: n,
            ),
            new RaisedButton(onPressed: ()async{

              bool focus = await Navigator.of(context).push(new MaterialPageRoute(builder: (_)=>new SecondPage()));
              if (focus == true|| focus==null){

                  FocusScope.of(context).requestFocus(n);

              }
              },
            child: new Text("NAVIGATE"),),
          ],
        ),
      ),
    );
  }
}


class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(title:new Text("Second Page")),
    body: new Center(
      child: new RaisedButton(onPressed: (){Navigator.pop(context,true);},child: new Text("BACK"),),
    ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.