我无法理解以下代码段中Widget BuildContext
和Builder BuildContext
之间的差异:
@override
Widget build(BuildContext context) {
和
new Builder(
builder: (BuildContext context){
使用Widget BuildContext
SnackBar不会出现在用户界面中,但日志中会显示错误,表明Scaffold.of() Context
在使用时没有脚手架Builder context
一切正常。
Scaffold.of(context).showSnackBar(
new SnackBar(content: new Text('Processing Data')));
编辑:main.dart文件:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'APP'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final formKey = new GlobalKey<FormState>();
String username;
@override
//This context
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
body: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
new Flexible(
child: new Container(
child: new Center(
child: new Text(widget.title),
),
),
flex: 1,
),
new Form(
key: formKey,
child:
new Row(children: <Widget>[
new Flexible(child:
new Container(
margin: EdgeInsets.zero,
child: new TextFormField(
decoration:
new InputDecoration(
hintText: 'Username',
labelText: "1",
labelStyle: new TextStyle(color: new Color.fromARGB(255, 0, 0, 0)),
),
validator: (val) => val.isEmpty? 'Username can\'t be empty.' : null,
),
),
flex: 1,
),
new Flexible(child:
new Container(
margin: EdgeInsets.only(
left: 8.0,
right: 8.0,
),
child: new TextFormField(
decoration: new InputDecoration(
border: InputBorder.none,
hintText: "Password",
),
validator: (val) => val.isEmpty? 'Password can\'t be empty.' : null,
obscureText: true,
),
),
flex: 1,
),
new Container(
child:
new Builder(
//And this context
builder: (BuildContext context){
return RaisedButton(
child: new Text("Sign in"),
onPressed: (){
if (formKey.currentState.validate()) {
// If the form is valid, display a snackbar. In the real world, you'd
// often want to call a server or save the information in a database
Scaffold.of(context).showSnackBar(
new SnackBar(content: new Text('Processing Data')));
}
},
);
},
),
),
],
),
),
],
),
),
);
}
}
答案 0 :(得分:2)
如果您使用Scaffold
属性引用SnackBar
出现在key
的当前final GloabalKey<ScaffoldState> _key = GlobalKey<ScaffoldState>();
,则会更好。
Scaffold(
key:_key,
...
)
_key.currentState.showSnackBar(mySnackBar)
显示小吃店
context
请注意,在使用需要访问此上下文的窗口小部件时,每个构建器方法都会获得context
以便能够引用它。在您的代码中,build
混淆了,如果您将context
方法(Buildcontext scaffoldContext)
更改为Scaffold.of(scaffoldContext)
之类的独特内容,然后使用function tr() {
var vysledok = document.getElementById('vysledok_body');
var text= document.getElementById('source').value;
var languageFrom = document.getElementById("src").value;
var languageTo = document.getElementById("dst").value;
vysledok.innerHTML="";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', "https://.... someurl.php?
from=" + languageFrom + "&to=" + languageTo + "&text=" + text, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
var obj = JSON.parse(xmlhttp.responseText);
console.log(obj.ip);
vysledok.innerHTML = "<p class='bdr'>"+obj.text+"</p>";
}
if(xmlhttp.status == 500) {
//console.log('nejde');
tr();
}
}
};
xmlhttp.send(null);
}
我不是确定这是否有效,因为我经常在Flutter看到这种混乱。
无论如何,上面的解决方案更清洁了。