{"id":1158,"date":"2023-03-16T22:18:28","date_gmt":"2023-03-16T14:18:28","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1158"},"modified":"2023-03-16T22:18:35","modified_gmt":"2023-03-16T14:18:35","slug":"flutter-widget-snackbar","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/16\/flutter-widget-snackbar\/","title":{"rendered":"Flutter Widget\u4e4bSnackBar"},"content":{"rendered":"<p>SnackBar\u662f\u7528\u6237\u64cd\u4f5c\u540e\uff0c\u663e\u793a\u63d0\u793a\u4fe1\u606f\u7684\u4e00\u4e2a\u63a7\u4ef6\uff0c\u7c7b\u4f3cToast\uff0c\u4f1a\u81ea\u52a8\u9690\u85cf\u3002SnackBar\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u64cd\u4f5c\u6309\u94ae\u3002SnackBar\u662f\u901a\u8fc7Scaffold\u7684showSnackBar\u65b9\u6cd5\u6765\u663e\u793a\u7684\u3002<\/p>\n<p>Widget\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/flutter.io\/docs\/development\/ui\/widgets\">https:\/\/flutter.io\/docs\/development\/ui\/widgets<\/a><br \/>\nSnackBar\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/docs.flutter.io\/flutter\/material\/SnackBar-class.html\">https:\/\/docs.flutter.io\/flutter\/material\/SnackBar-class.html<\/a><br \/>\n\u00a0 \u00a0 \u00a0 <\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-dart\">import &#039;package:flutter\/material.dart&#039;;\n\nclass SnackBarDemoPage extends StatefulWidget {\n  @override\n  State&lt;StatefulWidget&gt; createState() =&gt; new _SnackBarDemoPageState();\n}\n\nclass _SnackBarDemoPageState extends State&lt;SnackBarDemoPage&gt; {\n\n  @override\n  void initState() {\n    super.initState();\n  }\n\n  Widget build(BuildContext context) {\n    return Scaffold(\n        appBar: new AppBar(\n          title: new Text(&#039;SnackBar Demo&#039;),\n        ),\n        floatingActionButton: new Builder(builder: (BuildContext context) {\n          return new FloatingActionButton(\n            onPressed: () {\n              \/\/ SnackBarBuilder.showSnackBar(context);\n              SnackBarBuilder.showAlertSnackBar(context);\n            },\n            child: new Icon(Icons.add),\n          );\n        }),\n\/\/        body: Center(\n\/\/          child: Text(&#039;SnackBar&#039;),\n\/\/        )\n        body: new Builder(builder: (BuildContext context) {\n          return new Center(\n            child: new GestureDetector(\n              onTap: () {\n                final snackBar = new SnackBar(content: new Text(&#039;\u8fd9\u662f\u4e00\u4e2aSnackBar&#039;));\n                Scaffold.of(context).showSnackBar(snackBar);\n              },\n              child: new Text(&#039;\u663e\u793aSnackBar&#039;),\n            ),\n          );\n        })\n    );\n  }\n}\n\nclass SnackBarBuilder {\n  static void showSnackBar(BuildContext context) {\n    final snackBar = new SnackBar(content: new Text(&#039;\u8fd9\u662f\u4e00\u4e2aSnackBar!&#039;));\n    Scaffold.of(context).showSnackBar(snackBar);\n  }\n\n  static void showAlertSnackBar(BuildContext context) {\n    final snackBar = new SnackBar(\n      content: new Text(&#039;\u5220\u9664\u4fe1\u606f&#039;),\n      action: new SnackBarAction(\n          label: &#039;\u64a4\u6d88&#039;,\n          onPressed: () {\n            \/\/ do something to undo\n          }\n      ),\n    );\n    Scaffold.of(context).showSnackBar(snackBar);\n  }\n}<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Flutter\/Flutter_Widget_SnackBar.png\" alt=\"Flutter Widget SnackBar\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SnackBar\u662f\u7528\u6237\u64cd\u4f5c\u540e\uff0c\u663e\u793a\u63d0\u793a\u4fe1\u606f\u7684\u4e00\u4e2a\u63a7\u4ef6\uff0c\u7c7b\u4f3cToast\uff0c\u4f1a\u81ea\u52a8\u9690\u85cf\u3002SnackBar\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u64cd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[305],"class_list":["post-1158","post","type-post","status-publish","format-standard","hentry","category-flutter","tag-flutter-widget"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1158"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1158\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}