如何在颤振中创建吐司?
您可以访问使用Scaffold.of(context)
然后像做
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("Sending Message"),
));
小吃吧和烤面包是一样的,根据material.io
https://material.io/guidelines/components/snackbars-toasts.html#snackbars-toasts-specs
父ScaffoldState
编辑:
一个完整的例子(我喜欢我如何在5分钟内做到这一点。爱你扑)
runApp(new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text("Snack bar"),
),
body: new ListView.builder(
itemCount: 42,
itemBuilder: (context, index) {
return new Card(
child: new Column(
children: <Widget>[
new Image.network("https://image.freepik.com/free-vector/unicorn-background-design_1324-79.jpg"),
new ListTile(
title: new Text("Wow"),
subtitle: new Text("That's a cool unicorn out there"),
trailing: new IconButton(
icon: const Icon(Icons.star),
onPressed:() {
Scaffold.of(context).showSnackBar(
new SnackBar(
content: new Text("Added to favorite"),
action: new SnackBarAction(
label: "UNDO",
onPressed:() => Scaffold.of(context).hideCurrentSnackBar(),
),
),
);
},
),
)
],
),
);
},
),
),
));
SnackBar
肯定是使用合适的班级,由黑人指出。
一个棘手的事情有关showSnackBar
越来越向ScaffoldState
,如果你试图到您建造Scaffold
构建方法中调用showSnackBar
。
您可能会看到像这样的错误,其中包含一些解释如何解决问题的文本。
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
The following assertion was thrown while handling a gesture:
Scaffold.of() called with a context that does not contain a Scaffold.
No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). This
usually happens when the context provided is from the same StatefulWidget as that whose build
function actually creates the Scaffold widget being sought.
There are several ways to avoid this problem. The simplest is to use a Builder to get a context that
is "under" the Scaffold. For an example of this, please see the documentation for Scaffold.of():
https://docs.flutter.io/flutter/material/Scaffold/of.html
A more efficient solution is to split your build function into several widgets. This introduces a
new context from which you can obtain the Scaffold. In this solution, you would have an outer widget
that creates the Scaffold populated by instances of your new inner widgets, and then in these inner
widgets you would use Scaffold.of().
A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, then use the
key.currentState property to obtain the ScaffoldState rather than using the Scaffold.of() function.
The context used was:
MyHomePage
When the exception was thrown, this was the stack:
#0 Scaffold.of (package:flutter/src/material/scaffold.dart:444:5)
#1 MyHomePage.build.<anonymous closure> (/Users/jackson/Library/Developer/CoreSimulator/Devices/7072C907-DBAD-44FE-8F40-0257442C51D9/data/Containers/Data/Application/77FEC1A4-1453-442C-8208-96E0323DEFB2/tmp/so_scratch2Tkq9Jb/so_scratch2/lib/main.dart:23:24)
#2 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:323:14)
#3 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:375:30)
#4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
#5 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:149:9)
#6 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:119:7)
#7 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
#8 BindingBase&SchedulerBinding&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20)
#9 BindingBase&SchedulerBinding&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
#10 BindingBase&SchedulerBinding&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
#11 BindingBase&SchedulerBinding&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
#12 BindingBase&SchedulerBinding&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
#13 _invoke1 (file:///b/build/slave/Mac_Engine/build/src/flutter/lib/ui/hooks.dart:100)
#14 _dispatchPointerDataPacket (file:///b/build/slave/Mac_Engine/build/src/flutter/lib/ui/hooks.dart:58)
Handler: onTap
Recognizer:
TapGestureRecognizer#69dbc(debugOwner: GestureDetector, state: ready)
════════════════════════════════════════════════════════════════════════════════════════════════════
您可以通过一个GlobalKey
您Scaffold
构造:
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final key = new GlobalKey<ScaffoldState>();
return new Scaffold(
key: key,
floatingActionButton: new Builder(
builder: (BuildContext context) {
return new FloatingActionButton(
onPressed:() {
key.currentState.showSnackBar(new SnackBar(
content: new Text("Sending Message"),
));
},
tooltip: 'Increment',
child: new Icon(Icons.add),
);
}
),
);
}
}
或者你可以使用一个Builder
创建BuildContext
那是脚手架的孩子。
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton: new Builder(
builder: (BuildContext context) {
return new FloatingActionButton(
onPressed:() {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("Sending Message"),
));
},
tooltip: 'Increment',
child: new Icon(Icons.add),
);
}
),
);
}
}
最后,你可以将你的小部件分成多个类,这是最好的长期方法。
试过了GlobalKey,现在我得到这个异常: 'I/flutter(4965):处理手势时抛出下面的断言: I/flutter(4965):type'LabeledGlobalKey
看起来你正在使用一个'GlobalKey'作为一个参数,在那里需要一个'BuildContext'。我无法帮助您进一步调试,而无需查看更多代码。请张贴引发异常的代码行,可能你只是没有使用正确的参数。 –
这应该如何包装在一个onPressed例如?因为我已经尝试过了,屏幕上什么都没有显示出来。 – aziza