{"id":99,"date":"2025-06-24T22:26:10","date_gmt":"2025-06-24T14:26:10","guid":{"rendered":"https:\/\/www.bqmz001.top\/?p=99"},"modified":"2025-06-24T22:26:10","modified_gmt":"2025-06-24T14:26:10","slug":"flutter-%e7%bb%99%e5%88%97%e8%a1%a8%e6%b7%bb%e5%8a%a0%e4%b8%8b%e6%8b%89%e5%88%b7%e6%96%b0%e3%80%81toast%e5%92%8cdialog","status":"publish","type":"post","link":"https:\/\/www.bqmz001.top\/index.php\/2025\/06\/24\/flutter-%e7%bb%99%e5%88%97%e8%a1%a8%e6%b7%bb%e5%8a%a0%e4%b8%8b%e6%8b%89%e5%88%b7%e6%96%b0%e3%80%81toast%e5%92%8cdialog\/","title":{"rendered":"flutter \u7ed9\u5217\u8868\u6dfb\u52a0\u4e0b\u62c9\u5237\u65b0\u3001Toast\u548cDialog"},"content":{"rendered":"<h2>0\u3001\u505a\u5185\u5bb9\u65f6\u5019\u7684\u52d8\u8bef<\/h2>\n<p>\u9996\u5148\u8bf4firstName\u662f\u540d\uff0clastName\u662f\u59d3\u3002<br \/>\u63a5\u4e0b\u6765\u6b63\u6587\uff1a<\/p>\n<h2>1\u3001\u7ed9ListView\u6dfb\u52a0\u4e0b\u62c9\u5237\u65b0<\/h2>\n<p>\u4e0a\u4ee3\u7801\uff01<\/p>\n<h5>main.dart<\/h5>\n<pre><code>import &#039;package:day04\/main_provider.dart&#039;;\nimport &#039;package:flutter\/material.dart&#039;;\nimport &#039;package:provider\/provider.dart&#039;;\n\nvoid main() {\n  runApp(\n    MultiProvider(\n      providers: [ChangeNotifierProvider(create: (context) =&amp;gt; MainProvider())],\n      child: MyApp(),\n    ),\n  );\n}\n\nclass MyApp extends StatelessWidget {\n  const MyApp({super.key});\n\n  \/\/ This widget is the root of your application.\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: &#039;Flutter Demo&#039;,\n      theme: ThemeData(\n        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),\n      ),\n      home: const MyHomePage(title: &#039;Flutter Demo Home Page&#039;),\n    );\n  }\n}\n\nclass MyHomePage extends StatelessWidget {\n  const MyHomePage({super.key, required this.title});\n\n  final String title;\n\n  @override\n  Widget build(BuildContext context) {\n    final provider = Provider.of(context);\n\n    return Scaffold(\n      appBar: AppBar(\n        backgroundColor: Theme\n            .of(context)\n            .colorScheme\n            .inversePrimary,\n        title: Text(title),\n      ),\n      body: Center(\n        child: Column(\n          mainAxisAlignment: MainAxisAlignment.center,\n          children: [\n            Text(provider.text),\n            ElevatedButton(\n              onPressed: provider.getList,\n              child: const Text(&#039;\u8bf7\u6c42\u6570\u636e&#039;),\n            ),\n            ElevatedButton(\n              onPressed: () =&gt; provider.addContact(),\n              child: const Text(&#039;\u6dfb\u52a0\u4e00\u4e2a\u8054\u7cfb\u4eba&#039;),\n            ),\n            Expanded(\n                child: RefreshIndicator(\n                  onRefresh:() async=&gt;{\n                    provider.getList()\n                  },\n                  child: ListView.builder(\n                    itemCount: provider.dataList.length,\n                    itemBuilder: (context, index) {\n                      return ListTile(\n                        title: Text(&#039;${provider.dataList[index]\n                            .firstName} ${provider.dataList[index].lastName}&#039;),\n                        subtitle: Text(&#039;${provider.dataList[index]\n                            .phoneNumber}&#039;),\n                      );\n                    },\n                  ),\n                )\n            )\n          ],\n        ),\n      ),\n    );\n  }\n}\n<\/code><\/pre>\n<p>\u4eba\u8bdd\uff1a<br \/>Expanded\u6491\u5f00\u540e\u7528RefreshIndicator\u5305\u88f9\u539f\u5148\u7684ListView.Builder<br \/>\u7136\u540e\u5728RefreshIndicator\u7684onRefresh\u4e8b\u4ef6\u91cc\u53bb\u5199\u5237\u65b0\u7684\u4e8b\u4ef6\uff0c\u4e5f\u5c31\u662f\u8c03provider\u7684getList()\u65b9\u6cd5<br \/>\u4ee5\u9632\u6709\u4eba\u627e\u4e0d\u5230\u5c31\u653e\u4e00\u4e0b\u4ee3\u7801\u7247\u6bb5<\/p>\n<h5>main_provider.dart \u7247\u6bb5<\/h5>\n<pre><code>  void getList() async {\n    await DioUtil.getInstance().get(\n      &quot;http:\/\/192.168.5.116:20080\/contact\/list&quot;,\n      null,\n      () {\n        _text = &quot;loading&quot;;\n        notifyListeners();\n      },\n      (data) {\n        BaseBean bean = BaseBean.fromJson(data);\n        _dataList=List.from(bean.data.map((e) =&amp;gt; ContactBean.fromJson(e)));\n        notifyListeners();\n      },\n      (reason) {\n        _text = reason;\n        notifyListeners();\n      },\n    );\n  }\n<\/code><\/pre>\n<hr>\n<p>\u7136\u540e\u4f60\u5c31\u4f1a\u5f97\u5230\uff1a<\/p>\n<p><strong>\u4e00\u4e2a\u6ca1\u6cd5\u5237\u65b0\u7684\u4e0b\u62c9\u5237\u65b0\u7ec4\u4ef6<\/strong><br \/>\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u662f\u4e0d\u662f\u5f53\u4f60\u8dd1\u8d77\u6765\u7684\u65f6\u5019\u611f\u89c9\u88ab\u547d\u8fd0\u6349\u5f04\u4e86\uff0c\u4e0b\u62c9\u5237\u65b0\u7684\u65f6\u5019\u5c0f\u5708\u5708\u62c9\u4e0b\u6765\u5c31\u56de\u53bb\u5566<br \/><del>\u7136\u540e\u4f60\u5f00\u59cb\u4e0a\u706b\uff0c\u968f\u540e\u7838\u952e\u76d8\u62f3\u5934\u76f4\u51b2\u7535\u8111\u5c4f\u5e55\uff0c\u8001\u5b50\u4e0d\u53bb\u5b66flutter\u4e86\uff01\uff08\u4e0d\u662f\uff09<\/del><\/p>\n<hr>\n<p><strong>\u6765\u6765\u6765\u4e0b\u706b\u836f\u9012\u4e0a\u6765<\/strong><\/p>\n<p>\u53ea\u9700\u8981\u4ece\u8fd9\u6837\uff1a<\/p>\n<pre><code>void getList() async \/\/\u6709\u53ef\u80fd\u751a\u81f3\u8fdevoid\u90fd\u6ca1\u6709\n<\/code><\/pre>\n<p>\u6539\u6210\u8fd9\u6837\uff1a<\/p>\n<pre><code>Future&lt;void&gt; getList() async \n<\/code><\/pre>\n<p>\u5c31\u597d\u5566\uff01<br \/>\u539f\u56e0\u5462\u5c31\u662fRefreshIndicator\u7684onRefresh\u4e8b\u4ef6\u9700\u8981\u8fd4\u56de\u4e00\u4e2aFuture\u5bf9\u8c61\uff0c\u6765\u5224\u65ad\u5237\u65b0\u8fd9\u4e8b\u5e72\u5b8c\u4e86\u6ca1\u3002\u5177\u4f53\u770b\u4e00\u4e0b\u6e90\u7801\u5185\u5bf9\u4e8e\u8be5\u4e8b\u4ef6\u53c2\u6570\u7684\u8bf4\u660e\uff1a<\/p>\n<pre><code>  \/\/\/ A function that&#039;s called when the user has dragged the refresh indicator\n  \/\/\/ far enough to demonstrate that they want the app to refresh. The returned\n  \/\/\/ [Future] must complete when the refresh operation is finished.\n  final RefreshCallback onRefresh;\n<\/code><\/pre>\n<p>\u5176\u4e2d\u8fd9\u53e5<code>[Future] must complete when the refresh operation is finished.<\/code> \u5c31\u544a\u8bc9\u4f60 \u6211\u7ec4\u4ef6\u603b\u5f97\u77e5\u9053\u8fd9\u4e8b\u5b8c\u4e86\u5427\u3002<\/p>\n<hr>\n<p>\u7136\u540e\u4f60\u53d1\u73b0\u8c03\u7528\u662f\u8c03\u7528\u4e86\u2014\u2014<strong>\u4f46\u662f\u90a3\u4e2aRefreshIndicator\u8fd8\u662f\u5237\u4e00\u4e0b\u5c31\u8dd1\u4e86<\/strong><br \/>\u89e3\u51b3\u529e\u6cd5\uff1a\u7ed9\u65b9\u6cd5\u9762\u524d\u52a0\u4e0aawait\u5373\u53ef\uff0c\u4e5f\u5c31\u662f<code>await provider.getList()<\/code><br \/>\u8fd9\u6837\u5c31\u80fd\u8ba9RefreshIndicator\u6b63\u786e\u7b49\u5f85\u65b9\u6cd5\u6267\u884c\u5b8c\u6210\u518d\u6d88\u5931\u3002<\/p>\n<p>\u6dfb\u52a0\u4e0b\u62c9\u5237\u65b0\u8fd9\u4e8b\u7b97\u5b8c\u4e86\u3002<\/p>\n<h2>2\u3001\u6dfb\u52a0toast\u548cdialog<\/h2>\n<p>\u81f3\u4e8etoast\u7684\u8bdd\u5148\u53bbpubspec.yaml\u6dfb\u52a0\u4e00\u4e2a\uff1a<code>flutter_styled_toast: ^2.2.1<\/code><br \/>\u7136\u540eflutter pub get\u4e0b\u8f7d\u4e0b\u6765\u5c31\u884c<br \/>\u8fd9\u73a9\u610f\u5728pub.dev\u4e0a\u770b\u8d77\u6765\u86ee\u7b80\u5355\uff0c\u4e5f\u5c31<code>showToast(&quot;hello styled toast&quot;,context:context);<\/code>\u8fd9\u4e48\u7b80\u5355\uff0c\u4e00\u4e2a\u6587\u5b57\u548c\u4e00\u4e2acontext\u5c31\u591f\u7528<br \/>\u5f15\u7528\u4e86\u76f4\u63a5\u8fd9\u4e48\u5e72\u5c31\u884c\uff1a<\/p>\n<pre><code>import &#039;package:day04\/main_provider.dart&#039;;\nimport &#039;package:flutter\/material.dart&#039;;\nimport &#039;package:flutter_styled_toast\/flutter_styled_toast.dart&#039;;\nimport &#039;package:provider\/provider.dart&#039;;\n\n\nvoid main() {\n  runApp(\n    MultiProvider(\n      providers: [ChangeNotifierProvider(create: (context) =&gt; MainProvider())],\n      child: MyApp(),\n    ),\n  );\n}\n\nclass MyApp extends StatelessWidget {\n  const MyApp({super.key});\n\n  \/\/ This widget is the root of your application.\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: &#039;Flutter Demo&#039;,\n      theme: ThemeData(\n        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),\n      ),\n      home: const MyHomePage(title: &#039;Flutter Demo Home Page&#039;),\n    );\n  }\n}\n\nclass MyHomePage extends StatelessWidget {\n  const MyHomePage({super.key, required this.title});\n\n  final String title;\n\n  @override\n  Widget build(BuildContext context) {\n    final provider = Provider.of&lt;MainProvider&gt;(context);\n\n    return Scaffold(\n      appBar: AppBar(\n        backgroundColor: Theme\n            .of(context)\n            .colorScheme\n            .inversePrimary,\n        title: Text(title),\n      ),\n      body: Center(\n        child: Column(\n          mainAxisAlignment: MainAxisAlignment.center,\n          children: &lt;Widget&gt;[\n            Text(provider.text),\n            ElevatedButton(\n              onPressed: provider.getList,\n              child: const Text(&#039;\u8bf7\u6c42\u6570\u636e&#039;),\n            ),\n            ElevatedButton(\n              onPressed: () =&gt; provider.addContact(),\n              child: const Text(&#039;\u6dfb\u52a0\u4e00\u4e2a\u8054\u7cfb\u4eba&#039;),\n            ),\n            Expanded(\n                child: RefreshIndicator(\n                  onRefresh:() async=&gt;{\n                    provider.getList()\n\n                  },\n                  child: ListView.builder(\n                    itemCount: provider.dataList.length,\n                    itemBuilder: (context, index) {\n                      return ListTile(\n                        title: Text(&#039;${provider.dataList[index].lastName} ${provider.dataList[index].firstName}&#039;),\n                        subtitle: Text(&#039;${provider.dataList[index].phoneNumber}&#039;),\n                        onTap: ()=&gt;{\n                          showToast(&#039;\u70b9\u51fb\u4e86\u7b2c${index+1}\u4e2a\u8054\u7cfb\u4eba\uff1a${provider.dataList[index].lastName} ${provider.dataList[index].firstName}&#039;,context: context)\n                        },\n                      );\n                    },\n                  ),\n                )\n            )\n          ],\n        ),\n      ),\n    );\n  }\n}\n<\/code><\/pre>\n<p>\u91cd\u70b9\u4e5f\u5c31\u8fd9\u884c\uff1a<code>showToast(&#039;\u70b9\u51fb\u4e86\u7b2c${index+1}\u4e2a\u8054\u7cfb\u4eba\uff1a${provider.dataList[index].lastName} ${provider.dataList[index].firstName}&#039;,context: context)<\/code><\/p>\n<p><em>\u6ce8\uff1atoast\u5b9e\u73b0\u6709\u5f88\u591a\u5305\uff0cflutter_styled_toast\u5c31\u662f\u6587\u7ae0\u91cc\u7684\u7b97\u4e00\u4e2a\uff0c\u4e5f\u6709\u4e2afluttertoast\u2014\u2014\u4e0d\u8fc7\u53ea\u652f\u6301Android&amp;iOS&amp;web\u3002\u8fd8\u6709\u4e00\u4e2a\u662ftoastification\u548c\u672c\u6587\u91cc\u7684flutter_styled_toast\u4e00\u6837\u652f\u6301\u5168\u5e73\u53f0\uff0c\u4f46\u90a3\u4e2atoast\u524d\u7aef\u540c\u5b66\u66f4\u719f\u6089\u2014\u2014\u53f3\u4e0a\u89d2\u5f39\u51fa\u6d88\u606f\u3002<br \/>\u8fd9\u51e0\u4e2a\u7528\u6cd5\u90fd\u5f88\u7b80\u5355\uff0c\u770b\u770bpub\u7684example\u5c31\u884c<\/em><\/p>\n<hr>\n<h4>\u6dfb\u52a0dialog<\/h4>\n<p>\u5176\u5b9e\u6dfb\u52a0dialog\u4e5f\u6ca1\u90a3\u4e48\u96be\uff0c\u73b0\u5728\u6682\u65f6\u5148\u52a0\u4e00\u4e2aAlertDialog\u5bf9\u8bdd\u6846\u548c\u6309\u94ae\u3002\u4e0a\u4ee3\u7801\uff1a<\/p>\n<h5>main.dart<\/h5>\n<pre><code>import &#039;package:day04\/main_provider.dart&#039;;\nimport &#039;package:flutter\/material.dart&#039;;\nimport &#039;package:flutter_styled_toast\/flutter_styled_toast.dart&#039;;\nimport &#039;package:provider\/provider.dart&#039;;\n\n\nvoid main() {\n  runApp(\n    MultiProvider(\n      providers: [ChangeNotifierProvider(create: (context) =&gt; MainProvider())],\n      child: MyApp(),\n    ),\n  );\n}\n\nclass MyApp extends StatelessWidget {\n  const MyApp({super.key});\n\n  \/\/ This widget is the root of your application.\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: &#039;Flutter Demo&#039;,\n      theme: ThemeData(\n        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),\n      ),\n      home: const MyHomePage(title: &#039;Flutter Demo Home Page&#039;),\n    );\n  }\n}\n\nclass MyHomePage extends StatelessWidget {\n  const MyHomePage({super.key, required this.title});\n\n  final String title;\n\n  @override\n  Widget build(BuildContext context) {\n    final provider = Provider.of&lt;MainProvider&gt;(context);\n\n    return Scaffold(\n      appBar: AppBar(\n        backgroundColor: Theme\n            .of(context)\n            .colorScheme\n            .inversePrimary,\n        title: Text(title),\n      ),\n      body: Center(\n        child: Column(\n          mainAxisAlignment: MainAxisAlignment.center,\n          children: &lt;Widget&gt;[\n            Text(provider.text),\n            ElevatedButton(\n              onPressed: provider.getList,\n              child: const Text(&#039;\u8bf7\u6c42\u6570\u636e&#039;),\n            ),\n            ElevatedButton(\n              onPressed: () =&gt; provider.addContact(),\n              child: const Text(&#039;\u6dfb\u52a0\u4e00\u4e2a\u8054\u7cfb\u4eba&#039;),\n            ),\n            Expanded(\n                child: RefreshIndicator(\n                  onRefresh: () async {\n                    await provider.getList();\n                  },\n                  child: ListView.builder(\n                    itemCount: provider.dataList.length,\n                    itemBuilder: (context, index) {\n                      return ListTile(\n                        title: Text(&#039;${provider.dataList[index].lastName} ${provider.dataList[index].firstName}&#039;),\n                        subtitle: Text(&#039;${provider.dataList[index].phoneNumber}&#039;),\n                        onTap: () async  {\n                          if(index%2==0){\n                            showToast(&#039;\u70b9\u51fb\u4e86\u7b2c${index+1}\u4e2a\u8054\u7cfb\u4eba\uff1a${provider.dataList[index].lastName} ${provider.dataList[index].firstName}&#039;,context: context);\n                          } else {\n                            var data =await showADialog(context, &#039;\u63d0\u793a&#039;,\n                                &#039;\u70b9\u51fb\u4e86\u7b2c${index + 1}\u4e2a\u8054\u7cfb\u4eba\uff1a${provider\n                                    .dataList[index].lastName} ${provider\n                                    .dataList[index].firstName}&#039;);\n                            showToast(data.toString(), context: context);\n                          }\n                        },\n                      );\n                    },\n                  ),\n                )\n            )\n          ],\n        ),\n      ),\n    );\n  }\n\n  Future&lt;dynamic&gt; showADialog(context,title,content){\n    return showDialog(\n        context: context,\n        builder: (context)=&gt;AlertDialog(\n          title: Text(title),\n          content: Text(content),\n          actions: [\n            TextButton(\n              onPressed: () =&gt; Navigator.of(context).pop(3),\n              child: Text(&#039;\u67d0\u79cd\u4e2d\u7acb\u6309\u94ae\uff1f&#039;),\n            ),\n            TextButton(\n              onPressed: () =&gt; Navigator.of(context).pop(2),\n              child: Text(&#039;\u53d6\u6d88&#039;),\n            ),\n            TextButton(\n              onPressed: () =&gt; Navigator.of(context).pop(1),\n              child: Text(&#039;\u786e\u5b9a&#039;),\n            ),\n          ],\n        )\n    );\n  }\n}\n<\/code><\/pre>\n<p>\u4ee3\u7801\u91cc\u9762\u903b\u8f91\u662f\uff1aListTile\u7684\u70b9\u51fb\u4e8b\u4ef6onTap()\u91cc\uff0c\u5982\u679c\u70b9\u51fb\u662f\u5076\u6570index\u7684\u5c31\u662ftoast\uff0c\u70b9\u51fb\u5947\u6570index\u7684\u5c31\u662fAlertDialog\u3002<br \/>\u5728\u8fd9\u628ashowDialog\u65b9\u6cd5\u5355\u72ec\u62ff\u51fa\u6765\u5c01\u88c5\u4e86\u4e00\u4e0b\uff0c\u65b9\u4fbf\u770b\u6e05\u695a\u4ee3\u7801\u3002<\/p>\n<p>showDialog\u65b9\u6cd5\u8fd4\u56deFuture&lt;&gt;\u5bf9\u8c61\uff0c\u4e00\u822c\u5199Future\u3002\u91cc\u9762\u597d\u50cf\u80fd\u653e\u591a\u4e2a\u6309\u94ae\u3002<br \/>\u5173\u95ed\u5bf9\u8bdd\u6846\u7528<code>Navigator.of(context).pop()<\/code>\u5373\u53ef\uff0c\u9700\u8981\u8fd4\u56de\u5565\u5f80pop\u91cc\u4f20\u5565\u6296\u5c31\u884c\u3002\u6211\u8fd9\u91cc\u4f20123\uff0c\u4ee3\u8868\u786e\u5b9a\u6309\u94ae\uff08positive\uff09\u3001\u53d6\u6d88\u6309\u94ae\uff08negative\uff09\u548c\u4e2d\u7acb\u6309\u94ae\uff08neutral\uff09\u65b9\u4fbf\u5224\u65ad\u3002\u53ea\u4e0d\u8fc7\u8bf4\u62ff\u5230\u6211\u70b9\u4e86\u54ea\u4e2a\u6309\u94ae\u540e\u53c8\u5f39\u4e86\u4e00\u4e2atoast \u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>0\u3001\u505a\u5185\u5bb9\u65f6\u5019\u7684\u52d8\u8bef \u9996\u5148\u8bf4firstName\u662f\u540d\uff0clastName\u662f\u59d3\u3002\u63a5\u4e0b\u6765\u6b63\u6587\uff1a 1\u3001\u7ed9ListView [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-androidflutter"],"_links":{"self":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":52,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/99\/revisions\/151"}],"wp:attachment":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}