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

bbuxkriu  于 2024-01-03  发布在  Flutter
关注(0)|答案(7)|浏览(239)

我创建了一个带有一个浮动操作按钮的计数器应用程序。
如果我想再添加一个按钮来重置计数器,我可以在底部栏的哪里添加第二个浮动操作按钮?
另外,我必须在void部分添加任何方法,或者是否有任何可用的重置计数器功能?

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: 'Counter App',
  8. theme: ThemeData(
  9. primarySwatch: Colors.blue,
  10. ),
  11. home: MyHomePage(title: 'Counter App'),
  12. );
  13. }
  14. }
  15. class MyHomePage extends StatefulWidget {
  16. MyHomePage({Key key, this.title}) : super(key: key);
  17. final String title;
  18. @override
  19. _MyHomePageState createState() => _MyHomePageState();
  20. }
  21. class _MyHomePageState extends State<MyHomePage> {
  22. int _counter = 0;
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. appBar: AppBar(
  27. title: Text(widget.title),
  28. ),
  29. body: Center(
  30. child: Text('You have pressed the button $_counter times.'),
  31. ),
  32. bottomNavigationBar: BottomAppBar(
  33. child: Container(
  34. height: 50.0,
  35. ),
  36. ),
  37. floatingActionButton: FloatingActionButton(
  38. onPressed: () => setState(() {
  39. _counter++;
  40. }),
  41. tooltip: 'Increment Counter',
  42. child: Icon(Icons.add),
  43. ),
  44. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  45. );
  46. }
  47. }

字符串

bttbmeg0

bttbmeg01#

Scaffold widget上的floatingActionButton属性不一定需要接受FloatingActionButton widget。它也可以接受ColumnRow widget。
下面,我将分享我的Scaffold小部件示例,其中两个浮动操作按钮彼此重叠。

  1. return Scaffold(
  2. appBar: AppBar(
  3. title: Text(""),
  4. ),
  5. body: SingleChildScrollView(/*...*/),
  6. floatingActionButton: Column(
  7. mainAxisAlignment: MainAxisAlignment.end,
  8. children: [
  9. FloatingActionButton(
  10. child: Icon(
  11. Icons.delete
  12. ),
  13. onPressed: () {
  14. //...
  15. },
  16. heroTag: null,
  17. ),
  18. SizedBox(
  19. height: 10,
  20. ),
  21. FloatingActionButton(
  22. child: Icon(
  23. Icons.star
  24. ),
  25. onPressed: () => _someFunc(),
  26. heroTag: null,
  27. )
  28. ]
  29. )
  30. );

字符串

展开查看全部
llycmphe

llycmphe2#

您可以使用flutter_speed_dial包:https://pub.dartlang.org/packages/flutter_speed_dial
在上面的链接有一个例子,展示了如何使用它。你必须使用SpeedDial类和children[]上,你可以添加一些按钮与SpeedDialChild。下面的例子显示了2个FAB。
使用它的示例:

  1. Widget _getFAB() {
  2. return SpeedDial(
  3. animatedIcon: AnimatedIcons.menu_close,
  4. animatedIconTheme: IconThemeData(size: 22),
  5. backgroundColor: Color(0xFF801E48),
  6. visible: true,
  7. curve: Curves.bounceIn,
  8. children: [
  9. // FAB 1
  10. SpeedDialChild(
  11. child: Icon(Icons.assignment_turned_in),
  12. backgroundColor: Color(0xFF801E48),
  13. onTap: () { /* do anything */ },
  14. label: 'Button 1',
  15. labelStyle: TextStyle(
  16. fontWeight: FontWeight.w500,
  17. color: Colors.white,
  18. fontSize: 16.0),
  19. labelBackgroundColor: Color(0xFF801E48)),
  20. // FAB 2
  21. SpeedDialChild(
  22. child: Icon(Icons.assignment_turned_in),
  23. backgroundColor: Color(0xFF801E48),
  24. onTap: () {
  25. setState(() {
  26. _counter = 0;
  27. });
  28. },
  29. label: 'Button 2',
  30. labelStyle: TextStyle(
  31. fontWeight: FontWeight.w500,
  32. color: Colors.white,
  33. fontSize: 16.0),
  34. labelBackgroundColor: Color(0xFF801E48))
  35. ],
  36. );
  37. }

字符串

结果:


的数据

展开查看全部
ha5z0ras

ha5z0ras3#

是的,它的工作..!

  1. floatingActionButton: Row(
  2. mainAxisAlignment: MainAxisAlignment.end,
  3. children: [
  4. FloatingActionButton(
  5. onPressed: () => {},
  6. child: Icon(Icons.navigate_before_rounded),
  7. heroTag: "fab1",
  8. ),
  9. FloatingActionButton(
  10. onPressed: () => {},
  11. child: Icon(Icons.navigate_next_rounded),
  12. heroTag: "fab2",
  13. ),
  14. ]
  15. )

字符串
x1c 0d1x的数据

展开查看全部
inkz8wg9

inkz8wg94#

根据medium post
您可以使用列(垂直对齐)或行部件(水平对齐)与2 FAB作为孩子,只是设置英雄标签空或分配不同的英雄标签。

z0qdvdin

z0qdvdin5#

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

  1. Stack(
  2. children: <Widget>[
  3. Align(
  4. alignment: Alignment.bottomLeft,
  5. child: FloatingActionButton(
  6. heroTag: null,
  7. ...),
  8. ),
  9. Align(
  10. alignment: Alignment.bottomRight,
  11. child: FloatingActionButton(
  12. heroTag: null,
  13. ...),
  14. ),
  15. ],
  16. )

字符串

展开查看全部
guykilcj

guykilcj6#

我用这个修复了它,也可以在按钮之间添加空间,你可以添加一个宽度和“英雄”标签是非常重要的。

  1. floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
  2. floatingActionButton: Padding(
  3. padding: const EdgeInsets.all(8.0),
  4. child: Row(
  5. mainAxisAlignment: MainAxisAlignment.center,
  6. children: <Widget>[
  7. FloatingActionButton(
  8. backgroundColor: Colors.green,
  9. heroTag: "btn",
  10. onPressed: () => _speak(textEditingController.text),
  11. child: Icon(Icons.play_arrow),
  12. ),
  13. SizedBox(
  14. width: 40,
  15. ),
  16. FloatingActionButton(
  17. backgroundColor: Colors.red,
  18. heroTag: "btn2",
  19. onPressed: () => _stop(),
  20. child: Icon(Icons.stop),
  21. )
  22. ],
  23. ),
  24. )

字符串
enter image description here

展开查看全部
plupiseo

plupiseo7#

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

  1. class RoundIconButton extends StatelessWidget {
  2. const RoundIconButton({Key? key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return RawMaterialButton(
  6. constraints: BoxConstraints(minHeight: 40, minWidth: 40),
  7. shape: CircleBorder(),
  8. fillColor: Colors.white,
  9. onPressed: () {},
  10. child: Text("+"),
  11. );
  12. }
  13. }
  14. class Fab extends StatelessWidget {
  15. const Fab({Key? key}) : super(key: key);
  16. @override
  17. Widget build(BuildContext context) {
  18. return Column(
  19. children: [
  20. RawMaterialButton(),
  21. RawMaterialButton(),
  22. ],
  23. );
  24. }
  25. }

字符串

展开查看全部

相关问题