Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text in TextField scrolls up after opening the keyboard #48

Closed
SergeiMikhailovskii opened this issue Nov 25, 2019 · 14 comments
Closed

Text in TextField scrolls up after opening the keyboard #48

SergeiMikhailovskii opened this issue Nov 25, 2019 · 14 comments

Comments

@SergeiMikhailovskii
Copy link

SergeiMikhailovskii commented Nov 25, 2019

Hello, I faced with the following problem after adding your keyboard: when I open the keyboard text in TextField scrolls up and isn't shown until I close the keyboard. Here are the screenshots:

Before opening of the keyboard:

Screen Shot 2019-11-25 at 14 17 42

After opening:

Screen Shot 2019-11-25 at 14 18 21

Text in textfield didn't disappear but it only scrolled up that it's not visible now, unfortunately I can't show it. So, here's the way how I implemented keyboard to my widget:

class InputGoalWidget extends StatefulWidget {
  final bool isValid = true;

  final String title;
  final String hint;
  final TextInputType tit;
  final String emoji;
  final TextEditingController controller;

  InputGoalWidget({this.title, this.hint, this.emoji, this.controller, this.tit});

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

class _InputGoalWidgetState extends State<InputGoalWidget> with SingleTickerProviderStateMixin {
  AnimationController animationController;
  Animation<double> animation;
  FocusNode _node = FocusNode();

  KeyboardActionsConfig _buildConfig(BuildContext context) {
    return KeyboardActionsConfig(
      keyboardActionsPlatform: KeyboardActionsPlatform.ALL,
      keyboardBarColor: Colors.white,
      nextFocus: false,
      actions: [
        KeyboardAction(
          focusNode: _node,
          displayCloseWidget: true,
        ),
      ],
    );
  }

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 600),
    )..addListener(() => setState(() {}));

    animation = Tween<double>(
      begin: 50.0,
      end: 120.0,
    ).animate(CurvedAnimation(
      parent: animationController,
      curve: Interval(0.8, 1.0, curve: Curves.fastOutSlowIn),
    ));

    animationController.forward();
  }

  @override
  Widget build(BuildContext context) {
    return buildContainer(context);
  }

  Container buildContainer(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 3),
      color: Color(AppColors.white),
      child: Padding(
        padding: const EdgeInsets.only(top: 15, bottom: 10),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(left: 20.0, right: 16),
              child: Text(
                widget.emoji,
                textAlign: TextAlign.justify,
                style: TextStyle(fontSize: 32),
              ),
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(widget.title, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                Container(
                  height: MediaQuery.of(context).size.height * 0.06,
                  width: MediaQuery.of(context).size.width * 0.7,
                  child: Center(
                    child: Padding(
                      padding: const EdgeInsets.only(bottom: 0),
                        child: FormKeyboardActions(
                          config: _buildConfig(context),
                          child: TextField(
                            textCapitalization: TextCapitalization.sentences,
                            focusNode: _node,
                            style: TextStyle(
                              fontSize: 18,
                              color: Color(AppColors.brandViolet),
                              fontWeight: FontWeight.w500,
                            ),
                            keyboardType: widget.tit,
                            inputFormatters: widget.tit == TextInputType.number ? [WhitelistingTextInputFormatter.digitsOnly] : null,
                            decoration: InputDecoration(
                              hintText: widget.hint,
                              border: InputBorder.none,
                            ),
                            controller: widget.controller,
                          ),
                        ),
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}

And here's the way how this widgets are used in the screen:

 Flexible(
            flex: 1,
            child: ListView(
              children: <Widget>[
                _nameWidget,
                _calorieWidget,
                _stepsWidget,
                _waterWidget,
              ],
            ),
          ),

So. _nameWidget, _calorieWidget, _stepsWidget and _waterWidget are InputGoalWidgets. Why such issue can happen? Thanks in advance for any help!

@diegoveloper
Copy link
Owner

diegoveloper commented Nov 25, 2019 via email

@SergeiMikhailovskii
Copy link
Author

Sure, here it is

keyboard

@diegoveloper
Copy link
Owner

diegoveloper commented Nov 26, 2019 via email

@SergeiMikhailovskii
Copy link
Author

I modified it in this way:

Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Text(widget.title, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
                Container(
                  height: MediaQuery.of(context).size.height * 0.06,
                  width: MediaQuery.of(context).size.width * 0.7,
                  child: Center(
                    child: Padding(
                      padding: const EdgeInsets.only(bottom: 0),
                        child: FormKeyboardActions(
                          config: _buildConfig(context),
                          child: TextField(
                            textCapitalization: TextCapitalization.sentences,
                            focusNode: _node,
                            style: TextStyle(
                              fontSize: 18,
                              color: Color(AppColors.brandViolet),
                              fontWeight: FontWeight.w500,
                            ),
                            keyboardType: widget.tit,
                            inputFormatters: widget.tit == TextInputType.number ? [WhitelistingTextInputFormatter.digitsOnly] : null,
                            decoration: InputDecoration(
                              hintText: widget.hint,
                              border: InputBorder.none,
                            ),
                            controller: widget.controller,
                          ),
                        ),
                    ),
                  ),
                )
              ],
            )

But it still doesn't work. Or I didn't understand you in the right way

@diegoveloper
Copy link
Owner

diegoveloper commented Nov 27, 2019 via email

@ShrutiThakur01
Copy link

ShrutiThakur01 commented Dec 16, 2019

Hello All,

any update about this?
I am facing same problem. i have already posted my problem in detail on Stack Overflow.

https://stackoverflow.com/questions/59353957/textfield-shrinking-scrolling-when-keyboard-appears-using-keyboard-actions-packa

Thanks in advance

@diegoveloper
Copy link
Owner

diegoveloper commented Dec 16, 2019 via email

@EricBichara
Copy link

I'm also seeing this issue, will try to create a sample soon

@edblocker
Copy link

I'm experiencing the same behaviour

@sergeatoma
Copy link

Same here

@diegoveloper
Copy link
Owner

diegoveloper commented Mar 10, 2020 via email

@sergeatoma
Copy link

@diegoveloper you can easily paste code below as is, and check. Thanks!

import 'package:flutter/material.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:keyboard_actions/keyboard_actions_config.dart';

class KeyboardTestScreen extends StatefulWidget {
  @override
  _KeyboardTestScreenState createState() => _KeyboardTestScreenState();
}

class _KeyboardTestScreenState extends State<KeyboardTestScreen> {
  final FocusNode _amountFocusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Keyboard test"),
      ),
      body: _rootWidgetBuilder(context),
    );
  }

  Widget _rootWidgetBuilder(BuildContext context) {
    return ListView(
      children: <Widget>[
        Container(
          height: 100.0,
          color: Colors.grey,
        ),
        _problemSection(context),
        Container(
          height: 100.0,
          color: Colors.black54,
        ),
      ],
    );
  }

  KeyboardActionsConfig _buildKeyboardActions(BuildContext context) {
    return KeyboardActionsConfig(
        keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
        actions: [
          KeyboardAction(
            focusNode: _amountFocusNode,
            displayArrows: false,
            displayDoneButton: true,
          )
        ]);
  }

  Widget _problemSection(BuildContext context) {
    const TextStyle sampleTextStyle = TextStyle(
      fontSize: 24.0,
      color: Colors.red,
      letterSpacing: 1.0,
      fontWeight: FontWeight.w600,
    );
    return Container(
      height: 120.0,
      padding: EdgeInsets.only(
        left: 24.0,
        right: 24.0,
      ),
      child: KeyboardActions(
        config: _buildKeyboardActions(context),
        child: Row(
          children: <Widget>[
            Container(
              width: 72.0,
              height: 72.0,
              margin: EdgeInsets.only(right: 24.0),
              child: Icon(Icons.access_alarm),
            ),
            Expanded(
              child: TextFormField(
                focusNode: _amountFocusNode,
                keyboardType: TextInputType.numberWithOptions(
                    signed: false, decimal: true),
                style: sampleTextStyle,
                decoration: InputDecoration(
                  contentPadding: EdgeInsets.only(top: 0.0),
                  labelText: "Label",
                  labelStyle: TextStyle(
                      fontSize: 16.0,
                      color: Colors.grey,
                      fontWeight: FontWeight.w500),
                  helperStyle: TextStyle(
                      fontSize: 12.0,
                      color: Colors.grey,
                      fontWeight: FontWeight.w500),
                  helperText: "Long description",
                  prefixText: "\$",
                  alignLabelWithHint: true,
                  prefixStyle: sampleTextStyle.copyWith(height: 1.8),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

sample

@diegoveloper
Copy link
Owner

@sergeatoma use KeyboardActions as a parent of your ListView

  Widget _rootWidgetBuilder(BuildContext context) {
    return KeyboardActions(
      config: _buildKeyboardActions(context),
      child: ListView(
        children: <Widget>[
          Container(
            height: 100.0,
            color: Colors.grey,
          ),
          _problemSection(context),
          Container(
            height: 100.0,
            color: Colors.black54,
          ),
        ],
      ),
    );
  }

@luckymehndiratta
Copy link

luckymehndiratta commented Mar 16, 2020

After Update Flutter version we face this issue and App Hang
/InputMethodManager(19541): Starting input: tba=com.example.mannforce_hcm ic=io.flutter.plugin.editing.InputConnectionAdaptor@9f4eb0d mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
D/InputMethodManager(19541): startInputInner - Id : 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants