欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter如何輕松實現(xiàn)動態(tài)更新ListView淺析

 更新時間:2022年02月15日 10:21:34   作者:技術(shù)小黑屋  
在Android中通常都會用到listview.那么flutter里面怎么用呢?下面這篇文章主要給大家介紹了關(guān)于Flutter如何輕松實現(xiàn)動態(tài)更新ListView的相關(guān)資料,需要的朋友可以參考下

前言

在 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)文章

最新評論