避免软键盘隐藏自定义输入对话框的Flutter方法
创始人
2024-12-16 12:30:14
0

要避免软键盘隐藏自定义输入对话框,可以使用FocusScope来控制焦点的转移。以下是一个示例代码,演示如何在自定义输入对话框中避免软键盘的隐藏:

import 'package:flutter/material.dart';

class CustomInputDialog extends StatefulWidget {
  @override
  _CustomInputDialogState createState() => _CustomInputDialogState();
}

class _CustomInputDialogState extends State {
  TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('Custom Input Dialog'),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextField(
            controller: _textEditingController,
            decoration: InputDecoration(
              labelText: 'Enter a value',
            ),
          ),
        ],
      ),
      actions: [
        ElevatedButton(
          child: Text('Cancel'),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        ElevatedButton(
          child: Text('OK'),
          onPressed: () {
            // Perform the desired action
            // ...
            Navigator.of(context).pop();
          },
        ),
      ],
    );
  }

  @override
  void initState() {
    super.initState();
    // Request focus on the text field when the dialog is shown
    WidgetsBinding.instance.addPostFrameCallback((_) {
      FocusScope.of(context).requestFocus(FocusNode());
      Future.delayed(Duration(milliseconds: 100)).then((_) {
        FocusScope.of(context).requestFocus(_textFieldFocusNode);
      });
    });
  }

  @override
  void dispose() {
    _textEditingController.dispose();
    super.dispose();
  }
}

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text('Custom Input Dialog Example'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Show Dialog'),
          onPressed: () {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return CustomInputDialog();
              },
            );
          },
        ),
      ),
    ),
  ));
}

在上面的代码中,CustomInputDialog是一个自定义的输入对话框,它使用AlertDialog包裹。在initState方法中,我们使用WidgetsBinding.instance.addPostFrameCallback来在界面绘制完成后请求焦点。首先,我们请求焦点在一个空的FocusNode上,以避免软键盘自动弹出。然后,我们使用Future.delayed延迟100毫秒,以确保焦点请求生效后再次请求焦点在文本字段上,这将导致软键盘弹出并保持弹出状态。

当用户点击“OK”按钮时,你可以在onPressed回调中执行所需的操作,然后关闭对话框。

相关内容

热门资讯

记者发布!pokemmo修改器... 记者发布!pokemmo修改器手机版,wepoker私人辅助器,总是真的是有挂(有挂助手)1、操作简...
实测分享!德普之星透视辅助软件... 实测分享!德普之星透视辅助软件是真的吗,德州hhpoker脚本,真是是有挂(有挂分享)1、下载好脚本...
揭秘真相!wepoker辅助器... 揭秘真相!wepoker辅助器官方,we poker辅助器,本来真的有挂(有挂总结)1、wepoke...
科技通报!wepoker怎么增... 科技通报!wepoker怎么增加运气,HH平台挂,一贯真的有挂(有挂细节)亲,关键说明,透视脚本安卓...
技术分享!hhpoker有没有... 技术分享!hhpoker有没有辅助,hhpoker脚本,真是真的有挂(有挂方式)1、很好的工具软件,...
一分钟揭秘!pokemmo手机... 一分钟揭秘!pokemmo手机版修改器,大菠萝辅助器,原来真的是有挂(有挂总结)1、让任何用户在无需...
9分钟了解!wepoker透视... 9分钟了解!wepoker透视苹果系统,wepoker辅助透视软件,果然存在有挂(有挂教学)1、we...
免费测试版!werplan辅助... 免费测试版!werplan辅助软件,德普之星透视辅助软件,确实是有挂(真的有挂)辅助器是一种具有地方...
玩家实测!wepoker有辅助... 玩家实测!wepoker有辅助插件吗,wepoker透视脚本是什么,切实真的是有挂(有挂总结)1、打...
科普分享!wpk可以作弊吗,h... 科普分享!wpk可以作弊吗,hhpoker德州有挂吗,原来是有挂(有人有挂)1、进入到是否有挂之后,...