2017-12-25 28 views
1

내비게이션 막대에서 뒤로 버튼을 사용하여 사용자가 뒤로 이동 한 후에 textField에 집중하려고합니다. 어떻게 그걸합니까? TextField 위젯의 autofocus 속성을 사용해 보았습니다. 그러나 이것은 위젯이 처음 생성 될 때 앞으로 탐색 할 때만 작동합니다. iOS에는 viewDidAppear 메소드가 있습니다. Flutter에 비슷한 것이 있습니까?Flutter : 뒤로 내비게이션에 키보드 초점 맞추기

감사합니다.

답변

2

당신은 당신의 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"),), 
    ), 
    ); 
    } 
}