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

7

9

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.

Salinasalinas answered 25/7, 2019 at 17:38 Comment(0)
E
3

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

Emma answered 10/8, 2020 at 4:25 Comment(0)
B
11

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` 
*/
    );
  }
}
Budge answered 2/3, 2021 at 10:48 Comment(0)
B
7

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

https://mcmap.net/q/274215/-flutter-floatingactionbutton-in-the-center

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

Benevolence answered 25/7, 2019 at 18:0 Comment(6)
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.Salinasalinas
I added a bit to the answer, I don't think there is an align left optionBenevolence
Yes Sir, I just noticed your edit. I totally agree that by default there is not a left alignment option.Salinasalinas
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?Benevolence
I didn't try it yet. But as you suggest I'll try it as well.Salinasalinas
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.Benevolence
M
6

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

Marder answered 20/11, 2020 at 11:8 Comment(1)
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?Farrell
R
3

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: (){},
    ),
  ),
Retroversion answered 23/12, 2019 at 15:21 Comment(1)
I suggest to avoid this technique as you have different screen sizes and it the feeling will be different across devices.Uxorious
E
3

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

Emma answered 10/8, 2020 at 4:25 Comment(0)
A
2

You can wrap your floatingActionButton with Row() and give crossAxisAlignment to center:

floatingActionButton: Row(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    Padding(
      padding: const EdgeInsets.only(left:25.0),
      child: FloatingActionButton(
        onPressed: () {
          // Add your onPressed code here!
        },
        child: Icon(Icons.message,color: Colors.white,),
        backgroundColor: Colors.green,
      ),
    ),
  ],
),
Airflow answered 18/9, 2020 at 19:46 Comment(0)
H
0

You can use this code

floatingActionButton: Align(
            alignment: Alignment.centerLeft,
            child: FloatingActionButton(

              onPressed: (){},
            ),
          ),
Haemolysin answered 7/10, 2023 at 7:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.