Flutter的教程:ListView

本文學習一下列表widget,是最多見的需求 
在Flutter中,用ListView來顯示列表項,支持垂直和水平方向展現,經過一個屬性咱們就能夠控制其方向 
1.水平的列表 
2.垂直的列表 
3.數據量很是大的列表 
4.內置的ListTile(挺好用的)app

4.內置的ListTile(挺好用的),先看下這個widget,在下面的listView中將直接使用less

/**
 * ListTile
 */
import 'package:flutter/material.dart';
 
void main() {
  runApp(new MyApp());
}
 
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "ListTile",
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("ListTile"),
        ),
        body: new MyCard(),
      ),
    );
  }
}
 
class MyCard extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MyCardState();
  }
}
 
class MyCardState extends State<MyCard> {
  var _throwShotAway = false;
 
  @override
  Widget build(BuildContext context) {
    return new Card(
      child: new ListTile(
          title: new Text("duo_shine"),
          subtitle: new Text("duo_shine@163.com"),
          //以前顯示icon
          leading: new Icon(Icons.email, color: Colors.blueAccent),
          //以後顯示checkBox
          trailing: new Checkbox(
              value: _throwShotAway,
              onChanged: (bool newValue) {
                setState(() {
                  _throwShotAway = newValue;
                });
              })),
    );
  }
}

咱們只關注ListTile便可,雖然組合了Card(Android的cardView)還有checkBox等widget,包括列表的點擊事件等(ps:ListTile中有點擊的監聽onTap),這些後續咱們將單獨學習,當前咱們在學習列表項展現 
運行: 
這裏寫圖片描述ide

 2.垂直的列表學習

import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
 
void main() {
  runApp(new MyApp());
}
 
/**
 * 垂直listView
 */
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'list';
    return new MaterialApp(
      title: title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(title),
        ),
        body: new Center(
          child: new ListView(
            //控制方向 默認是垂直的
//           scrollDirection: Axis.horizontal,
            children: <Widget>[
              _getContainer('Maps', Icons.map),
              _getContainer('phone', Icons.phone),
              _getContainer('Maps', Icons.map),
            ],
          ),
        ),
      ),
    );
  }
 
  /**
   * 抽取item項
   */
  Widget _getContainer(String test, IconData icon) {
    return new Container(
      width: 160.0,
//      ListTile
      child: new ListTile(
//       顯示在title以前
        leading: new Icon(icon),
//        顯示在title以後
        trailing: new Icon(icon),
        title: new Text(test),
        subtitle:new Text("我是subtitle") ,
      ),
    );
  }
}

運行: 
這裏寫圖片描述ui

1.水平的列表this

void main() {
  runApp(new MyApp());
}
 
/**
 * 垂直listView
 */
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'list';
    return new MaterialApp(
      title: title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(title),
        ),
        body: new Center(
          child: new ListView(
            //控制方向 默認是垂直的
           scrollDirection: Axis.horizontal,
            children: <Widget>[
              _getContainer('Maps', Icons.map),
              _getContainer('phone', Icons.phone),
              _getContainer('Maps', Icons.map),
            ],
          ),
        ),
      ),
    );
  }
 
  /**
   * 抽取item項
   */
  Widget _getContainer(String test, IconData icon) {
    return new Container(
      width: 160.0,
//      ListTile
      child: new ListTile(
//       顯示在title以前
        leading: new Icon(icon),
        title: new Text(test),
      ),
    );
  }
}

運行: 
這裏寫圖片描述spa

3.數據量很是大的列表code

import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
 
void main() {
  runApp(new MyApp( items: new List<String>.generate(10000, (i) => "Item $i"),));
}
 
/**
 *大量的item 好比上萬個
 */
class MyApp extends StatelessWidget {
  final List<String> items;
 
  MyApp({Key key, @required this.items}) : super(key: key);
 
  @override
  Widget build(BuildContext context) {
    final title = 'Long List';
 
    return new MaterialApp(
      title: title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(title),
        ),
        body: new ListView.builder(
          itemCount: items.length,
          itemBuilder: (context, index) {
            return new ListTile(
              title: new Text('${items[index]}'),
            );
          },
        ),
      ),
    );
  }
}

相關文章
相關標籤/搜索