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

flu*_*alk 6 dart flutter flutter-test flutter-layout

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

如果我想再增加一个按钮来重置计数器,我该在底部栏中添加第二个浮动操作按钮吗?

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

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,
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

RaZ*_*zLe 47

floatingActionButtonScaffold小部件上的属性不一定需要带FloatingActionButton小部件。它也可以带ColumnRow小部件。

下面,我将分享我的 Scaffold 小部件示例,其中两个浮动操作按钮位于彼此之上。

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,
      )
    ]
  )
);
Run Code Online (Sandbox Code Playgroud)

  • `heroTag` 是应用于按钮的 `Hero` 小部件的标签。默认情况下,Flutter 假定每个屏幕应该只有一个“FloatingActionButton”,如果在屏幕上使用多个 FloatingActionButton,则必须显式设置 HeroTags。在上面的示例中,我将它们设置为“null”,因为我没有在这些按钮上使用任何动画。如果您使用动画,您可以设置类似 `heroTag: "heroTag1"` 的内容,并在另一个 FloatingActionButton 上设置类似 `heroTag: "heroTag2"` 的内容。有关更多详细信息,请查看此链接:https://api.flutter.dev/flutter/material/FloatingActionButton-class.html (3认同)

Sac*_*lal 8

是的,它成功了..!

      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",
          ),
        ]
      )
Run Code Online (Sandbox Code Playgroud)

右下角有两个 FloatingActionButton


Aug*_*sto 7

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

在上面的链接上有一个示例,显示了如何使用它。您必须使用SpeedDialclass,并且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))
          ],
        );
  }
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明

  • 是否可以在屏幕右下角水平对齐 2 个 FAB,而不需要快速拨号小部件的展开功能?(即永久显示两个 FAB) (4认同)

Ko *_*ynn 6

媒体报道

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


Nai*_*hhi 6

您可以通过设置“heroTag:null”来实现,如下所示:

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