如何在 Flutter 中制作两个浮动操作按钮?

How to Make Two Floating Action Button in Flutter?

创建了带有一个浮动操作按钮的计数器应用程序。

如果我想再添加一个按钮来重置计数器,我可以在底部栏的哪里添加第二个浮动操作按钮?

还有我必须在无效部分添加任何方法,或者是否有可用的重置计数器功能?

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Counter App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Counter App'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text('You have pressed the button $_counter times.'),
      ),
      bottomNavigationBar: BottomAppBar(
        child: Container(
          height: 50.0,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => setState(() {
          _counter++;
            }),
        tooltip: 'Increment Counter',
        child: Icon(Icons.add),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}

您可以使用 flutter_speed_dial 包:https://pub.dartlang.org/packages/flutter_speed_dial

在上面的 link 上有一个例子展示了如何使用它。您必须使用 SpeedDial class 并且在 children[] 上您可以使用 SpeedDialChild 添加一些按钮。下面的示例显示了 2 个 FAB。

使用示例:

Widget _getFAB() {
        return SpeedDial(
          animatedIcon: AnimatedIcons.menu_close,
          animatedIconTheme: IconThemeData(size: 22),
          backgroundColor: Color(0xFF801E48),
          visible: true,
          curve: Curves.bounceIn,
          children: [
                // FAB 1
                SpeedDialChild(
                child: Icon(Icons.assignment_turned_in),
                backgroundColor: Color(0xFF801E48),
                onTap: () { /* do anything */ },
                label: 'Button 1',
                labelStyle: TextStyle(
                    fontWeight: FontWeight.w500,
                    color: Colors.white,
                    fontSize: 16.0),
                labelBackgroundColor: Color(0xFF801E48)),
                // FAB 2
                SpeedDialChild(
                child: Icon(Icons.assignment_turned_in),
                backgroundColor: Color(0xFF801E48),
                onTap: () {
                   setState(() {
                      _counter = 0;
                   });
                },
                label: 'Button 2',
                labelStyle: TextStyle(
                    fontWeight: FontWeight.w500,
                    color: Colors.white,
                    fontSize: 16.0),
                labelBackgroundColor: Color(0xFF801E48))
          ],
        );
  }

结果:

根据medium post

您可以使用 Column(用于垂直对齐)或 Row 小部件(用于水平对齐),将 2 个 FAB 作为子项,只需将 hero Tag 设置为 null 或分配不同的 HeroTag。

floatingActionButton 属性 on Scaffold widget 不一定需要带 FloatingActionButton widget。它还可以使用 ColumnRow 个小部件。

下面,我将分享我的脚手架小部件示例,其中有两个浮动操作按钮彼此重叠。

return Scaffold(
  appBar: AppBar(
    title: Text(""),
  ),
  body: SingleChildScrollView(/*...*/),
  floatingActionButton: Column(
    mainAxisAlignment: MainAxisAlignment.end,
    children: [
      FloatingActionButton(
        child: Icon(
          Icons.delete
        ),
        onPressed: () {
          //...
        },
        heroTag: null,
      ),
      SizedBox(
        height: 10,
      ),
      FloatingActionButton(           
        child: Icon(
          Icons.star
        ),
        onPressed: () => _someFunc(),
        heroTag: null,
      )
    ]
  )
);

您可以通过如下设置"heroTag: null"来实现:

Stack(
  children: <Widget>[
    Align(
      alignment: Alignment.bottomLeft,
      child: FloatingActionButton(
                heroTag: null,
             ...),
    ),
    Align(
      alignment: Alignment.bottomRight,
      child: FloatingActionButton(
                heroTag: null,
             ...),
    ),
  ],
)

我用这个修复了它,还要在按钮之间添加 space 你可以添加宽度,'hero' 标签非常重要。

floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
        floatingActionButton: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              FloatingActionButton(
                backgroundColor: Colors.green,
                heroTag: "btn",
                onPressed: () => _speak(textEditingController.text),
                child: Icon(Icons.play_arrow),
              ),
              SizedBox(
                width: 40,
              ),
              FloatingActionButton(
                backgroundColor: Colors.red,
                heroTag: "btn2",
                onPressed: () => _stop(),
                child: Icon(Icons.stop),
              )
            ],
          ),
        )

enter image description here

是的,成功了..!

      floatingActionButton: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(
            onPressed: () => {},
            child: Icon(Icons.navigate_before_rounded),
            heroTag: "fab1",
          ),
          FloatingActionButton(
            onPressed: () => {},
            child: Icon(Icons.navigate_next_rounded),
            heroTag: "fab2",
          ),
        ]
      )

在 Flutter 文档中,我们最多可以在单个屏幕上使用一个浮动操作按钮。我们可以使用 RawMaterialButton() 小部件来完成。浮动操作按钮的这个小部件父小部件

是这样的

  class RoundIconButton extends StatelessWidget {
      const RoundIconButton({Key? key}) : super(key: key);

      @override
      Widget build(BuildContext context) {
        return RawMaterialButton(
          constraints: BoxConstraints(minHeight: 40, minWidth: 40),
          shape: CircleBorder(),
          fillColor: Colors.white,
          onPressed: () {},
          child: Text("+"),
        );
      }
    }
class Fab extends StatelessWidget {
  const Fab({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        RawMaterialButton(),
        RawMaterialButton(),
      ],
    );
  }
}