2

I want to center the FloatingActionButton horizontally on the screen but i don't seem to find any resource on how to do it.. This is my flutter code..

  void main() => runApp(MaterialApp(
  home: Home()
));

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('My App'),
          centerTitle: true,
          backgroundColor: Colors.brown[700],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            // Add your onPressed code here!
          },
          child: Icon(Icons.add),
          backgroundColor: Colors.brown[700],
        ),
        backgroundColor: Colors.white70
    );
  }
}
Zulfiqar Ali
  • 269
  • 1
  • 3
  • 12

3 Answers3

2

Add this to your Scaffold:

floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat
Saman Salehi
  • 1,995
  • 1
  • 22
  • 36
1

Try this:

floatingActionButton: Center(
          child: FloatingActionButton(
            onPressed: () {
              // Add your onPressed code here!
            },
            child: Icon(Icons.add),
            backgroundColor: Colors.brown[700],
          ),
        ),

Result

Result

Sandeep Sharma
  • 639
  • 2
  • 9
  • 34
  • I knew how to do this but this way the `FloatingActionButton` gets centered both vertically and horizontally, i just wanted to center it horizontally downwards, `floatingActionButtonLocation` property worked for me. – Zulfiqar Ali Apr 26 '20 at 17:04
-1

So this is the floatingActionButton property of the Scaffold. You could put him into the body and then use a Center, or Column or Row widget to center it. Otherwise, just wrap the FloatingActionButton in a Row with mainaxisalignment: MainaxisAlignment.center. You can assign many widgets to the floatingActionButton property of the scaffold, not only FloatingActionButtons. Hope this helps.

Edit: it would then look like this:

Scaffold(
    floatingActionButton: Row(
         mainAxisAlignment: MainAxisAlignment.center,
         children: [
             FloatingActionButton()
          ],
    ),
),
If Clause
  • 67
  • 7