【问题标题】:Flutter: How to attach a FloatingActionButton to the AppBar?Flutter:如何将 FloatingActionButton 附加到 AppBar?
【发布时间】:2018-10-06 20:40:48
【问题描述】:

Scaffold-Widget 只允许在右下角或底部中心放置FloatingActionButton。我怎样才能把它放在AppBarbody 之间,就像这里一样?

[

【问题讨论】:

标签: flutter floating-action-button appbar


【解决方案1】:

现在可以使用:

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

【讨论】:

    【解决方案2】:

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

    和代码 sn-p 看起来像这样

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

    【讨论】:

      【解决方案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,
              )
            ],
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-02-04
        • 2020-11-23
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        • 2019-03-28
        • 1970-01-01
        相关资源
        最近更新 更多