Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyboard is pushing the FloatingActionButton upward in flutter app

I am trying to create a simple To Do App in in flutter with a Floating Action Button in the bottom which when clicked show an Alert Dialog to add items to the list. Every time I click on the button, the Keyboard pushes the Action Button upward causing overflowing error. Is there any way to avoid pushing the action button upward when Keyboard is opened? Here is the snapshot I took: Snapshot Below the source code:

import 'package:flutter/material.dart';
import '../model/todo_item.dart';

class ToDoScreen extends StatefulWidget {
  @override
  _ToDoScreenState createState() => _ToDoScreenState();
}

class _ToDoScreenState extends State<ToDoScreen> {
  TextEditingController _textEditingController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueAccent,
      body: Column(
        children: <Widget>[ToDoItem("Going for a Walk", "12 January, 2019")],
      ),
      floatingActionButton: FloatingActionButton(
        tooltip: 'Add Item',
        child: Icon(Icons.add),
        backgroundColor: Colors.red,
        onPressed: _showFormDialog,
      ),
    );
  }

  void _showFormDialog() {
    var alert = AlertDialog(
      content: Row(
        children: <Widget>[
          Expanded(
            child: TextField(
              controller: _textEditingController,
              autofocus: true,
              decoration: InputDecoration(
                  labelText: "Item",
                  hintText: "eg. Buy Vegetables",
                  icon: Icon(Icons.note_add)),
            ),
          )
        ],
      ),
      actions: <Widget>[
        FlatButton(
          onPressed: () {
            // _handleSubmit(_textEditingController.text);
            _textEditingController.clear();
          },
          child: Text("Save ToDo"),
        ),
        FlatButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text("Cancel"),
        )
      ],
    );
    showDialog(context: context, builder: (BuildContext context) => alert);
  }
}
like image 852
Albert George Avatar asked Jan 09 '19 17:01

Albert George


People also ask

How do I turn off the floating action button in flutter?

Currently, a fab (floating action button) can be disabled by setting the onPressed attribute to null.

What is floating action button flutter?

A floating action button is a circular icon button that hovers over content to promote a primary action in the application. Floating action buttons are most commonly used in the Scaffold. floatingActionButton field.

How do you make a floating button in flutter?

Create a floating action button and give it a child. Add callback onPressed function to increase the counter variable i. Give background-color if needed. Use floatingActionButtonLocation to set the location of the button.

What is floatingactionbutton in flutter?

FloatingActionButton in Flutter. A floating action button is a circular icon button that hovers over content to promote a primary action in the application. Floating action buttons are most commonly used in the Scaffold.floatingActionButton field.

What does onpressed do in a floatingactionbutton?

It controls the cursor for the mouse pointer when it interacts with the button. onPressed: callback function. splashColor: splash color of FloatingActionButton.

What is resizetoavoidbottominset in flutter?

It is default functionality of the Flutter. so, if you set resizeToAvoidBottomInset: false then you will lose the functionality of keeping your UI components above the keyboard. To have a background image in your application better way is to do it like this:


4 Answers

I had the same issue, where my Floating Action Button would get pushed up.

I solved this using the property:

resizeToAvoidBottomPadding: false, // fluter 1.x
resizeToAvoidBottomInset: false // fluter 2.x

On the parent Scaffold.

I tested it with your code, it solves the issue as well.

like image 154
Kevin Van Den Broek Avatar answered Oct 14 '22 11:10

Kevin Van Den Broek


You can check if the keyboard is show up or not, and based on that create the floating button or not.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: keyboardIsOpened ? 
          null ://or an empty container
          FloatingActionButton(
          tooltip: 'Add Item',
          child: Icon(Icons.add),
          backgroundColor: Colors.red,
          onPressed: _showFormDialog,
      ),
    );
  }

Inside the build method you can know if the keyboard show up by using MediaQuery.of(context).viewInsets.bottom and save its value on a bool variable keyboardIsOpened like the following code.

@override
Widget build(BuildContext context) {
  bool keyboardIsOpened = MediaQuery.of(context).viewInsets.bottom != 0.0;
like image 31
Abdelazeem Kuratem Avatar answered Oct 14 '22 10:10

Abdelazeem Kuratem


Used MediaQuery and Visibility

Visibility(
          visible: MediaQuery.of(context).viewInsets.bottom == 0.0,
          child: FloatingActionButton(
            onPressed: () {},
            child: Icon(Icons.chat),
          ),
        ),

When the keyboard is opened, the bottom will not be zero, which will cause fab to get invisible.

like image 25
BIS Tech Avatar answered Oct 14 '22 12:10

BIS Tech


Wrap your complete code in this

 new Container(
child: Column(
children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
            child: Stack(
              children: <Widget>[
              // Your body code
              ] // Widget
             ), // Stack
            ), // SingleChildScrollView
           ), // Expanded
        Container(
        alignment: Alignment.bottomCenter,
        padding: EdgeInsets.all(10.0),
        child : 
        // button code here 
        // to make button full width use minWidth: double.infinity,
         ,
        ), //Container
        ], // Widget
        ), // Column
        ), // Container
like image 26
Harsh Kashyap Avatar answered Oct 14 '22 12:10

Harsh Kashyap