flutternavigationfocusautofocus

Focus isn't regained when swiping back from a Route, unlike using the back button in Flutter, iOS


Any ideas on why this is happening and possible solutions would be really welcome!

When I have assigned Focus to a Widget and I am navigating to a new route, and then navigating back to the previous route, that Widget does not re-gain Focus. That is explicitly happening when using the swiping back gesture in iOS. Back button for example has no issue in allowing the widget to re-gain focus .


Solution

  • You can request a function in the screen you are navigating. Then in the next screen use that function in dispose method. In the first screen focus the necessary widget inside the function. For example:

    Navigator.push(MaterialPageRoute(builder:(_)=>SecondScreen(onDispose:(){
    FocusScope.of(context).requestFocus(focusNode);
    })));
    

    Second screen:

    class SecondScreen extends StatefulWidget{
    final void Function() onDispose;
    const SecondScreen(this.onDispose());
    State<_SecondScreen> createState()=> _SecondState();
    }
    
    class _SecondState extends State<_SecondScreen>{
    
    @override
    dispose(){
    widget.onDispose();
    super.dispose();
      }
    }