我可以在Flutter中創建類似Toasts的東西嗎?只是一個很小的通知窗口,不直接面對用戶,並且不鎖定或淡化背後的視圖?如何在顫振中創建吐司?
回答
您可以訪問使用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'。我無法幫助您進一步調試,而無需查看更多代碼。請張貼引發異常的代碼行,可能你只是沒有使用正確的參數。 –
- 1. 顫振 - 創建倒計時部件
- 2. 顫振 - FloatingActionButton在中心
- 3. 與顫振
- 4. 顫振 - 它在幕後如何工作?
- 5. 顫振試驗MissingPluginException
- 6. 顫振:使用SlideTransition
- 7. 創建在顫振自定義時鐘部件
- 8. 如何集中自定義吐司創建
- 9. 創建全屏自定義吐司
- 10. 在顫振卡中插入JSON信息?
- 11. 顫振不可變類?
- 12. 顫振 - 佈局網格
- 13. 吐司
- 14. Android如何顯示吐司?
- 15. 如何在顫振中做「磨砂玻璃」效果?
- 16. 如何顯示數組中的吐司
- 17. 吐司崩潰
- 18. 吐司消息
- 19. 錯誤吐司
- 20. 如何在顫振和平臺之間傳輸話筒數據?
- 21. 如何選擇開發團隊在顫振
- 22. 顫振 - 無法構建,因爲frawework已經構建
- 23. eclipse中的吐司錯誤
- 24. 吐司中沒有文本
- 25. 吐司不在服務
- 26. 計時器在吐司android
- 27. 顫振構造函數錯誤
- 28. 顫振單元測試失敗
- 29. 顫振 - 打字文字動畫
- 30. 顫振:Tabbar vs Bottom導航條
這應該如何包裝在一個onPressed例如?因爲我已經嘗試過了,屏幕上什麼都沒有顯示出來。 – aziza