简体   繁体   English

Position Flutter中的两个浮动动作按钮

[英]Position two floating action button in Flutter

How can I position multiple floating action button in Flutter.如何在 Flutter 中使用 position 多个浮动操作按钮。 In particular I need one button in the bottom-right of the screen and another one in the top left.特别是我需要屏幕右下角的一个按钮和左上角的另一个按钮。

Use Column and add two floating buttons inside it one is on the top left and the second one is on the bottom right使用 Column 并在其中添加两个浮动按钮,一个在左上角,第二个在右下角

       Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Align(
              alignment: Alignment.topLeft,
              child: FloatingActionButton(
                tooltip: 'Increment',
                child: Icon(Icons.add),
              ),
            ),
            Expanded( 
              child: Align(
                alignment: Alignment.bottomRight,
                child: FloatingActionButton(
                  tooltip: 'Increment',
                  child: Icon(Icons.add),
                ),
              ),
            )
          ],
        ),

Check the below code and screenshot may be this will help you:检查以下代码和屏幕截图可能会对您有所帮助:

在此处输入图像描述

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
              child: Container(
                width: MediaQuery.of(context).size.width,
                color: Colors.red,
                child: Align(
                  alignment: Alignment.topLeft,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      FloatingActionButton(onPressed: (){},child: Icon(Icons.add),),

                    ],
                  ),
                ),
              )
          ),
          Expanded(child: Container(
              width: MediaQuery.of(context).size.width,
              color: Colors.green,
              child: Text('Center',textAlign: TextAlign.center,)
          )),
          Expanded(
              child: Container(
                width: MediaQuery.of(context).size.width,
                color: Colors.red,
                child: Align(
                  alignment: Alignment.bottomRight,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      FloatingActionButton(onPressed: (){},child: Icon(Icons.add),),
                    ],
                  ),
                ),
              )
          ),

        ],
      ),
    );
  }

you can use the stack() widget with positioned() in order to achieve this, for example:您可以使用带有定位()stack()小部件来实现这一点,例如:

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Maps and Floating Buttons Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Maps and Floating Buttons Demo'),
    );
  }
}

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

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

class _MyHomePageState extends State<MyHomePage> {
  GoogleMapController mapController;
  var initialLocation =
      CameraPosition(target: LatLng(-27.56, -58.58), zoom: 12);

  void mapControllerCreated(GoogleMapController controller) {
    mapController = controller;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(children: <Widget>[
        GoogleMap(
          zoomControlsEnabled: false,
          zoomGesturesEnabled: true,
          mapToolbarEnabled: true,
          compassEnabled: false,
          myLocationButtonEnabled: true,
          initialCameraPosition: initialLocation,
          onMapCreated: mapControllerCreated,
          onCameraMove: (position) {},
          onCameraIdle: () {},
        ),
        Positioned(
            top: 50,
            right: 20,
            child: FloatingActionButton(
              child: Icon(Icons.textsms_sharp),
              backgroundColor: Colors.blue,
              heroTag: 1,
              onPressed: () {
                //do something on press
              },
            )),
        Positioned(
            top: 50,
            left: 20,
            child: FloatingActionButton(
              child: Icon(Icons.check),
              backgroundColor: Colors.blue,
              heroTag: 1,
              onPressed: () {
                //do something on press
              },
            )),
        Positioned(
          bottom: 50,
          left: 20,
          child: FloatingActionButton(
            child: Icon(Icons.search),
            backgroundColor: Colors.blue,
            heroTag: 1,
            onPressed: () {
              //do something on press
            },
          ),
        )
      ]),
    );
  }
}


here a screenshoot:这里有一个截图: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM