Flutter中使用FloatingActionButton在不同页面上调用方法
我正在尝试使用另一个页面上的浮动操作按钮来调用方法(在不同页面上定义)。
FloatingActionButton
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(
Icons.add,
color: Colors.white70,
),
splashColor: Colors.blueGrey,
backgroundColor: Colors.blueAccent,
),
我需要调用的方法在
void _showForm(int? id) async {}
_showForm 内部返回 showModalBottomSheet
I'm trying to call a method(defined on a different page), using the Floating Action Button from another page.
FloatingActionButton
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(
Icons.add,
color: Colors.white70,
),
splashColor: Colors.blueGrey,
backgroundColor: Colors.blueAccent,
),
The method which I need to call is inside
void _showForm(int? id) async {}
_showForm returns showModalBottomSheet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在单独的 .dart 文件中或在任何其他类之外设置该方法。请记住在参数中包含 BuildContext:
import 'package:flutter/material.dart';
现在,导入浮动操作按钮所在的 .dart 文件,并在 onPressed 回调中调用该方法:
Set up the method in a separate .dart file or outside any other class. Remember to include the BuildContext in the arguments:
import 'package:flutter/material.dart';
Now, import the .dart file where your floating action button is and call the method in the onPressed callback:
只需将该功能传递到您的 FAB(浮动操作按钮)页面即可。
您可以在此处查看示例:将方法作为参数传递给小部件
Just pass the function to your FAB (Floating Action Button) Page.
You can see an example here: Pass method as parameter to a widget