了解在Flutter中按下返回按钮以精确知道软键盘何时被关闭。

huangapple go评论54阅读模式
英文:

How to know exactly the moment the Back button is pressed to dismiss the Soft keyboard in flutter

问题

I don't want to wait until the Soft keyboard has been dismissed.

我不想等到软键盘被关闭才行。

The moment the user presses the back button to dismiss the keyboard I want to also at the same speed and time dismiss something else as well.

当用户按下返回按钮关闭键盘时,我想以相同的速度和时间关闭其他东西。

I attempted the following code to know when the user has pressed the back button to dismiss the soft keyboard

我尝试了以下代码来确定用户何时按下返回按钮以关闭软键盘:

@override
Widget build(BuildContext context) {
  return WillPopScope(
    onWillPop: () async {
      print("Back button has been pressed"); //This seems not to be called when the soft keyboard is been dismissed
      return true;
    },
    child: TextButton("Sample Button")),
  );
}

I was surprised that the WillPopScope widgets' onWillPop method wasn't called when the soft keyboard was dismissed.

我对WillPopScope小部件的onWillPop方法在软键盘被关闭时没有被调用感到惊讶。

I don't want to use Keyboard Visibility package because that will only fire when the keyboard has been dismissed. Instead I want to know immediately the back button was pressed to dismiss the keyboard at that point in time.

我不想使用键盘可见性包,因为它只在键盘被关闭时触发。相反,我想立即知道返回按钮何时被按下以关闭键盘。

英文:

I don't want to wait until the Soft keyboard has been dismissed.

The moment the user presses the back button to dismiss the keyboard I want to also at the same speed and time dismiss something else as well.

I attempted the following code to know when the user has pressed the back button to dismiss the soft keyboard

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        print(
            "Back button has been pressed"); //This seems not to be called when the soft keyboard is been dismissed
        return true;
      },
      child: TextButton("Sample Button")),
    );
  }

I was surprised that the WillPopScope widgets' onWillPop method wasn't called when the softkeyboard was dismissed.

I don't want to use Keyboard Visibility package because that will only fire when the keyboard has been dismissed. Instead I want to know immediately the back button was pressed to dismiss the keyboard at that point in time.

答案1

得分: 1

class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State {
final _focusNode = FocusNode();

@override
void initState() {
super.initState();
_focusNode.addListener(_onFocusChange);
}

@override
void dispose() {
_focusNode.removeListener(_onFocusChange);
super.dispose();
}

void _onFocusChange() {
if (!_focusNode.hasFocus) {
print('Back button was pressed to dismiss keyboard');
// Perform action you want here
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: ElevatedButton(
onPressed: () {},
child: Text('Sample Button'),
autofocus: true,
focusNode: _focusNode,
),
),
),
);
}
}

英文:
class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  final _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    _focusNode.addListener(_onFocusChange);
  }

  @override
  void dispose() {
    _focusNode.removeListener(_onFocusChange);
    super.dispose();
  }

  void _onFocusChange() {
    if (!_focusNode.hasFocus) {
      print('Back button was pressed to dismiss keyboard');
      // Perform action you want here
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: ElevatedButton(
            onPressed: () {},
            child: Text('Sample Button'),
            autofocus: true,
            focusNode: _focusNode,
          ),
        ),
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年5月8日 02:45:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定