flutter ListView就像iOS的tableview,安卓就是Listview GridView Widget相当于iOS的collectionview 和点击事件

摘要:
进口包装:颤振/材料。飞镖;voidmain(){runApp(MaterialApp(title:'Fluttergeste',home:LearnListView(),));}classLearnListViewextendsStatefulWidget{@overrideState<StatefulWidget>createState(){
import 'package:flutter/material.dart';

void main() {
  runApp( MaterialApp(
    title: 'Flutter gesture',
    home: LearnListView(),
  ));
}
class LearnListView extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return new _LearnListView();
  }
}
class _LearnListView extends State<StatefulWidget>{

  final List<int> data=[];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    for(int i=0;i<100;i++){
      data.add(i);
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          int itemDat=data[index];
          return new Row(
            children: <Widget>[
              new Container(
                child:new Image.network(
                  'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=15335368'
                      '41326&di=682e2e7c3810ac92be325e62e173c0ea&imgtype=0&src=http%3A%2F%2Fs6.si'
                      'naimg.cn%2Fmw690%2F006LDoUHzy7auXEaYER25%26690',
                   100.0,
                  height: 70.0,
                  fit: BoxFit.cover,
                ),
                margin: EdgeInsets.all(10.0),
              ),
              new Expanded(
                child:new Column(
                  children: <Widget>[
                    new Container(
                      height: 70.0,
                      child: new Text('这是一张非常漂亮的美女图$itemDat,喜欢就赶紧来欣赏吧,等着你哦'),
                    ),
                  ],
                ),
                flex: 1,
              ),
            ],
          );
        },
        itemCount:data.length ,
      ),
    );
  }
}

flutter ListView就像iOS的tableview,安卓就是Listview GridView Widget相当于iOS的collectionview 和点击事件第1张

简单的List(纵向)

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'Basic List';

    return new MaterialApp(
      title: title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(title),
        ),
        body: new ListView(
          children: <Widget>[
            new ListTile(
              leading: new Icon(Icons.map),
              title: new Text('Map'),
            ),
            new ListTile(
              leading: new Icon(Icons.photo),
              title: new Text('Album'),
            ),
            new ListTile(
              leading: new Icon(Icons.phone),
              title: new Text('Phone'),
            ),
          ],
        ),
      ),
    );
  }
}

简单的List(横向)

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'Horizontal List';

    return new MaterialApp(
      title: title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(title),
        ),
        body: new Container(
          margin: new EdgeInsets.symmetric(vertical: 20.0),
          height: 200.0,
          child: new ListView(
            scrollDirection: Axis.horizontal,
            children: <Widget>[
              new Container(
                 160.0,
                color: Colors.red,
              ),
              new Container(
                 160.0,
                color: Colors.blue,
              ),
              new Container(
                 160.0,
                color: Colors.green,
              ),
              new Container(
                 160.0,
                color: Colors.yellow,
              ),
              new Container(
                 160.0,
                color: Colors.orange,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
使用长列表,自定义参数
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp(
    items: new List<String>.generate(10000, (i) => "Item $i"),
  ));
}

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]}'),
            );
          },
        ),
      ),
    );
  }
}

flutter ListView就像iOS的tableview,安卓就是Listview GridView Widget相当于iOS的collectionview 和点击事件第2张

创建不同类型子项的List
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp(
    items: new List<ListItem>.generate(
      1000,
          (i) => i % 6 == 0
          ? new HeadingItem("Heading $i")
          : new MessageItem("Sender $i", "Message body $i"),
    ),
  ));
}

class MyApp extends StatelessWidget {
  final List<ListItem> items;

  MyApp({Key key, @required this.items}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final title = 'Mixed List';

    return new MaterialApp(
      title: title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(title),
        ),
        body: new ListView.builder(
          // Let the ListView know how many items it needs to build
          itemCount: items.length,
          // Provide a builder function. This is where the magic happens! We'll
          // convert each item into a Widget based on the type of item it is.
          itemBuilder: (context, index) {
            final item = items[index];

            if (item is HeadingItem) {
              return new ListTile(
                title: new Text(
                  item.heading,
                  style: Theme.of(context).textTheme.headline,
                ),
              );
            } else if (item is MessageItem) {
              return new ListTile(
                title: new Text(item.sender),
                subtitle: new Text(item.body),
              );
            }
          },
        ),
      ),
    );
  }
}

// The base class for the different types of items the List can contain
abstract class ListItem {}

// A ListItem that contains data to display a heading
class HeadingItem implements ListItem {
  final String heading;

  HeadingItem(this.heading);
}

// A ListItem that contains data to display a message
class MessageItem implements ListItem {
  final String sender;
  final String body;

  MessageItem(this.sender, this.body);
}

flutter ListView就像iOS的tableview,安卓就是Listview GridView Widget相当于iOS的collectionview 和点击事件第3张

创建一个 Grid List(就是使用形如iOS的collectionview)
import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'Grid List';

    return new MaterialApp(
      title: title,
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(title),
        ),
        body: new GridView.count(
          // Create a grid with 2 columns. If you change the scrollDirection to
          // horizontal, this would produce 2 rows.
          crossAxisCount: 2,
          // Generate 100 Widgets that display their index in the List
          children: new List.generate(100, (index) {
            return new Center(
              child: new Text(
                'Item $index',
                style: Theme.of(context).textTheme.headline,
              ),
            );
          }),
        ),
      ),
    );
  }
}

flutter ListView就像iOS的tableview,安卓就是Listview GridView Widget相当于iOS的collectionview 和点击事件第4张

 左右滑动删除List项

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp(
    items: new List<String>.generate(20, (i) => "Item $i"),
  ));
}

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) {
            final item = items[index];

            return new Dismissible(
              // Each Dismissible must contain a Key. Keys allow Flutter to
              // uniquely identify Widgets.
              key: new Key(item),
              // We also need to provide a function that will tell our app
              // what to do after an item has been swiped away.
              onDismissed: (direction) {
                items.removeAt(index);

                Scaffold.of(context).showSnackBar(
                    new SnackBar(content: new Text("$item dismissed")));
              },
              // Show a red background as the item is swiped away
              background: new Container(color: Colors.red),
              child: new ListTile(title: new Text('$item')),
            );
          },
        ),
//        new ListView.builder(
//          itemCount: items.length,
//          itemBuilder: (context, index) {
//            return new ListTile(
//              title: new Text('${items[index]}'),
//            );
//          },
//        ),
      ),
    );
  }
}

flutter ListView就像iOS的tableview,安卓就是Listview GridView Widget相当于iOS的collectionview 和点击事件第5张

定义参数Listview cell的点击事件是通过touch实现的
import 'package:flutter/material.dart';

void main() {
  runApp(SampleApp());
}

class SampleApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sample App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SampleAppPage(),
    );
  }
}

class SampleAppPage extends StatefulWidget {
  SampleAppPage({Key key}) : super(key: key);

  @override
  _SampleAppPageState createState() => _SampleAppPageState();
}

class _SampleAppPageState extends State<SampleAppPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Sample App"),
      ),
      body: ListView(children: _getListData()),
    );
  }

  _getListData() {
    List<Widget> widgets = [];
    for (int i = 0; i < 100; i++) {
      widgets.add(GestureDetector(
        child: Padding(
          padding: EdgeInsets.all(10.0),
          child: Text("Row $i"),
        ),
        onTap: () {
          print('row tapped ${i}');
        },
      ));
    }
    return widgets;
  }
}

免责声明:文章转载自《flutter ListView就像iOS的tableview,安卓就是Listview GridView Widget相当于iOS的collectionview 和点击事件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇java.sql.SQLException: Unknown system variable 'query_cache_size'HttpClient 专题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ListView数据动态更新

      经常会用到数据与前台控件绑定相关的问题,一直知道要用委托(代理)但每次都忘,而且每次都百度了一遍又一遍,就是不长记性,这次一定好好记下来下次就不会忘了。       后台数据与前台绑定主要分为两步:       第一步把要绑定的数据定义为一个数据集合或对象绑定到List中,方便调用: public class TestCase:INotifyP...

C# ListView用法详解

一、ListView类   1、常用的基本属性:         (1)FullRowSelect:设置是否行选择模式。(默认为false) 提示:只有在Details视图该属性才有意义。         (2) GridLines:设置行和列之间是否显示网格线。(默认为false)提示:只有在Details视图该属性才有意义。         (3)A...

Android蓝牙开发技术学习总结

Android开发,提供对蓝牙的通讯栈的支持,允许设别和其他的设备进行无线传输数据。应用程序层通过安卓API来调用蓝牙的相关功能,这些API使程序无线连接到蓝牙设备,并拥有P2P或者多端无线连接的特性。 蓝牙的功能: 1、扫描其他蓝牙设备 2、为可配对的蓝牙设备查询蓝牙适配器 3、建立RFCOMM通道(其实就是尼玛的认证) 4、通过服务搜索来链接其他的设备...

ListView实现下拉刷新功能

  很久没有写博客了,感觉都懒惰了,今天说一下一个自定义的空间,就是ListView下拉列表可以刷新的功能,相信很多同学都看到过这种功能,最典型的就是新浪微博的下拉刷新列表了。   废话不多说,首先呢,下拉刷新的那个带有progressBar的是ListView的headView,所以首先我们需要自定义一个HeadView,如下所示: 1 <?xm...

Android学习笔记(20)————利用ListView制作带竖线的多彩表格

http://blog.csdn.net/conowen/article/details/7421805 /********************************************************************************************* author:conowen@大钟 * E-mail:cono...

项目经理打分

02章《深入C#数据类型》项目经理评分   一:创建MyOffices项目,创建UserInfo类,用来存储员工 工号,姓名,年龄,评价,年度得分 二:创建查看评分窗体(frmShow),添加定义员工数组,将员工数据绑定到frmShow窗体的ListView控件上。运行结果如下: 实现思路: 长度为3的UserInfo类型数组,并初始化数组、赋值...