【问题标题】:Flutter when button clicked scroll down and open textfield单击按钮时颤动向下滚动并打开文本字段
【发布时间】:2025-11-28 07:50:02
【问题描述】:

我有 Flutter 应用程序。我在页面顶部有一个按钮,在页面底部有一个文本字段。 我想要一些当我单击按钮时向下滚动并打开文本字段的东西。我该怎么做?

提前谢谢...

【问题讨论】:

  • 发布你的代码,到目前为止你尝试了什么?

标签: flutter dart flutter-layout flutter-animation


【解决方案1】:

让我重新表述您的问题:您基本上希望在按下按钮时聚焦文本字段。

final FocusNode _focusNode = FocusNode(); //This is the solution of your problem
final TextField _textField = TextField(
  focusNode: _focusNode,
  decoration: InputDecoration(
    border: InputBorder.none,
    hintText: 'This is your textfield'
  ),
);

final FloatingActionButton _myButton = FloatingActionButton(
        onPressed: () => _focusNode.requestFocus(),
        child: Icon(Icons.adjust),
        backgroundColor: Colors.green,
      );

将它们放在你的脚手架中并尝试一下:

return Scaffold(
  appBar: AppBar(
    title: const Text('Change focus example'),
  ),
  body: SingleChildScrollView(child: Column(
  children: <Widget> [
    Container(height: MediaQuery.of(context).size.height*3),
    _textField,
  ]
  )),
  floatingActionButton: _myButton,
);

【讨论】: