flutter-listview

作者: iPhone | 来源:发表于2019-07-02 16:11 被阅读0次
import 'package:flutter/material.dart';

void main() => runApp(MyApp2());

class MyApp extends StatelessWidget {

  List<String> strItems = <String>[
    '图标 -> keyboard', '图标 -> print',
    '图标 -> router', '图标 -> pages',
    '图标 -> zoom_out_map', '图标 -> zoom_out',
    '图标 -> youtube_searched_for', '图标 -> wifi_tethering',
    '图标 -> wifi_lock', '图标 -> widgets',
    '图标 -> weekend', '图标 -> web',
    '图标 -> accessible', '图标 -> ac_unit',
  ];

  List<Icon> iconItems = <Icon>[
    new Icon(Icons.keyboard), new Icon(Icons.print),
    new Icon(Icons.router), new Icon(Icons.pages),
    new Icon(Icons.zoom_out_map), new Icon(Icons.zoom_out),
    new Icon(Icons.youtube_searched_for), new Icon(Icons.wifi_tethering),
    new Icon(Icons.wifi_lock), new Icon(Icons.widgets),
    new Icon(Icons.weekend), new Icon(Icons.web),
    new Icon(Icons.accessible), new Icon(Icons.ac_unit),
  ];

  Widget buildlistData(BuildContext context, String str, Icon icon) => ListTile(

    isThreeLine: false,
    leading: icon,
    title: Text(str),
    trailing: Icon(Icons.keyboard_arrow_right),
    onTap: (){
      showDialog(context: context,
        builder: (BuildContext context){
          return AlertDialog(
            content: Text('您选择的item内容为:$str'),
            title: Text('demo'),
          );
        },
      );
    },

  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return buildlistData(context, strItems[index], iconItems[index]);
          },
          itemCount: iconItems.length,
        ),
      ),
    );
  }
}

class MyApp1 extends StatelessWidget {

  List<String> strItems = <String>[
    '图标 -> keyboard', '图标 -> print',
    '图标 -> router', '图标 -> pages',
    '图标 -> zoom_out_map', '图标 -> zoom_out',
    '图标 -> youtube_searched_for', '图标 -> wifi_tethering',
    '图标 -> wifi_lock', '图标 -> widgets',
    '图标 -> weekend', '图标 -> web',
    '图标 -> accessible', '图标 -> ac_unit',
  ];

  List<Icon> iconItems = <Icon>[
    new Icon(Icons.keyboard), new Icon(Icons.print),
    new Icon(Icons.router), new Icon(Icons.pages),
    new Icon(Icons.zoom_out_map), new Icon(Icons.zoom_out),
    new Icon(Icons.youtube_searched_for), new Icon(Icons.wifi_tethering),
    new Icon(Icons.wifi_lock), new Icon(Icons.widgets),
    new Icon(Icons.weekend), new Icon(Icons.web),
    new Icon(Icons.accessible), new Icon(Icons.ac_unit),
  ];

  Widget buildlistData(BuildContext context, String str, Icon icon) => ListTile(

    isThreeLine: false,
    leading: icon,
    title: Text(str),
    trailing: Icon(Icons.keyboard_arrow_right),
    onTap: (){
      showDialog(context: context,
        builder: (BuildContext context){
          return AlertDialog(
            content: Text('您选择的item内容为:$str'),
            title: Text('demo'),
          );
        },
      );
    },
  );

  @override
  Widget build(BuildContext context) {
    List<Widget> _list = new List();
    for(int i = 0; i < strItems.length; i++){
      _list.add(buildlistData(context, strItems[i], iconItems[i]));
    }
    var divideList = ListTile.divideTiles(tiles: _list,context: context).toList();
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Scrollbar(
          child: ListView(
//            children: _list,
          children: divideList,
          ),
        ),
      ),
    );
  }
}

class MyApp2 extends StatelessWidget {

  List<String> strItems = <String>[
    '图标 -> keyboard', '图标 -> print',
    '图标 -> router', '图标 -> pages',
    '图标 -> zoom_out_map', '图标 -> zoom_out',
    '图标 -> youtube_searched_for', '图标 -> wifi_tethering',
    '图标 -> wifi_lock', '图标 -> widgets',
    '图标 -> weekend', '图标 -> web',
    '图标 -> accessible', '图标 -> ac_unit',
  ];

  List<Icon> iconItems = <Icon>[
    new Icon(Icons.keyboard), new Icon(Icons.print),
    new Icon(Icons.router), new Icon(Icons.pages),
    new Icon(Icons.zoom_out_map), new Icon(Icons.zoom_out),
    new Icon(Icons.youtube_searched_for), new Icon(Icons.wifi_tethering),
    new Icon(Icons.wifi_lock), new Icon(Icons.widgets),
    new Icon(Icons.weekend), new Icon(Icons.web),
    new Icon(Icons.accessible), new Icon(Icons.ac_unit),
  ];

  Widget buildlistData(BuildContext context, String str, Icon icon) => ListTile(

    isThreeLine: false,
    leading: icon,
    title: Text(str),
    trailing: Icon(Icons.keyboard_arrow_right),
    onTap: (){
      showDialog(context: context,
        builder: (BuildContext context){
          return AlertDialog(
            content: Text('您选择的item内容为:$str'),
            title: Text('demo'),
          );
        },
      );
    },
  );

  @override
  Widget build(BuildContext context) {
    List<Widget> _list = new List();
    for(int i = 0; i < strItems.length; i++){
      _list.add(buildlistData(context, strItems[i], iconItems[i]));
    }
    var divideList = ListTile.divideTiles(tiles: _list,context: context).toList();
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: ListView.separated(
            itemBuilder: (context,item){
              return buildlistData(context, strItems[item], iconItems[item]);
            },
            separatorBuilder: (context,index){
              return Divider();
            },
            itemCount: iconItems.length
        ),
      ),
    );
  }
}

相关文章

网友评论

    本文标题:flutter-listview

    本文链接:https://www.haomeiwen.com/subject/ngqthctx.html