单击时将TextFormField隐藏在键盘后面

时间:2020-06-25 09:13:35

标签: android flutter user-interface dart

我已经在Column内实现了TextFormField,其父级是SingleChildScrollView。 但是当我单击TextFormField时,以某种方式无法自动滚动。

有关更多详细信息,我要添加视频URL。 Video

代码如下:

Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      floatingActionButton: FloatingActionButton(
        backgroundColor: AppTheme.colors.themeBlue,
        child: Icon(Icons.arrow_forward),
        onPressed: () {

        },
      ),
      body: Container(
        color: AppTheme.colors.backgroundLight,
        child: Column(
          children: <Widget>[
            AppBarWidget(
              title: Constants.addClient,
            ),
            ProfileImageWidget(),
            Container(
                height: 100,
                width: screenSize.width,
                child: ListView(
                  padding: EdgeInsets.all(AppSize.small),
                  children: <Widget>[
                    SizedBox(
                      height: 10,
                    ),
                    ProfileTextFieldWidget(
                      labelTextStyle: AppTheme.textStyle.lightHeading
                          .copyWith(
                          fontSize: AppFontSize.s18,
                          color: AppTheme.colors.themeBlue),
                      labelText: Constants.clientName,
                      hintText: Constants.michaelNilson,
//                        cursorColor: AppTheme.colors.themeBlue,
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    ProfileTextFieldWidget(
                      labelTextStyle: AppTheme.textStyle.lightHeading
                          .copyWith(
                          fontSize: AppFontSize.s18,
                          color: AppTheme.colors.themeBlue),
                      labelText: Constants.email,
                      hintText: Constants.email,
                      textInputAction: TextInputAction.done,
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    ProfileTextFieldWidget(
                      labelTextStyle: AppTheme.textStyle.lightHeading
                          .copyWith(
                          fontSize: AppFontSize.s18,
                          color: AppTheme.colors.themeBlue),
                      labelText: Constants.mobile,
                      hintText: Constants.mobile,
                      textInputAction: TextInputAction.done,
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    ProfileTextFieldWidget(
                      labelTextStyle: AppTheme.textStyle.lightHeading
                          .copyWith(
                          fontSize: AppFontSize.s18,
                          color: AppTheme.colors.themeBlue),
                      labelText: Constants.followUp,
                      hintText: Constants.followUp,
                      textInputAction: TextInputAction.done,
                      suffixIcon: IconButton(
                        icon: Icon(
                          Icons.keyboard_arrow_down,
                          color: Colors.black87,
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    ProfileTextFieldWidget(
                      labelTextStyle: AppTheme.textStyle.lightHeading
                          .copyWith(
                          fontSize: AppFontSize.s18,
                          color: AppTheme.colors.themeBlue),
                      labelText: Constants.date,
                      suffixIcon: IconButton(
                        icon: Icon(
                          Icons.date_range,
                          size: 15,
                          color: Colors.black87,
                        ),
                      ),
                      hintText: Constants.date,
                      textInputAction: TextInputAction.done,
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    ProfileTextFieldWidget(
                      labelTextStyle: AppTheme.textStyle.lightHeading
                          .copyWith(
                          fontSize: AppFontSize.s18,
                          color: AppTheme.colors.themeBlue),
                      suffixIcon: IconButton(
                        icon: Icon(
                          Icons.access_time,
                          size: AppSize.medium,
                          color: Colors.black87,
                        ),
                      ),
                      hintText: Constants.time,
                      labelText: Constants.time,
                      textInputAction: TextInputAction.done,
                    ),
                    SizedBox(
                      height: AppSize.small,
                    ),
                    ProfileTextFieldWidget(
                      labelTextStyle: AppTheme.textStyle.lightHeading
                          .copyWith(
                          fontSize: AppFontSize.s18,
                          color: AppTheme.colors.themeBlue),
                      labelText: Constants.notes,
                      hintText: Constants.notes,
                      textInputAction: TextInputAction.done,
                    ),
                    SizedBox(
                      height: AppSize.small,
                    ),
                  ],
                )
            )
          ],
        ),
      ),
    );
  }

有关详细信息,请参见视频:

2 个答案:

答案 0 :(得分:0)

立即删除resizeToAvoidBottomPadding: false,

并使用 resizeToAvoidBottomInset: true,

答案 1 :(得分:0)

撰写动画并在TextField获得焦点时向上移动TextField容器。

要了解有关制作动画的知识,请参阅Composing Animations and Chaining Animations in Dart's Flutter Framework

使用Flutter的FocusNode检测对TextField的焦点

这是一个示例:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Animation Demo',
      theme: new ThemeData(
        primaryColor: new Color(0xFFFF0000),
      ),
      home: new FormDemo(),
    );
  }
}

class FormDemo extends StatefulWidget {
  @override
  _FormDemoState createState() => _FormDemoState();
}

class _FormDemoState extends State<FormDemo> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation _animation;

  FocusNode _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(vsync: this, duration: Duration(milliseconds: 300));
    _animation = Tween(begin: 300.0, end: 50.0).animate(_controller)
    ..addListener(() {
      setState(() {});
    });

    _focusNode.addListener(() {
      if (_focusNode.hasFocus) {
        _controller.forward();
      } else {
        _controller.reverse();
      }
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    _focusNode.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false, // this avoids the overflow error
      appBar: AppBar(
        title: Text('TextField Animation Demo'),
      ),
      body: new InkWell( // to dismiss the keyboard when the user tabs out of the TextField
        splashColor: Colors.transparent,
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            children: <Widget>[
              SizedBox(height: _animation.value),
              TextFormField(
                decoration: InputDecoration(
                  labelText: 'I move!',
                ),
                focusNode: _focusNode,
              )
            ],
          ),
        ),
      ),
    );
  }
}