Flutter:如何将FloatingActionButton附加到AppBar?

时间:2018-04-26 08:14:54

标签: flutter floating-action-button appbar

Scaffold - 小工具只允许在右下角或底部中心放置FloatingActionButton。如何将它放在AppBarbody之间,就像这里一样?

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

3 个答案:

答案 0 :(得分:6)

现在可以使用:

Scaffold(
      appBar: AppBar(title: Text('Title'),),
      body: ...,
      floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
      floatingActionButton: FloatingActionButton(...),
    ),

答案 1 :(得分:4)

Siva Kumar 给出了很好的例子,但FAB的第二个不可点击。所以我在代码中做了一些更改,现在效果很好!

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,
        )
      ],
    );
  }
}

答案 2 :(得分:3)

我们可以通过使用堆栈来实现它

和代码段看起来像这样

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]}