为什么floatingActionButton不能与flutter一起使用?

svdrlsy4  于 2022-12-05  发布在  Flutter
关注(0)|答案(1)|浏览(129)

我必须在FloatingActionButton上运行一个动作。为了测试它是否工作或按下正确,我只是在onPressed()中创建了一个简单的打印,但在终端中什么也没有。这是我的代码:

@override
  Widget buildView() {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Find Devices'),
      ),
      // body:
      floatingActionButton: FloatingActionButton(
        onPressed: () =>
            print("Scan Button"),
        backgroundColor: Colors.blue,
        child: const Icon(Icons.search),
      ),
    );
  }
xytpbqjk

xytpbqjk1#

这看起来像是在使用StatefulWidget中的buildView方法。为了使FloatingActionButton真正显示在屏幕上,您需要从StatefulWidgetbuild方法而不是buildView中返回它。
请尝试修改代码,如下所示:

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: const Text('Find Devices'),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: () =>
          print("Scan Button"),
      backgroundColor: Colors.blue,
      child: const Icon(Icons.search),
    ),
  );
}

相关问题