ListView怎么在Flutter中使用-創(chuàng)新互聯(lián)

ListView怎么在Flutter中使用?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)建站主營河曲網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),河曲h5成都小程序開發(fā)搭建,河曲網(wǎng)站營銷推廣歡迎河曲等地區(qū)企業(yè)咨詢

JSON 數(shù)據(jù)結(jié)構(gòu)

ListView怎么在Flutter中使用

Item 結(jié)構(gòu)

Item 的結(jié)構(gòu)是一個(gè) Card 包含著一個(gè) Row 然后這個(gè) Row 里面左邊是一個(gè) Image ,右邊是一個(gè) Column

功能實(shí)現(xiàn)

  1. material 庫

  2. Json 解析

  3. 網(wǎng)絡(luò)請求

  4. 加載菊花

要實(shí)現(xiàn)上面四個(gè)功能,我們首先需要在 .dart 文件中引入如下代碼

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

網(wǎng)絡(luò)請求

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("、")}"
     ),
//     導(dǎo)演
     Text(
      '導(dǎo)演:${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)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

當(dāng)前標(biāo)題:ListView怎么在Flutter中使用-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://bm7419.com/article32/hdosc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、做網(wǎng)站、域名注冊全網(wǎng)營銷推廣、網(wǎng)站內(nèi)鏈、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司