首页 文章

Flutter:如何将FloatingActionButton附加到AppBar?

提问于
浏览
1

Scaffold -Widget仅允许在右下角或底部中心放置 FloatingActionButton . 如何将它放在 AppBar 和_588061之间?

[
A floating action button attached to the app bar1

2 回答

  • 3

    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

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

    和代码片段看起来像这样

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

相关问题