Flutter中使用FloatingActionButton在不同页面上调用方法

发布于 2025-01-14 10:56:27 字数 459 浏览 3 评论 0原文

我正在尝试使用另一个页面上的浮动操作按钮来调用方法(在不同页面上定义)。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一人独醉 2025-01-21 10:56:27

在单独的 .dart 文件中或在任何其他类之外设置该方法。请记住在参数中包含 BuildContext:

import 'package:flutter/material.dart';

showForm(int id, BuildContext ctx) {
  return showModalBottomSheet(
    context: ctx,
    builder: (ctx) => BottomSheet(
      onClosing: () {},
      builder: (ctx) => Container(
        height: 200,
        child: Center(
          child: Text('Hello, there!'),
        ),
      ),
    ),
  );
}

现在,导入浮动操作按钮所在的 .dart 文件,并在 onPressed 回调中调用该方法:

 floatingActionButton: FloatingActionButton(
    onPressed: () {
      showForm(1, context);
    },
    child: Icon(
      Icons.add,
      color: Colors.white70,
    ),
    splashColor: Colors.blueGrey,
    backgroundColor: Colors.blueAccent,
  ),

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';

showForm(int id, BuildContext ctx) {
  return showModalBottomSheet(
    context: ctx,
    builder: (ctx) => BottomSheet(
      onClosing: () {},
      builder: (ctx) => Container(
        height: 200,
        child: Center(
          child: Text('Hello, there!'),
        ),
      ),
    ),
  );
}

Now, import the .dart file where your floating action button is and call the method in the onPressed callback:

 floatingActionButton: FloatingActionButton(
    onPressed: () {
      showForm(1, context);
    },
    child: Icon(
      Icons.add,
      color: Colors.white70,
    ),
    splashColor: Colors.blueGrey,
    backgroundColor: Colors.blueAccent,
  ),
孤单情人 2025-01-21 10:56:27

只需将该功能传递到您的 FAB(浮动操作按钮)页面即可。
您可以在此处查看示例:将方法作为参数传递给小部件

  • 顺便说一下,voidCallBack 只是一个好听的词,而不是“void”类型。

Just pass the function to your FAB (Floating Action Button) Page.
You can see an example here: Pass method as parameter to a widget

  • And by the way, voidCallBack is just a nice word instead of "void" type.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文