Search code examples
flutterkeyboardfloating-action-button

Prevent keyboard coming up when floating action button is pressed


I have a row with a text field and a floating action button. Everytime i push the floating action button the text field is activated and the keyboard comes up. How can I prevent this behaviour? The floating action button triggers showing the device location on a map and the text field is for location search if you don´t allow location service or want to search on other places.

Container(
                padding: const EdgeInsets.only(left: 10, right: 10),
                color: Colors.white,
// Text field for search
                child: Row(
                  children: [
                    TextField(
                      style: GoogleFonts.juliusSansOne(
                          fontSize: 14, color: Colors.black),
                      controller: controllerTXT,
// new camera position depending on user input
                      onSubmitted: (inputText) async {
                        LatLng? searchPosition = await getLocationLine();
                        if (searchPosition != null) {
                          searchInputPosition(CameraPosition(
                            target: searchPosition,
                            zoom: 9.151926040649414,
                          ));
                          setState(() {
                            position = searchPosition;
                          });
                        }
                      },
                      textInputAction: TextInputAction.go,
                      decoration: InputDecoration(
                        filled: true,
                        fillColor: Colors.white,
                        hintText: "PLZ/Ort",
                        border: InputBorder.none,
                        contentPadding: EdgeInsets.only(left: 20.0, top: 16.0),
                        icon: Container(
                          width: 10,
                          height: 30,
                          child: Icon(
                            Icons.search,
                            color: Colors.black,
                            size: 36,
                          ),
                        ),
                      ),
                    ),
                    SizedBox(
                      width: 16,
                    ),
                    Padding(
                      padding: const EdgeInsets.fromLTRB(
                        8.0,
                        8.0,
                        0,
                        8.0,
                      ),
                      child: SizedBox(
                        width: 30,
                        height: 30,
// my Location Button
                        child: FloatingActionButton(
                          onPressed: _myPosition,
                          materialTapTargetSize: MaterialTapTargetSize.padded,
                          backgroundColor: Color.fromRGBO(180, 24, 32, 1),
                          child: Icon(Icons.my_location_rounded, size: 28.0),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      );

Solution

  • I found the solution and it was my stupid mistake: I had the same code in an if/else for MediaQuery and forgotten to update it. In this else tree the floatingActionButton had been declared as suffixIcon instead of being part of a row.