【问题标题】:Flutter/Dart Scrolling textfield above keyboard键盘上方的颤振/飞镖滚动文本字段
【发布时间】:2019-09-14 08:05:16
【问题描述】:

我正在使用文本字段,我怎样才能让它在输入电子邮件和密码时向上滚动一点?

这里是代码预览

https://pastebin.com/4EngQDV5

blank

【问题讨论】:

  • 但是这些文本字段在哪里?你能展示一下你到目前为止所拥有的吗?
  • @Hosar,我添加了代码预览,代码可能有点乱,但你可以看看thx。
  • 您应该保留问题中的代码以供将来参考。
  • 我的解决方案here at this thread 解决了这个问题。它非常简短,不需要动画。

标签: dart flutter


【解决方案1】:

好的,首先,您粘贴的代码不完整,所以我猜您将这些文本字段放在列中。您有两个选择:

1)在你的 Scaffold 中,你可以将此属性设置为 false,例如 resizeToAvoidBottomInset: false,
2o) 您可以使用 SingleChildScrollView 在键盘出现时向上移动您的文本字段,例如:

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: SingleChildScrollView(
          child: MyLoginPage(title: 'Flutter Demo Home Page'),
        ),
      ),
    );
  }
}

class MyLoginPage extends StatefulWidget {
  MyLoginPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyLoginPageState createState() => _MyLoginPageState();
}

class _MyLoginPageState extends State<MyLoginPage> {
  String _email;
  String _password;
  TextStyle style = TextStyle(fontSize: 25.0);

  @override
  Widget build(BuildContext context) {
    final emailField = TextField(
      obscureText: false,
      style: style,
      decoration: InputDecoration(
          contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
          prefixIcon: Icon(FontAwesomeIcons.solidEnvelope),
          hintText: "Email",
          focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.red[300], width: 32.0),
              borderRadius: BorderRadius.circular(97.0))),
      onChanged: (value) {
        setState(() {
          _email = value;
        });
      },
    );
    final passwordField = TextField(
      obscureText: true,
      style: style,
      decoration: InputDecoration(
          contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
          prefixIcon: Icon(FontAwesomeIcons.key),
          hintText: "Password",
          focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.red[300], width: 32.0),
              borderRadius: BorderRadius.circular(25.0))),
      onChanged: (value) {
        setState(() {
          _password = value;
        });
      },
    );

    return Center(
      child: Column(
        children: <Widget>[
          Container(
            color: Colors.yellow[300],
            height: 300.0,
          ),
          emailField,
          passwordField
        ],
      ),
    );
  }
}

只需复制并粘贴代码,看看是否是您想要的。
希望对您有所帮助。

【讨论】:

  • 我试过这个解决方案。但是打字后,它不能向上滚动,你能帮我解决这个问题吗?
【解决方案2】:

在我的案例中,有一种解决方案可以提供预期的结果。 TextField 有一个属性调用scrollPaddingscrollPadding: EdgeInsets.only(bottom:40), 默认设置为EdgeInsets.all(20.0)

您可以给定一个固定值或使用 viewInsets。 希望这对你也有帮助。 请查找有关此here 的更多信息。

【讨论】:

    【解决方案3】:

    其实我也有同样的问题。认为您需要将 TextForms 包装到 ListView 中才能使其工作。

    import 'package:flutter/material.dart';
    
    class ScrollView extends StatefulWidget {
    @override
    State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ScrollViewState();
    }
    }
    
    class _ScrollViewState extends State<ScrollView>{
    @override
    Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: userInputBackgroundColor,
        body: ListView(
                  padding: EdgeInsets.all(0.0),
                  shrinkWrap: true,
                  children: <Widget>[
                   TextForm(),
                   TextForm()
                  ]
              ),
            )
        }
    }
    

    【讨论】:

    • 我正在尝试仅使用文本字段,您可以在 og post 中查看我的代码。
    【解决方案4】:

    我使用颤振 2.2.0,当我添加 SingleChildScrollView 时,如果键盘显示并与文本字段重叠,它会自动向上滚动。 resizeToAvoidBottomInset 默认 true 允许此功能(在 iOS 上测试)。如果您希望 Textfield 有更多的键盘空间,请尝试回答 Varsik Nikoyan

    https://stackoverflow.com/a/67088995/12970517

    希望能帮上忙。

    【讨论】:

      【解决方案5】:

      这是新代码。我认为这个对你有帮助。在 passwordField 声明之后添加此代码。

       return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              emailField,
              SizedBox(
                height: 10,
              ),
              passwordField,
            ],
          ),
        ),
      );
      

      【讨论】:

      • 我在新页面上尝试了这个,但它都出现在屏幕顶部,我该如何定位文本字段?
      • 我更改了代码。请在您的最终密码字段声明后添加此代码
      【解决方案6】:

      我找到了一个不错的解决方案 我已经 affed 到 TextFormField scrollPadding 属性如下

      child: TextFormField(
              scrollPadding: EdgeInsets.only(
                  bottom: MediaQuery.of(context).viewInsets.bottom + fontSize*4),
              validator: _validator,
              cursorColor: cTextRegularColor,
              controller: _controller,
              obscureText: _obscure,
              obscuringCharacter: '*',
              textAlign: _textAlign,
              keyboardType: _input,
              style: const TextStyle(
                color: cTextRegularColor,
                fontSize: fontSize,
              ),
      

      通过设置bottomInset,在我的表单中,每当用户点击表单中的TextField时,它不仅会向下滚动,还会显示表单中的下一个TextField,因此用户完成后不需要滚动填充当前字段。

      显然,当用户点击表单中的下一个字段时,屏幕将再次滚动,并再次显示下一个字段,

      我发现在长表单(超过 5 个字段)中,用户根本不需要滚动。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-25
        • 2020-11-05
        • 1970-01-01
        • 2021-09-27
        • 2022-07-27
        • 2020-05-19
        • 1970-01-01
        相关资源
        最近更新 更多