当键盘打开时,如何滚动屏幕以使文本字段获得焦点。

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

how to scroll the screen with textfield gets focus when the keyboard open

问题

我有一个Flutter应用程序,并创建了一个表单屏幕来获取用户详细信息。在表单屏幕上,当用户点击文本字段并获得焦点时,键盘会打开,需要滚动文本字段,以便用户可以看到他正在输入的内容。

英文:

I have flutter app and created form screen to get user details. In the form screen when user tap the textfield and it gets focus then keyboard opens need to scroll the textfield user want to seen what he is typing.

答案1

得分: 5

要在文本字段获得焦点时滚动到它,您需要使用Scrollable.ensureVisible方法。以下是演示如何使用它的一些代码。希望这有所帮助!

class TextFieldVisibility extends StatefulWidget {
  const TextFieldVisibility({Key? key}) : super(key: key);

  @override
  State<TextFieldVisibility> createState() => TextFieldVisibilityState();
}

class TextFieldVisibilityState extends State<TextFieldVisibility> {
  final _key1 = GlobalKey<State<StatefulWidget>>();
  final _key2 = GlobalKey<State<StatefulWidget>>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SingleChildScrollView(
      child: Column(
        children: [
          Container(
            color: Colors.orange,
            height: 1000,
          ),
          TextField(
            key: _key1,
            onTap: () {
              ensureVisibleOnTextArea(textfieldKey: _key1);
            },
          ),
          TextField(
            key: _key2,
            onTap: () {
              ensureVisibleOnTextArea(textfieldKey: _key2);
            },
          )
        ],
      ),
    ));
  }

  Future<void> ensureVisibleOnTextArea(
      {required GlobalKey textfieldKey}) async {
    final keyContext = textfieldKey.currentContext;
    if (keyContext != null) {
      await Future.delayed(const Duration(milliseconds: 500)).then(
        (value) => Scrollable.ensureVisible(
          keyContext,
          duration: const Duration(milliseconds: 200),
          curve: Curves.decelerate,
        ),
      );
      // 如果第一个不起作用,可以尝试以下可选部分
      // await Future.delayed(const Duration(milliseconds: 500)).then(
      //   (value) => Scrollable.ensureVisible(
      //     keyContext,
      //     duration: const Duration(milliseconds: 200),
      //     curve: Curves.decelerate,
      //   ),
      // );
    }
  }
}

祝好!

英文:

To scroll to the text field when it receives focus, you need to use the Scrollable.ensureVisible method. Here is some code to show you how to use it. I hope it helps!

Regards.

class TextFieldVisibility extends StatefulWidget {
  const TextFieldVisibility({Key? key}) : super(key: key);

  @override
  State&lt;TextFieldVisibility&gt; createState() =&gt; TextFieldVisibilityState();
}

class TextFieldVisibilityState extends State&lt;TextFieldVisibility&gt; {
  final _key1 = GlobalKey&lt;State&lt;StatefulWidget&gt;&gt;();
  final _key2 = GlobalKey&lt;State&lt;StatefulWidget&gt;&gt;();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SingleChildScrollView(
      child: Column(
        children: [
          Container(
            color: Colors.orange,
            height: 1000,
          ),
          TextField(
            key: _key1,
            onTap: () {
              ensureVisibleOnTextArea(textfieldKey: _key1);
            },
          ),
          TextField(
            key: _key2,
            onTap: () {
              ensureVisibleOnTextArea(textfieldKey: _key2);
            },
          )
        ],
      ),
    ));
  }

  Future&lt;void&gt; ensureVisibleOnTextArea(
      {required GlobalKey textfieldKey}) async {
    final keyContext = textfieldKey.currentContext;
    if (keyContext != null) {
      await Future.delayed(const Duration(milliseconds: 500)).then(
        (value) =&gt; Scrollable.ensureVisible(
          keyContext,
          duration: const Duration(milliseconds: 200),
          curve: Curves.decelerate,
        ),
      );
      // Optional if doesnt work with the first
      // await Future.delayed(const Duration(milliseconds: 500)).then(
      //   (value) =&gt; Scrollable.ensureVisible(
      //     keyContext,
      //     duration: const Duration(milliseconds: 200),
      //     curve: Curves.decelerate,
      //   ),
      // );
    }
  }
}

答案2

得分: 0

将您的列部件包装在SingleChildScrollView中。

示例:

 Scaffold(
    body: SingleChildScrollView(
      child: Column(
        children:[ 
          TextField(),
          TextField()
        ],
      ),
    )
  ),
英文:

Wrap you Column Widget with SingleChildScrollView

Example:

 Scaffold(
        body: SingleChildScrollView(
          child: Column(
            children:[
              TextField(),
              TextField()
          ],
        ),
      )
    ),

huangapple
  • 本文由 发表于 2023年3月9日 21:55:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685576.html
匿名

发表评论

匿名网友

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

确定