how to return AnimatinonContainer to its previous state?


How to make the AnimatedContainer return to its previous state (1) when the keyboard is closed? at the moment changing the location of the container happens via onTap

Widget _formAuth(String labelForm, String labelButton, void func()) {
    return AnimatedContainer(
      duration: Duration(seconds: 1),
      curve: Curves.ease,
      margin: EdgeInsets.fromLTRB(13, _marginFormAuth, 13, 50),
Widget _inputLoginOrPassword(
      Icon icon, String hint, TextEditingController controller, bool obscure) {
    return Container(
      margin: EdgeInsets.fromLTRB(10, 35, 10, 0),
      child: TextField(
        onTap: () {
          setState(() {
            _marginFormAuth = 20;

enter image description here


You can define a boolean variable in your state class and assign its value in the build method like following:

bool isKeyboardOpened;

build () {
isKeyboardOpened = MediaQuery.of(context).viewInsets.bottom > 0;
_marginFormAuth = isKeyboardOpened ? 20 : whateverYouHadBefore;

This solution to move containers when you open the keyboard isn’t the best approach however, you should modify the whole concept behind your code and use scrollables that by nature scroll when you focus a field 🙂

Answered By – Gabriel Costache

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *