我试图调用一个方法(定义在另一个页面上),使用来自另一个页面的浮动操作按钮。
FloatingActionButton
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(
Icons.add,
color: Colors.white70,
),
splashColor: Colors.blueGrey,
backgroundColor: Colors.blueAccent,
),
我需要调用的方法在
void _showForm(int? id) async {}
_showForm返回showModalBottomSheet
发布于 2022-03-15 11:48:12
在单独的.dart文件或任何其他类之外设置该方法。记住在参数中包括BuildContext:
进口‘包装:颤振/材料。飞镖’;
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,
),
发布于 2022-03-15 12:06:23
只需将函数传递到FAB (浮动动作按钮)页面即可。您可以在这里看到一个例子:Pass method as parameter to a widget
https://stackoverflow.com/questions/71487918
复制相似问题