Flutter: How to attach a FloatingActionButton to the AppBar?
Asked Answered
P

4

9

The Scaffold-Widget only allows to place a FloatingActionButton at the bottom right or the bottom center. How can I place it between AppBar and body like here?

[A floating action button attached to the app bar[1]

Prismoid answered 26/4, 2018 at 8:14 Comment(2)
I guess it is not available as of now. github.com/flutter/flutter/issues/227Bellbella
hi @Prismoid try with my answer, let me know is it working or notWoodcut
J
13

It is now possible using:

Scaffold(
      appBar: AppBar(title: Text('Title'),),
      body: ...,
      floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
      floatingActionButton: FloatingActionButton(...),
    ),
Juggernaut answered 16/1, 2020 at 9:55 Comment(0)
S
6

Siva Kumar gave nice example, but the second have of the FAB was not clickable. So I made some changes in the code and it works great now!

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  double appBarHeight = 200.0;

  @override
  Widget build(BuildContext context) {
    return new Stack(
      children: <Widget>[
        new Scaffold(
          appBar: new PreferredSize(
            preferredSize: new Size(MediaQuery.of(context).size.width, appBarHeight),
            child: new Container(
              color: Colors.blue,
              child: new Container(
                  margin:const EdgeInsets.only(top: 30.0),
                  child: new Column(children: <Widget>[
                    new Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        new Row(
                          children: <Widget>[
                            new IconButton(
                                icon: new Icon(
                                  Icons.arrow_back,
                                  color: Colors.white,
                                ),
                                onPressed: () {
                                  Navigator.pop(context, false);
                                }
                            ),
                            new Text(
                              "Controller Name",
                              style: new TextStyle(
                                  fontWeight: FontWeight.bold,
                                  color: Colors.white
                              ),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ]
                  )
              )
            )
          ),
          body: new Center(
            child: new Text('Hello!'),
          ),
        ),
        new Positioned(
          child: new FloatingActionButton(
            child: new Icon(Icons.add),
            onPressed: () {
              print('FAB tapped!');
            },
            backgroundColor: Colors.blueGrey,
          ),
          right: 10.0,
          top: appBarHeight - 5.0,
        )
      ],
    );
  }
}
Sling answered 26/4, 2018 at 11:6 Comment(0)
W
3

we can Achieve it by using stack

and code snippet look like this

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
  title: 'Flutter Demo',
  theme: new ThemeData(

    primarySwatch: Colors.blue,
  ),
  home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
 }
 }

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

  final String title;

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

 class _MyHomePageState extends State<MyHomePage> {




  @override
 Widget build(BuildContext context) {


return new Scaffold(
  appBar:  new PreferredSize(
    preferredSize: new Size(MediaQuery.of(context).size.width, 200.0),
    child:
   new Stack(
     alignment: const FractionalOffset(0.98, 1.12),
     children: <Widget>[new Container(
       color: Colors.blue,

       child: new Column(
         children: <Widget>[
           new Container(
               margin: const EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 0.0),
               child: new Column(children: <Widget>[
                 new Row(
                   mainAxisAlignment: MainAxisAlignment.spaceBetween,
                   children: <Widget>[
                     new Row(
                       children: <Widget>[
                         new IconButton(
                             icon: new Icon(
                               Icons.arrow_back,
                               color: Colors.white,
                             ),
                             onPressed: () {
                               Navigator.pop(context, false);
                             }),
                         new Text(
                           "Controller Name",
                           style: new TextStyle(
                               fontWeight: FontWeight.bold,
                               color: Colors.white),
                         ),
                       ],
                     ),
                   ],
                 ),
               ]))
         ],
       )
   ),new FloatingActionButton(onPressed: (){print("floating button Tapped");},child: new Icon(Icons.add),)],)

  ),
  body: new Center(
    // Center is a layout widget. It takes a single child and positions it
    // in the middle of the parent.
    child:
        new Container(),


  ),

   );
  }
 [![output][1]][1]}
Woodcut answered 26/4, 2018 at 9:52 Comment(0)
E
0

for me the solution was, set floatingActionButtonLocation to FloatingActionButtonLocation.endTop

Scaffold(
floatingActionButtonLocation: FloatingActionButtonLocation.endTop)
Emmert answered 30/8, 2022 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.