Flutter如何輕松實現(xiàn)動態(tài)更新ListView淺析
前言
在 App 開發(fā)過程中,ListView 是 比較很常見的控件,用來處理 列表類的數(shù)據(jù)展示。當(dāng)然 Flutter 也是支持的,由于 Flutter 是歸屬于聲明式 UI 編程,其處理起來要更加的簡單與便捷。
本文將通過一個極簡單的例子來說明一下 如何 實現(xiàn)動態(tài)更新數(shù)據(jù)。 在貼代碼之前,先介紹一些概念和內(nèi)容
數(shù)據(jù)集
final _names = ['Andrew', 'Bob', 'Charles']; int _counter = 0;
新的數(shù)據(jù)Item 'Someone($_counter)'
會被觸發(fā)加入到 _names 數(shù)組中。
觸發(fā)器
通常觸發(fā)加載數(shù)據(jù)是分頁數(shù)據(jù)加載完成,這里我們使用一個 FloatingActionButton
的點擊操作等價模擬。
floatingActionButton: FloatingActionButton( onPressed: () { setState(() { _names.add('Someone($_counter)'); _counter ++; }); }, tooltip: 'Add TimeStamp', child: const Icon(Icons.add),
展示視圖
Expanded( child: ListView.builder( itemCount: _names.length, itemBuilder: (BuildContext context, int index) { return Container( width: double.infinity, height: 50, alignment: Alignment.center, child: Text(_names[index])); }), ),
上述代碼
需要Expanded 包裹 ListView 確??臻g展示填充 使用 ListView.builder 方法實現(xiàn) ListView
總體來說,F(xiàn)lutter 中實現(xiàn) ListView 數(shù)據(jù)動態(tài)添加和展示,真的很便捷,少去了傳統(tǒng)UI 編程中顯式的 Adapter 等內(nèi)容,編碼效率提升不少。
完整代碼
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key, required this.title}) : super(key: key); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { final _names = ['Andrew', 'Bob', 'Charles']; int _counter = 0; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Column( children: [ Expanded( child: ListView.builder( itemCount: _names.length, itemBuilder: (BuildContext context, int index) { return Container( width: double.infinity, height: 50, alignment: Alignment.center, child: Text(_names[index])); }), ), ], ), floatingActionButton: FloatingActionButton( onPressed: () { setState(() { _names.add('Someone($_counter)'); _counter ++; }); }, tooltip: 'Add TimeStamp', child: const Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. ); } }
以上。
總結(jié)
到此這篇關(guān)于Flutter如何輕松實現(xiàn)動態(tài)更新ListView的文章就介紹到這了,更多相關(guān)Flutter動態(tài)更新ListView內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 新聞界面模擬ListView和ViewPager的應(yīng)用
本文主要介紹 Android ListView和ViewPager的應(yīng)用,這里模擬了新聞界面及實現(xiàn)示例代碼,有需要的小伙伴可以參考下2016-09-09android用java和c實現(xiàn)查找sd卡掛載路徑(sd卡路徑)的方法
這篇文章主要介紹了android用java和c實現(xiàn)查找sd卡掛載路徑(sd卡路徑)的方法,需要的朋友可以參考下2014-02-02Android PickerView底部選擇框?qū)崿F(xiàn)流程詳解
本次主要介紹Android中底部彈出框的使用,使用兩個案例來說明,首先是時間選擇器,然后是自定義底部彈出框的選擇器,以下來一一說明他們的使用方法2022-09-09Android中dip、dp、sp、pt和px的區(qū)別詳解
本篇文章是對Android中dip、dp、sp、pt和px的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06一個強(qiáng)大的側(cè)滑菜單控件ASwipeLayout
這篇文章主要為大家詳細(xì)介紹了強(qiáng)大的側(cè)滑菜單控件ASwipeLayout使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08Android應(yīng)用創(chuàng)建桌面快捷方式代碼
這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用創(chuàng)建桌面快捷方式代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Flutter 用自定義轉(zhuǎn)場動畫實現(xiàn)頁面切換
本篇介紹了 fluro 導(dǎo)航到其他頁面的自定義轉(zhuǎn)場動畫實現(xiàn),F(xiàn)lutter本身提供了不少預(yù)定義的轉(zhuǎn)場動畫,可以通過 transitionBuilder 參數(shù)設(shè)計多種多樣的轉(zhuǎn)場動畫,也可以通過自定義的 AnimatedWidget實現(xiàn)個性化的轉(zhuǎn)場動畫效果。2021-06-06Android AbsoluteLayout和RelativeLayout布局詳解
本文主要講解Android AbsoluteLayout和RelativeLayout布局,這里整理了相關(guān)資料,并附示例代碼和效果圖,有興趣的小伙伴可以參考下2016-08-08Android 中 Tweened animation的實例詳解
這篇文章主要介紹了Android 中 Tweened animation的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09