Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofocus not working on iOS #198

Open
denddyprod opened this issue Jun 8, 2022 · 1 comment
Open

Autofocus not working on iOS #198

denddyprod opened this issue Jun 8, 2022 · 1 comment

Comments

@denddyprod
Copy link

denddyprod commented Jun 8, 2022

Steps that I made:

Click button to the second page
My keyboard doesnt appear but focuses on input.
Click on the button that leads to the back page.
Press again the button leading to the second page

Expected results:

My expectation is that every time I enter the page my keyboard and focus will appear on the input.

Actual results:

After a few repetitions of the steps mentioned above, the focus remains on the input but the keyboard no longer appears.

This only happens on Safari on iOS, I also tested it on Chrome ( on iOS ) and Android phones as a result it worked. A strange fact is that the keyboard does not appear even though the focus is visible on the given input.

Code sample:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:keyboard_actions/keyboard_actions_item.dart';

void main() {
  runApp(const MaterialApp(
    title: 'Navigation Basics',
    home: FirstRoute(),
  ));
}

class FirstRoute extends StatelessWidget {
  const FirstRoute({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Route'),
      ),
      body: Center(
        child: ElevatedButton(
          child: const Text('Open route'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondRoute()),
            );
          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatefulWidget {
  SecondRoute({super.key});

  @override
  State<SecondRoute> createState() => _SecondRouteState();
}

class _SecondRouteState extends State<SecondRoute> {
  late FocusNode _nodeText1;
  late TextEditingController _controller1;

  KeyboardActionsConfig _buildConfig(BuildContext context) {
    return KeyboardActionsConfig(
      keyboardActionsPlatform: KeyboardActionsPlatform.ALL,
      keyboardBarColor: Colors.grey[200],
      nextFocus: false,
      actions: [
        KeyboardActionsItem(
          focusNode: _nodeText1,
        ),
      ],
    );
  }

  void initState() {
    super.initState();
    _nodeText1 = FocusNode();
    _controller1 = TextEditingController();
  }

  @override
  void dispose() {
    _nodeText1.dispose();
    _controller1.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Route'),
      ),
      body: KeyboardActions(
        config: _buildConfig(context),
        child: Container(
            width: double.infinity,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                TextField(
                  autofocus: true,
                  keyboardType: TextInputType.number,
                  focusNode: _nodeText1,
                  controller: _controller1,
                  decoration: InputDecoration(
                    hintText: "Input Number",
                  ),
                ),
                ElevatedButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: const Text('Go back!'),
                )
              ],
            )),
      ),
    );
  }
}
@diegoveloper
Copy link
Owner

Did you try using KeyboardActions above Column and not Container ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants