{"id":153,"date":"2025-06-26T00:10:20","date_gmt":"2025-06-25T16:10:20","guid":{"rendered":"https:\/\/www.bqmz001.top\/?p=153"},"modified":"2025-06-26T00:10:20","modified_gmt":"2025-06-25T16:10:20","slug":"flutter-listview%e6%87%92%e5%8a%a0%e8%bd%bd%ef%bc%88%e4%b8%8a%ef%bc%89-%e5%85%88%e6%90%9e%e5%a4%a7%e6%a6%82%e7%9a%84%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.bqmz001.top\/index.php\/2025\/06\/26\/flutter-listview%e6%87%92%e5%8a%a0%e8%bd%bd%ef%bc%88%e4%b8%8a%ef%bc%89-%e5%85%88%e6%90%9e%e5%a4%a7%e6%a6%82%e7%9a%84%e5%8e%9f%e7%90%86\/","title":{"rendered":"flutter ListView\u61d2\u52a0\u8f7d\uff08\u4e0a\uff09\u2014\u2014\u5148\u641e\u5927\u6982\u7684\u539f\u7406"},"content":{"rendered":"<h2>\u5e8f\uff1a\u6e29\u99a8\u63d0\u9192<\/h2>\n<p>\u5e72\u6d3b\u4e00\u5b9a\u8981\u7ec6\u81f4\uff0c\u6709\u53ef\u80fd\u4e00\u4e9b\u9e21\u6bdb\u849c\u76ae\u7684\u53c2\u6570\u9519\u8bef\u5c31\u4f1a\u8ba9\u4f60\u627e\u4e00\u4e0b\u5348\u751a\u81f3\u4e00\u5929\u90fd\u6ca1\u4ec0\u4e48\u8fdb\u5c55\u3002<br \/>\u4e3a\u4e86\u5b66\u4e60\u61d2\u52a0\u8f7d\uff0c\u540e\u7aef\u63d2\u5165\u4e86100\u6761\u5047\u6570\u636e\uff0c\u5199\u4e86\u4e00\u4e2a\u63a5\u53e3\u53eblistPage\u2014\u2014\u7ed3\u679c\u6211\u4ece\u8c03list\u63a5\u53e3\u7684\u65b9\u6cd5\u590d\u5236\u7c98\u8d34\u540e\u6070\u6070\u6ca1\u6709\u6539\u63a5\u53e3\u5730\u5740\uff0c\u5bfc\u81f4\u6211\u5bf9\u7740\u90a3\u4e2a<code>List.from(pageBean.records!.map((e) =&gt; ContactBean.fromJson(e)))<\/code>\uff08\u8fd9\u884c\u4ee3\u7801\u5b9e\u9645\u4e0a\u662f\u5bf9\u7684\uff09\u60f3\u7834\u5934\u90fd\u6ca1\u60f3\u51fa\u6765\u8fd9\u884c\u4ee3\u7801\u4e3a\u5565\u62a5\u9519\uff1a\u660e\u660e\u6570\u636e\u662f\u4e2aList\uff0c\u5728records\u91cc\uff0c\u4e3a\u4ec0\u4e48\u8fd9\u884c\u4e00\u76f4\u62a5\u9519\uff1f\uff01\u800clist\u63a5\u53e3\u7684\u8fd9\u884c\u5374\u4e0d\u4f1a\uff1f\uff01<\/p>\n<p><strong>\u4eba\u5728\u6c5f\u6e56\u98d8\u54ea\u6709\u4e0d\u6328\u5200\uff0c\u5e38\u5728\u6cb3\u8fb9\u8d70\u600e\u80fd\u4e0d\u6e7f\u978b\u5462\uff01<\/strong><\/p>\n<p>\u8fd8\u6709\uff0c\u8c8c\u4f3c\u5728Windows\u4e0b\uff0cRefreshIndicator\u662f\u4e0d\u54cd\u5e94\u7684&#8230;&#8230;<\/p>\n<p>\u597d\u5427\uff0c\u5f00\u59cb\u6b63\u6587\u4e86\u3002<\/p>\n<h2>1\u3001\u9996\u5148\u8981\u641e\u61c2\u61d2\u52a0\u8f7d\uff08\u5206\u9875\u52a0\u8f7d\uff09\u662f\u4ec0\u4e48\u4e1c\u897f<\/h2>\n<p>\u5e73\u65f6\u7ffb\u5404\u5f0f\u5404\u6837\u7684app\uff0c\u5927\u5217\u8868\uff08\u4f8b\u5982\u6dd8\u5b9d\u997f\u4e86\u5417\u7f8e\u56e2\u4ec0\u4e48\u7684\uff09100%\u4e0d\u662f\u4e00\u4e0b\u5b50\u5168\u90e8\u52a0\u8f7d\u51fa\u6765\u7684\uff08\u5b83\u4eec\u90fd\u8fd8\u6709\u4e2a\u6027\u5316\u63a8\u8350\uff0c\u6240\u4ee5\u51e0\u4e4e\u4e0d\u53ef\u80fd\uff09\u2014\u2014\u90a3\u5c31\u662f\u548c\u7af9\u5b50\u4e00\u6837\u4e00\u622a\u4e00\u622a\u52a0\u8f7d\u51fa\u6765\u7684\uff0c\u65e2\u7136\u662f\u4e00\u622a\u4e00\u622a\u7684\u6570\u636e\uff0c\u90a3\u6211\u5c31\u9700\u8981\u77e5\u9053\u8fd9\u4e9b\u4e1c\u897f\u662f\u51fa\u81ea\u54ea\u4e00\u622a\uff0c\u4e8e\u662f\u5c31\u6709\u4e86PageBean\u8fd9\u79cd\u5bf9\u8c61\u7684\u5b58\u5728\uff0c\u53ef\u4ee5\u544a\u8bc9\u4f60\u8fd9\u4e9b\u6570\u636e\u662f\u6574\u4f53\u7684\u54ea\u4e00\u90e8\u5206\u3002<\/p>\n<p>\u5c31\u62ff\u4ee5\u4e0b\u6570\u636e\u4e3e\u4f8b\uff08\u7eafAI\u751f\u6210\uff0c\u5982\u6709\u96f7\u540c\u7eaf\u5c5e\u5de7\u5408\uff09\uff1a<\/p>\n<pre><code>{\n    &quot;code&quot;: 200,\n    &quot;msg&quot;: &quot;\u64cd\u4f5c\u6210\u529f&quot;,\n    &quot;data&quot;: {\n        &quot;records&quot;: [\n            {\n                &quot;id&quot;: 1,\n                &quot;firstName&quot;: &quot;\u4e09&quot;,\n                &quot;lastName&quot;: &quot;\u5f20&quot;,\n                &quot;phoneNumber&quot;: &quot;13333333333&quot;,\n                &quot;age&quot;: 18,\n                &quot;sex&quot;: 1,\n                &quot;address&quot;: &quot;\u5c31\u4f4f\u5728\u90a3\u4e2a\u5c6f&quot;\n            },\n            {\n                &quot;id&quot;: 1936971071475949571,\n                &quot;firstName&quot;: &quot;\u4f1f&quot;,\n                &quot;lastName&quot;: &quot;\u5f20&quot;,\n                &quot;phoneNumber&quot;: &quot;13811234567&quot;,\n                &quot;age&quot;: 28,\n                &quot;sex&quot;: 1,\n                &quot;address&quot;: &quot;\u5317\u4eac\u5e02\u671d\u9633\u533a\u5efa\u56fd\u8def88\u53f7&quot;\n            }\n        ],\n        &quot;total&quot;: 101,\n        &quot;size&quot;: 10,\n        &quot;current&quot;: 1,\n        &quot;pages&quot;: 11\n    }\n}\n<\/code><\/pre>\n<p>\u6700\u5916\u5c42\u7684\u57fa\u672c\u662f\u4e2a\u4eba\u90fd\u8ba4\u8bc6\uff1a\u8fd4\u56de\u72b6\u6001\u548c\u6d88\u606f\uff0c\u4e5f\u5c31\u662f<code>code<\/code>\u548c<code>msg<\/code>\u3002<code>data<\/code>\u91cc\u9762\u653e\u8bf7\u6c42\u56de\u6765\u7684\u6570\u636e\u3002\u6709\u4e9b\u65f6\u5019\u8fd9\u4e2adata\u91cc\u76f4\u63a5\u5c31\u662f\u6570\u636ejson\uff0c\u4f46\u8981\u662f\u6709\u5206\u9875\uff0cdata\u8fd9\u5c42\u5c31\u9996\u5148\u662f\u4e00\u4e9b\u5206\u9875\u6570\u636e\u3002data\u91cc\u9762\u7684<code>records<\/code>\u624d\u662f\u771f\u6b63\u7684\u6570\u636e\u3002<br \/>\u6709\u4eba\u8bf4\u6211\u80fd\u4e0d\u80fd\u53bb\u6389\u8fd9\u5916\u9762\u7684\u5206\u9875\u2014\u2014\u867d\u8bf4\u4e5f\u4e0d\u662f\u4e0d\u884c\uff0c\u4f46\u8d77\u7801\u540e\u7aef\u63d0\u4f9b\u5206\u9875\u6570\u636e\u7684\u65b9\u5f0f\u662f\u76ee\u524d\u6765\u8bf4\u6700\u4f73\u5b9e\u8df5\u3001\u7b80\u4fbf\u8def\u5f84\u548c\u901a\u7528\u505a\u6cd5\u3002\u4ece\u8fd9\u4e2a\u5206\u9875\u6570\u636e\u6765\u770b\uff0c<br \/><code>total<\/code>\u80fd\u544a\u8bc9\u4f60\u540e\u7aef\u603b\u5171\u6709\u591a\u5c11\u6761\uff0c<br \/><code>size<\/code>\u544a\u8bc9\u4f60\u8fd9\u56de\u8fd4\u56de\u6765\u51e0\u6761\uff08\u5f53\u7136\u8fd9\u4e2a\u6570\u5b57\u662f\u9519\u7684\uff0c\u56e0\u4e3a\u8981\u7167\u987e\u9605\u8bfb\u4f53\u9a8c\u6545\u610f\u5220\u7684\u5269\u4e24\u6761\u800c\u975e10\uff0c\u52ff\u55b7\uff09\uff0c<br \/><code>current<\/code>\u544a\u8bc9\u4f60\u8fd9\u662f\u7b2c\u51e0\u9875<br \/><code>pages<\/code>\u544a\u8bc9\u4f60\u540e\u7aef\u603b\u5171\u6709\u591a\u5c11\u9875<br \/><em>\u6ce8\uff1a\u6211\u540e\u7aefspringboot\u7528mybatisplus\u81ea\u5e26\u7684\u5206\u9875\u5668\uff0c\u4eba\u5bb6\u957f\u8fd9\u4e2a\u6837\uff0c\u5982\u679c\u662fmybatis\u6216\u8005\u522b\u7684\u884d\u751f\u4ea7\u54c1\u751a\u81f3\u662fhibernate\/jpa\u4e4b\u7c7b\u7684\u4e0d\u4fdd\u8bc1\u5b57\u773c\u4e00\u6837\uff0c\u4f46\u63d0\u4f9b\u7684\u5185\u5bb9\u7c7b\u4f3c\uff0c\u5177\u4f53\u8bf7\u95ee\u4f60\u7684\u540e\u7aef\u540c\u5b66\u6216\u8005\u770b\u81ea\u5df1\u7528\u7684\u4ec0\u4e48\u5305\u3002<\/em><\/p>\n<p>\u597d\u4e86\u8bf4\u5b8cPageBean\u662f\u4ec0\u4e48\uff0c\u7136\u540e\u770b\u770b\u901a\u5e38\u600e\u4e48\u7528\uff08\u4e1a\u52a1\u903b\u8f91\uff09\uff1a<br \/>1\u3001\u521a\u8fdb\u53bb\u9875\u9762\u3001\u4e0b\u62c9\u5237\u65b0\u8bf7\u6c42\u52a0\u8f7d\u7b2c\u4e00\u9875\u6570\u636e\uff0c\u5148\u6e05\u7a7aList\u5bb9\u5668\uff0c\u8bf7\u6c42\u5230\u6570\u636e\u540e\u518d\u586b\u5145\u8fdb\u53bbList\u5bb9\u5668\uff1b<br \/>2\u3001\u5217\u8868\u62c9\u5230\u5e95\u5c31\u5728\u6700\u4e0b\u9762\u663e\u793aloading\u6837\u5f0f\uff0c\u8bf7\u6c42\u52a0\u8f7d\u7b2cn+1\u9875\u7684\u6570\u636e\uff08n\u6307\u5f53\u524d\u9875\uff09\uff0c\u7136\u540e\u628a\u8bf7\u6c42\u5230\u7684\u6570\u636e\u5f80\u540e\u8ddf\u7740\u586b\u5145\u8fdb\u53bbList\u5bb9\u5668\uff1b<br \/>3\u3001\u5217\u8868\u518d\u6b21\u62c9\u5230\u5e95\u90e8\u91cd\u590d\u7b2c\u4e8c\u6b65\uff1b<br \/>4\u3001\u6211\u6240\u6709\u6570\u636e\u90fd\u5c55\u793a\u51fa\u6765\u4e86\uff0c\u5217\u8868\u7406\u8bba\u4e0a\u62c9\u5230\u6700\u5e95\u4e0b\u4e86\uff0c\u6700\u4e0b\u9762\u4e0d\u80fd\u662floading\u4e86\u5e94\u8be5\u63d0\u793a\u7ed3\u675f\u4e86\uff0c\u4e0d\u8bf7\u6c42\u6570\u636e\u4e86\u3002<\/p>\n<p>\u7406\u8bba\u5b8c\u6bd5\uff0c\u5f00\u59cb\u5b9e\u64cd\uff01<\/p>\n<h2>2\u3001 \u7f51\u7edc\u8bf7\u6c42\u90e8\u5206\u600e\u4e48\u5199<\/h2>\n<p>\u60ef\u4f8b\u5148\u4e0a\u4ee3\u7801\uff1a<\/p>\n<h5>main_provider.dart<\/h5>\n<pre><code>import &#039;dart:convert&#039;;\n\nimport &#039;package:day05\/bean\/base_bean.dart&#039;;\nimport &#039;package:day05\/bean\/contact_bean.dart&#039;;\nimport &#039;package:day05\/dio_util.dart&#039;;\nimport &#039;package:dio\/dio.dart&#039;;\nimport &#039;package:flutter\/cupertino.dart&#039;;\n\nimport &#039;bean\/page_bean.dart&#039;;\n\nclass MainProvider with ChangeNotifier {\n  String _text = &#039;&#039;;\n\n  String get text =&gt; _text;\n\n  List&lt;ContactBean&gt; _dataList = [];\n  List&lt;ContactBean&gt; get dataList =&gt; _dataList;\n\n  int _pageNum = 1;\n  int get pageNum =&gt; _pageNum;\n\n\n  Future&lt;void&gt; 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) =&gt; ContactBean.fromJson(e)));\n        notifyListeners();\n      },\n      (reason) {\n        _text = reason;\n        notifyListeners();\n      },\n    );\n  }\n\n  Future&lt;void&gt; getListPage(int pageNum) async {\n    await DioUtil.getInstance().get(\n      &quot;http:\/\/192.168.5.116:20080\/contact\/listPage&quot;,\n      {\n        &#039;page&#039;: pageNum,\n        &#039;size&#039;: 10,\n      },\n          () {\n        _text = &quot;loading&quot;;\n        notifyListeners();\n      },\n          (data) {\n        var bean = BaseBean.fromJson(data);\n        var pageBean = PageBean.fromJson(bean.data);\n        if(pageBean.current==1) {\n          _dataList.clear();\n        }\n        _dataList.addAll(List.from(pageBean.records!.map((e) =&gt; ContactBean.fromJson(e))));\n        notifyListeners();\n      },\n          (reason) {\n        _text = reason;\n        notifyListeners();\n      },\n    );\n  }\n\n  void addPage(){\n    _pageNum++;\n  }\n\n  void resetPage() {\n    _pageNum = 1;\n  }\n\n  void addContact() async {\n    DioUtil.getInstance().addHeader(&quot;token&quot;, &quot;123456789&quot;);\n    await DioUtil.getInstance().post(\n      &quot;http:\/\/192.168.5.116:20080\/contact\/add&quot;,\n      {\n        &#039;firstName&#039;: &#039;\u738b&#039;,\n        &#039;lastName&#039;: &#039;\u516d&#039;,\n        &#039;phoneNumber&#039;: &#039;13456789010&#039;,\n        &#039;age&#039;: &#039;22&#039;,\n        &#039;sex&#039;: 1,\n        &#039;address&#039;: &#039;\u5c71\u897f\u7701\u664b\u57ce\u5e02&#039;,\n      },\n      () {\n        _text = &quot;loading&quot;;\n        notifyListeners();\n      },\n      (data) {\n        _text = &#039;\u6dfb\u52a0\u6210\u529f&#039;;\n        notifyListeners();\n      },\n      (reason) {\n        _text = reason;\n        notifyListeners();\n      },\n    );\n  }\n}\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u8fd9\u4e2a\u662f\u8ba9\u4f60\u4eec\u590d\u5236\u7684\uff0c\u73b0\u5728\u6458\u51fa\u6765\u91cd\u70b9\uff1a<\/p>\n<pre><code>List&lt;ContactBean&gt; _dataList = [];\nList&lt;ContactBean&gt; get dataList =&gt; _dataList;\n\nint _pageNum = 1;\nint get pageNum =&gt; _pageNum;\n\nFuture&lt;void&gt; getListPage(int pageNum) async {\n   await DioUtil.getInstance().get(\n     &quot;http:\/\/192.168.5.116:20080\/contact\/listPage&quot;,\n     {\n       &#039;page&#039;: pageNum,\n       &#039;size&#039;: 10,\n     },\n         () {\n       _text = &quot;loading&quot;;\n       notifyListeners();\n     },\n         (data) {\n       var bean = BaseBean.fromJson(data);\n       var pageBean = PageBean.fromJson(bean.data);\n       if(pageBean.current==1) {\n         _dataList.clear();\n       }\n       _dataList.addAll(List.from(pageBean.records!.map((e) =&gt; ContactBean.fromJson(e))));\n       notifyListeners();\n     },\n         (reason) {\n       _text = reason;\n       notifyListeners();\n     },\n   );\n }\n\nvoid addPage(){\n  _pageNum++;\n}\n\nvoid resetPage() {\n  _pageNum = 1;\n}\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u4f17\u6240\u5468\u77e5\uff0cUI\u662fStatelessWidget\uff0c\u6240\u4ee5\u72b6\u6001\u90fd\u5728provider\u91cc\u3002<br \/>\u73b0\u5728\u65b0\u589e\u4e86\u4e00\u4e2a_pageNum\u7684\u79c1\u6709\u53d8\u91cf\uff0c\u9ed8\u8ba4\u5c31\u662f1\uff0c\u4ee3\u8868\u7b2c\u4e00\u9875<br \/><code>addPage()<\/code>\u65b9\u6cd5\u548c<code>resetPage()<\/code>\u65b9\u6cd5\u662f\u7528\u6765\u9875\u6570+1\u548c\u91cd\u7f6e\u56de1\u7684<br \/>\u7136\u540e\u5c31\u8bb2<code>getListPage(int pageNum)<\/code>\u65b9\u6cd5\uff0c\u4f20\u5165\u8981\u8bf7\u6c42\u7684\u9875\u6570\uff0c\u9875\u6570\u52a0\u51cf\u901a\u8fc7\u521a\u4e0a\u9762\u8bb2\u7684\u4e24\u4e2a\u65b9\u6cd5\u63a7\u5236\uff0c\u91cc\u9762\u7684\u903b\u8f91\u4e5f\u548c\u6211\u4eec\u8bb2\u539f\u7406\u65f6\u5019\u7684\u60c5\u51b5\u662f\u7b26\u5408\u7684\uff1a<br \/>\u7b2c\u4e00\u9875\u5c31\u5148\u6e05\u7a7a\u518d\u91cd\u65b0\u586b\u5165\u6570\u636e\uff0c\u5176\u4ed6\u9875\u5c31\u5f80\u540e\u8ddf\u6570\u636e\u3002<\/p>\n<p>\u6211\u611f\u89c9\u6ca1\u4ec0\u4e48\u9700\u8981\u989d\u5916\u8bf4\u7684\u4e86\uff0c\u8fd9\u4e2aListView\u6682\u65f6\u770b\u8d77\u6765\u8981\u6bd4Android\u539f\u751f\u7684RecyclerView\u8981\u6ce8\u610f\u7684\u4e1c\u897f\u8981\u5c11\u5f88\u591a\u3002<\/p>\n<h2>3\u3001\u6211\u6570\u636e\u62ff\u5230\u4e86\u5982\u4f55\u5c55\u793a\uff1f<\/h2>\n<p>\u5148\u8bd5\u4e00\u4e0b\u624b\u52a8\u8bf7\u6c42\u5206\u9875\u6570\u636e~<br \/>\u4e0a\u4ee3\u7801\uff01<\/p>\n<h5>main.dart<\/h5>\n<pre><code>import &#039;package:day05\/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                    \/\/ \u5237\u65b0\u65f6\u91cd\u7f6e\u9875\u7801\u5e76\u91cd\u65b0\u52a0\u8f7d\u6570\u636e\n                    provider.resetPage();\n                    await provider.getListPage(1);\n                  },\n                  child: ListView.builder(\n                    itemCount: provider.dataList.length + 1, \/\/ \u591a\u4e00\u4e2a\u52a0\u8f7d\u63d0\u793a\u9879\n                    itemBuilder: (context, index) {\n                      if (index &lt; provider.dataList.length) {\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                      } else {\n                        \/\/ \u663e\u793a\u52a0\u8f7d\u63d0\u793a\n                        return ElevatedButton(onPressed: (){\n                          provider.addPage();\n                          provider.getListPage(provider.pageNum);\n                        }, child:const Text(&#039;\u5237\u65b0\u5217\u8868&#039;));\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>\u76f8\u8f83\u4e8e\u4e4b\u524d\u53ea\u6709\u4e0b\u62c9\u5237\u65b0\u90a3\u4f1a\uff0c<code>ListView.Builder<\/code>\u7684<code>itemCount<\/code>\u5c5e\u6027\u4e0d\u662fList\u7684\u957f\u5ea6\uff0c\u800c\u662f+1\u4e86\uff0c\u591a\u90a3\u4e00\u6761\u5c31\u662f\u4e3a\u4e86\u653e\u4e0a\u62c9\u52a0\u8f7d\u7684\u6846\u6846\u4e86\u3002<br \/>\uff08<em>\u6211\u8fd9\u91cc\u5148\u6682\u65f6\u653e\u7740\u4e00\u4e2a\u624b\u52a8\u7684\u52a0\u8f7d\u6309\u94ae\uff0c\u6309\u4e00\u4e0b\u518d\u52a0\u8f7d\u4e0b\u976210\u6761\u6570\u636e<\/em>\uff09<\/p>\n<p>\u4e5f\u5c31\u662f\u73b0\u5728\u7684UI\u903b\u8f91\u662f\uff1a\u4e0b\u62c9\u5237\u65b0-&gt;\u4e0a\u62c9\u5230\u6700\u540e\u4e00\u6761-&gt;\u6309\u4e00\u4e0b\u6309\u94ae\u52a0\u8f7d\u7b2c\u4e8c\u9875\uff0c\u4e4b\u540e\u4ee5\u6b64\u7c7b\u63a8\u3002<br \/>\uff08\u672c\u6765\u8fd8\u60f3\u7684itemCount\u8fd9\u4e48\u5199\uff1a<code>itemCount: provider.dataList.isEmpty ? 0 : provider.dataList.length + 1,<\/code>\uff0c\u4f46\u5b9e\u64cd\u8bc1\u660e\u867d\u7136\u53ef\u4ee5\u5728\u6ca1\u6709\u6570\u636e\u7684\u65f6\u5019\u9690\u85cf\u4f46\u7528\u8d77\u6765\u4f1a\u5361\u987f\uff0c\u6240\u4ee5\u5c31\u5148\u9732\u7740\u8fd9\u4e2abug\u4e4b\u540e\u89e3\u51b3\uff09<\/p>\n<p>\u5b9e\u9a8c\u6709\u6548\u3002<\/p>\n<p>\u8fd9\u65f6\u5019\u6709\u4eba\u5c31\u95ee\u4e86\uff1a\u8fd9\u624b\u52a8\u7684\u5237\u65b0\u65b9\u6cd5\u80fd\u505aapp\uff1f\u5f00\u4ec0\u4e48\u73a9\u7b11\uff01<br \/>\u90a3\u73b0\u5728\u5c31\u5f00\u59cb\u5c1d\u8bd5\u8ba9\u5b83\u81ea\u52a8\u5316\uff01<\/p>\n<p>\u4e0a\u4ee3\u7801\uff01<\/p>\n<h5>main.dart<\/h5>\n<pre><code>import &#039;package:day05\/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                    \/\/ \u5237\u65b0\u65f6\u91cd\u7f6e\u9875\u7801\u5e76\u91cd\u65b0\u52a0\u8f7d\u6570\u636e\n                    provider.resetPage();\n                    await provider.getListPage(provider.pageNum);\n                  },\n                  child: NotificationListener&lt;ScrollNotification&gt;(\n                    onNotification: (scrollInfo){\n                      if (scrollInfo.metrics.pixels == scrollInfo.metrics.maxScrollExtent) {\n                        \/\/ \u6eda\u52a8\u5230\u5e95\u90e8\uff0c\u52a0\u8f7d\u4e0b\u4e00\u9875\n                        provider.addPage();\n                        provider.getListPage(provider.pageNum);\n                      }\n                      return false;\n                    },\n                    child: ListView.builder(\n                      itemCount: provider.dataList.isEmpty ? 0 : provider.dataList.length + 1, \n                      itemBuilder: (context, index) {\n                        if (index &lt; provider.dataList.length) {\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                        } else {\n                          \/\/ \u663e\u793a\u52a0\u8f7d\u63d0\u793a\n                          return const Center(child: CircularProgressIndicator());\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>\u4e3b\u8981\u539f\u7406\u5c31\u662f\u5728RefreshIndicator\u4e0b\u7528NotificationListener\u518d\u5305\u4e00\u5c42\uff0c\u7528\u4e8e\u76d1\u542cListView\u7684\u6ed1\u52a8\u4e8b\u4ef6\uff0c\u518d\u5f80\u4e0b\u624d\u662fListView\u3002<br \/>\u5728NotificationListener\u7684onNotification\u4e8b\u4ef6\u4e2d\u5c06\u521a\u624d\u624b\u52a8\u6309\u6309\u94ae\u7684\u4e8b\u4ef6\u7ed9\u81ea\u52a8\u5316\uff1a\u5f53ListView\u5212\u62c9\u5230\u6700\u5e95\u4e0b\u540e\u53bb\u8c03\u63a5\u53e3\u8bf7\u6c42\u4e0b\u4e00\u9875\u7684\u6570\u636e\u3002<br \/>\u800cListView\u4e2d+1\u7684\u90a3\u4e00\u5757\u8fd4\u56de\u4e00\u4e2a\u52a0\u8f7d\u5708\u5708\u7528\u6765\u8868\u793a\u52a0\u8f7d\u72b6\u6001\u3002<\/p>\n<p>\u8fd9\u6837\u4f60\u5c31\u80fd\u5728\u624b\u673a\u4e0a\u5f97\u5230\u4e00\u4e2a\u80fd\u770b\u7684ListView\uff0c\u4f46\u8fd8\u79bb\u80fd\u7528\u5dee\u7684\u5f88\u8fdc\u3002<br \/>\u6240\u4ee5\u8be5\u7bc7\u6587\u7ae0\u6709\u540e\u7eed\uff0c\u7b49\u6211\u6478\u900f\u4e86\u9a6c\u4e0a\u66f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e8f\uff1a\u6e29\u99a8\u63d0\u9192 \u5e72\u6d3b\u4e00\u5b9a\u8981\u7ec6\u81f4\uff0c\u6709\u53ef\u80fd\u4e00\u4e9b\u9e21\u6bdb\u849c\u76ae\u7684\u53c2\u6570\u9519\u8bef\u5c31\u4f1a\u8ba9\u4f60\u627e\u4e00\u4e0b\u5348\u751a\u81f3\u4e00\u5929\u90fd\u6ca1\u4ec0\u4e48\u8fdb\u5c55\u3002\u4e3a\u4e86\u5b66\u4e60\u61d2\u52a0\u8f7d [&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-153","post","type-post","status-publish","format-standard","hentry","category-androidflutter"],"_links":{"self":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/153","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=153"}],"version-history":[{"count":71,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions\/224"}],"wp:attachment":[{"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bqmz001.top\/index.php\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}