How can I move floating action button to the left side of the Screen in Flutter?

13,342

Solution 1

Basically suggested by @E.Bradford in the comment above, but I wanted to provide some code, if needed. You can place the FloatingActionButton pretty much anywhere you want by stacking it into a positioned widget.

Stack(
  children: [
    Placeholder(),
    Positioned(
      left: 40,
      bottom: 40,
      child: FloatingActionButton(
        onPressed: () {},
      ),
    ),
  ],
)

enter image description here

Solution 2

You can just add into your Scaffold the named constructor:

floatingActionButtonLocation: FloatingActionButtonLocation.startFloat

or

floatingActionButtonLocation: FloatingActionButtonLocation.startDocked

Take the getting started app for example:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    //##########################################################################
       floatingActionButtonLocation: FloatingActionButtonLocation.startFloat,
    //##########################################################################
/*
or

`floatingActionButtonLocation: FloatingActionButtonLocation.startDocked` 
*/
    );
  }
}

Solution 3

I believe this previously provided answer provides the closest "Easy to Implement" set of options to your problem.

https://stackoverflow.com/a/49761700/10768398

The answer basically says to use something similar to the following on your Scaffold component of your Widget tree:

, floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat

Based on the descriptions of the options it doesn't look like there is an align to the left.

Also take a look at the available options here:

https://api.flutter.dev/flutter/material/FloatingActionButtonLocation-class.html

Solution 4

I used following code to make the floatingActionButton on left side by using padding on right side of FAB.

floatingActionButton: Padding(
    padding: const EdgeInsets.only(right: 275.0),
    child: FloatingActionButton(
      child: Icon(
        Icons.add,
        color: Colors.black,
      ),
      backgroundColor: Colors.orange,
      onPressed: (){},
    ),
  ),

Solution 5

This will place your FAB in the lower lefthand corner:

 @override
 Widget build(BuildContext context) {
   return Scaffold(
      floatingActionButtonLocation: FloatingActionButtonLocation.startDocked,
      floatingActionButton:
          FloatingActionButton(heroTag: null, onPressed: () {}),
   );

See https://api.flutter.dev/flutter/material/FloatingActionButtonLocation/startDocked-constant.html

Share:
13,342
Jay Tillu
Author by

Jay Tillu

|| यतो धर्मस्ततो जयः || 🌍 त्वदीयं वस्तु गोविन्द: तुभ्यमेव समर्पये 🇮🇳 🚩 Medium of the Divine 🚩 💻 Code for People not for Profit 💵

Updated on July 17, 2022

Comments

  • Jay Tillu
    Jay Tillu almost 2 years

    By default floating action button is at the right side of the screen. I know about the floatingActionButtonLocation and its properties like endDocked, startDocked, centerDocked, but none of them helped me in moving it to the left side of the screen.

  • Jay Tillu
    Jay Tillu almost 5 years
    ya, I saw it. But it suggests for how to implement a floating action button on Center. And I want it to left side of the screen. But thanks for respond.
  • E.Bradford
    E.Bradford almost 5 years
    I added a bit to the answer, I don't think there is an align left option
  • Jay Tillu
    Jay Tillu almost 5 years
    Yes Sir, I just noticed your edit. I totally agree that by default there is not a left alignment option.
  • E.Bradford
    E.Bradford almost 5 years
    Just a thought, but would a Stack object (with an array of widgets In the stack) and with one of the children having a Positioned widget (your desired button) work in your case?
  • Jay Tillu
    Jay Tillu almost 5 years
    I didn't try it yet. But as you suggest I'll try it as well.
  • E.Bradford
    E.Bradford almost 5 years
    It might qualify as a solution if you do not require scrolling on that particular screen. Scrolling would scroll the button completely off the screen.
  • Saar
    Saar over 3 years
    I suggest to avoid this technique as you have different screen sizes and it the feeling will be different across devices.
  • Ian Smith
    Ian Smith over 3 years
    Is there a way to add padding or a margin to the docked button so it's not as close to the edge of the screen?