我正在试着在点击floatingActionbutton的时候显示Snackbar分.但是当我点击floatingactionbutton的时候,它没有显示任何东西.这是我的代码.我用的是StatefulWidget.我调试并判断了onPressed函数也在执行,但不知何故看不到Snackbar.问题的根本原因是什么?我觉得我正在传递的BuildContext有一些问题.

class MyApp extends StatefulWidget{
  @override
  MyAppState createState() {
    // TODO: implement createState
    return new MyAppState();
  }

}

class MyAppState extends State<MyApp>{
  File _image;
  String _text;

  Future getImage() async {
    var image = await ImagePicker.pickImage(source: ImageSource.camera);
    _image = image;
    final FirebaseVisionImage visionImage = FirebaseVisionImage.fromFile(_image);
    final TextRecognizer textRecognizer = FirebaseVision.instance.textRecognizer();
    final VisionText visionText = await textRecognizer.processImage(visionImage);

    String detectedText = visionText.text;

    setState(() {
      _image = image;
      _text = detectedText;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: new AppBar(
          title: new Text('Image Picker Example'),
        ),
        body: new Center(
          child: _image == null
              ? new Text('No image selected.')
              : new Image.file(_image),
        ),
        floatingActionButton: new FloatingActionButton(
          onPressed: (){
            showSnackBar(context);
//            getImage();
          },
          tooltip: 'Pick Image',
          child: new Icon(Icons.add_a_photo),
        ),
      ),
    );
  }

  void showSnackBar(BuildContext context) {
    final scaffold = Scaffold.of(context);
    final snackBarContent = SnackBar(
      content: Text("sagar"),
      action: SnackBarAction(
          label: 'UNDO', onPressed: scaffold.hideCurrentSnackBar),
    );
    scaffold.showSnackBar(snackBarContent);
  }

}

推荐答案

这是因为使用的BuildContext没有Scaffold的祖先,因此无法找到它来呈现SnackBar,因为要由Scaffold来显示它.

根据of method份文件:

当在同一生成函数中实际创建Scaffold时, 生成函数的上下文参数不能用于查找 scaffold(因为它位于要返回的小部件的"上方").在这样的情况下 情况下,可以使用生成器的以下技术来提供 具有"位于"Scaffold"下"的BuildContext的新作用域:

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Demo')
    ),
    body: Builder(
      // Create an inner BuildContext so that the onPressed methods
      // can refer to the Scaffold with Scaffold.of().
      builder: (BuildContext context) {
        return Center(
          child: RaisedButton(
            child: Text('SHOW A SNACKBAR'),
            onPressed: () {
              ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                content: Text('Hello!'),
              ));
            },
          ),
        );
      },
    ),
  );
}

Solution

FloatingActionButton封装在Builder小部件中将以一种比使用GlobalKey更优雅的方式实现这一点,而在@Epichon的回答中已经提到了这一点.

Dart相关问答推荐

如何获取包含在 Dart 包中的assets资源 的文件路径?

带有字符串键的 Dart Map,与忽略大小写进行比较

如何在 Flutter 上实现事件监听器或委托

在启用 null 安全性的情况下,如何在 Iterable.firstWhere 中从 orElse 返回 null?

Angular Dart 和 Polymer Dart 的区别

如何保存 List 并使用 Hive 检索?

Flutter中包导入和普通导入有什么区别?

Flutter:合并两张图片,作为单张图片存储在本地存储中

错误:非抽象类InternalSelectableMathState

不要将 PageView 居中 - Flutter

Flutter Text 小部件中的 StrutStyle 是什么

在小部件构建期间,如何在Flutter中导航?

dart- 四舍五入

Flutter Drawer Widget - 更改 Scaffold.body 内容

如何判断两张 map 的省道是否相等

'dart:async' 的函数 `runZoned` 的用途

Dart:实例变量在私有类中应该是私有的还是公共的?

如何将条目添加到列表中的特定索引?

Dart,对泛型的限制?

如何在 Dart 中创建私有变量?