skip to Main Content

When I restart this code in Visual Studio autofocus is working as well as keyboard is showing. When I close the visual studio and re open the app in android emulator the keyboard is not showing but auto focus is working (Cursor is showing).

But its work in android 5 perfectly.

My code:

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 {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  
  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: TextField(
          autofocus: true,
        )
      ),
    );
  }
}

Can anyone fix this? Thank you.

2

Answers


  1. Try the following code:

    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @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 {
      const MyHomePage({Key? key, required this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final FocusNode focusNode = FocusNode();
    
      @override
      void initState() {
        super.initState();
        focusNode.requestFocus();
      }
    
      @override
      void dispose() {
        focusNode.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: TextField(
              focusNode: focusNode,
              autofocus: true,
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. Deleting autofocus: true and setting the focus after a delay worked for me:

    ...
    
    class _MyHomePageState extends State<MyHomePage> {
      late FocusNode myFocusNode;
    
      @override
      void initState() {
        super.initState();
        myFocusNode = FocusNode();
        SchedulerBinding.instance.addPostFrameCallback((_) {
          Timer(const Duration(milliseconds: 1000), () {focusNode.requestFocus();});
        });
      }
    
      ...
    
            child: TextField(
              focusNode: myFocusNode,
            )
    
      ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search