你可以看到我的按钮在Scaffold的体内.但是我得到了这个例外:
Scaffold.of()使用不包含Scaffold的上下文调用.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('SnackBar Playground'),
),
body: Center(
child: RaisedButton(
color: Colors.pink,
textColor: Colors.white,
onPressed: _displaySnackBar(context),
child: Text('Display SnackBar'),
),
),
);
}
}
_displaySnackBar(BuildContext context) {
final snackBar = SnackBar(content: Text('Are you talkin\' to …Run Code Online (Sandbox Code Playgroud) 我们需要关闭一个屏幕,然后打开第二个屏幕。并在第一个屏幕的第二个屏幕上显示小吃店。
我尝试使用Navigator.push,但是此屏幕已经打开,并且出现此错误“状态错误:流已被收听”。
我试图在我的应用程序中显示小吃店以通知用户,但没有脚手架它会显示我的错误。我目前的代码是:
scaffoldKey.currentState?.showSnackBar(
new SnackBar(
backgroundColor: color ?? Colors.red,
duration: Duration(milliseconds: milliseconds),
content: Container(
height: 50.0,
child: Center(
child: new Text(
title,
style: AppTheme.textStyle.lightText.copyWith(color: Colors.white),
overflow: TextOverflow.ellipsis,
),
),
),
),
)
Run Code Online (Sandbox Code Playgroud) 我试图显示一个Snackbar点击floatingActionbutton。但是,当我单击时,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 …Run Code Online (Sandbox Code Playgroud)