0

I am trying to add the flaoting action button in my flutter application and since only one child can be implemented I cannot figure out where should I put the code for the floating action button.

I can put it in the column as children but then it will not appear at the bottom left corner of the screen. I also tried creating another class and then putting it inside the scaffold widget but then my _favCity won't be included

....
class _favCity extends State<favCity> {
  String city1, city2;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Email Login"),
        centerTitle: true,
      ),
      backgroundColor: Colors.cyan,
      body: Container(
        margin: EdgeInsets.all(40.0),
        color: Colors.brown,
        child: Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(10.0),
              //color: Colors.white,
              child: TextField(
                decoration: new InputDecoration(
                  labelText: "Enter City 1",
                  fillColor: Colors.orange,
                  border: new OutlineInputBorder(
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                ),
                onSubmitted: (String userInput) {
                  setState(() {
                    city1 = userInput;
                  });
                },
              ),
            ),
            Container(
              padding: EdgeInsets.all(10.0),
             // color: Colors.white,
              child: TextField(
                decoration: InputDecoration(
                  labelText: "Enter city 2",
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                  fillColor: Colors.orange,
                ),
                onChanged: (String UserInput) {
                  setState(() {
                    city2 = UserInput;
                  });
                },
              ),
            ),
            Text("$city1 Is better than $city2"),

          ],
        ),

      ),
    );
  }
}
Ojasv Singh
  • 41
  • 1
  • 6

3 Answers3

1

A floating action button is also a widget and can be placed in an app using the below code.

import 'package:flutter/material.dart';

class FAButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('FAB Button Example'),
      ),
      body: Center(
        child: Text('Floating Action Button Screen'),
      ),
      //FAB is a property of the `Scaffold` Widget
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        //Widget to display inside Floating Action Button, can be `Text`, `Icon` or any widget.
        onPressed: () {
          //Code to execute when Floating Action Button is clicked
          //...
        },
      ),
    );
  }
}

For more Reference to the FAB or to further Customize it see this Link

Mukul
  • 1,020
  • 6
  • 12
0

For floatingActionButton you can see this document .

I use it in this way with no problems:

          floatingActionButton: FloatingActionButton(
            backgroundColor: Theme.of(context).primaryColor,
            child: Icon(
              Icons.comment,
              color: Colors.white,
            ),
            onPressed: () {
              // do something
            },
          ),
0

To add the FloatingActionButton in your Scaffold, you need use this:

@override
Widget build(BuildContext context) {
    return Scaffold(
        body: Container() // something you want to put in the body
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.check),
            onPressed: () { // ... function you want use when on pressed }
            backgroundColor: Color(0xFFE57373),
        ),
        // if you want change position of floating action button is here
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
}