How to properly display a Snackbar in Flutter?

前端 未结 2 1873
渐次进展
渐次进展 2021-01-12 05:48

I am trying to show a Snackbar on click of a floatingActionbutton. But when I click on the floatingactionbutton it\'s not showing anyt

2条回答
  •  情歌与酒
    2021-01-12 06:12

    class MyApp extends StatefulWidget{
      @override
      MyAppState createState() {
        // TODO: implement createState
        return new MyAppState();
      }
    
    }
    
    class MyAppState extends State{
    
      final GlobalKey _scaffoldkey = new GlobalKey();
      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(
            key: _scaffoldkey,
            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();
    //            getImage();
              },
              tooltip: 'Pick Image',
              child: new Icon(Icons.add_a_photo),
            ),
          ),
        );
      }
    
      void showSnackBar() {
        final snackBarContent = SnackBar(
          content: Text("sagar"),
          action: SnackBarAction(
              label: 'UNDO', onPressed: _scaffoldkey.currentState.hideCurrentSnackBar),
        );
        _scaffoldkey.currentState.showSnackBar(snackBarContent);
      }
    
    
    
    }
    

提交回复
热议问题