2017-12-25 23 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"),), 
    ), 
    ); 
    } 
} 
関連する問題