Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to attach a FloatingActionButton to the AppBar?

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]

like image 503
hendra Avatar asked Apr 26 '18 08:04

hendra


Video Answer


3 Answers

It is now possible using:

Scaffold(
      appBar: AppBar(title: Text('Title'),),
      body: ...,
      floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
      floatingActionButton: FloatingActionButton(...),
    ),
like image 105
Marco Avatar answered Sep 29 '22 20:09

Marco


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,
        )
      ],
    );
  }
}
like image 36
Arnold Parge Avatar answered Sep 29 '22 20:09

Arnold Parge


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]}
like image 41
siva kumar Avatar answered Sep 29 '22 19:09

siva kumar