ListView怎么在Flutter中使用?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
JSON 數(shù)據(jù)結(jié)構
Item 結(jié)構
Item 的結(jié)構是一個 Card
包含著一個 Row
然后這個 Row
里面左邊是一個 Image
,右邊是一個 Column
功能實現(xiàn)
material 庫
Json 解析
網(wǎng)絡請求
加載菊花
要實現(xiàn)上面四個功能,我們首先需要在 .dart
文件中引入如下代碼
import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart';
網(wǎng)絡請求
loadData() async { String loadRUL = "https://api.douban.com/v2/movie/in_theaters"; http.Response response = await http.get(loadRUL); var result = json.decode(response.body); setState(() { title = result['title']; print('title: $title'); subjects = result['subjects']; }); }
ListView && 加載菊花
getBody() { if (subjects.length != 0) { return ListView.builder( itemCount: subjects.length, itemBuilder: (BuildContext context, int position) { return getItem(subjects[position]); }); } else { // 加載菊花 return CupertinoActivityIndicator(); } }
Item編寫
getItem(var subject) { // 演員列表 var avatars = List.generate(subject['casts'].length, (int index) => Container( margin: EdgeInsets.only(left: index.toDouble() == 0.0 ? 0.0 : 16.0), child: CircleAvatar( backgroundColor: Colors.white10, backgroundImage: NetworkImage( subject['casts'][index]['avatars']['small'] ) ), ), ); var row = Container( margin: EdgeInsets.all(4.0), child: Row( children: <Widget>[ ClipRRect( borderRadius: BorderRadius.circular(4.0), child: Image.network( subject['images']['large'], width: 100.0, height: 150.0, fit: BoxFit.fill, ), ), Expanded( child: Container( margin: EdgeInsets.only(left: 8.0), height: 150.0, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ // 電影名稱 Text( subject['title'], style: TextStyle( fontWeight: FontWeight.bold, fontSize: 20.0, ), maxLines: 1, ), // 豆瓣評分 Text( '豆瓣評分:${subject['rating']['average']}', style: TextStyle( fontSize: 16.0 ), ), // 類型 Text( "類型:${subject['genres'].join("、")}" ), // 導演 Text( '導演:${subject['directors'][0]['name']}' ), // 演員 Container( margin: EdgeInsets.only(top: 8.0), child: Row( children: <Widget>[ Text('主演:'), Row( children: avatars, ) ], ), ) ], ), ) ) ], ), ); return Card( child: row, ); }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
當前標題:ListView怎么在Flutter中使用-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://www.rwnh.cn/article32/hdosc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、做網(wǎng)站、域名注冊、全網(wǎng)營銷推廣、網(wǎng)站內(nèi)鏈、標簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容