列表显示的领军人物,容纳多个子组件,可以通过builder、speared、custom等构造。有内边距,是否反向、滑动控制器等属性。

相关组件

ListTile GridView

ListView基本使用

【children】 : 子组件列表 【List
【padding】 : 点击事件 【EdgeInsetsGeometry】
1.gif

  1. import 'package:flutter/material.dart';
  2. class CustomListView extends StatelessWidget {
  3. final data = <Color>[
  4. Colors.purple[50],
  5. Colors.purple[100],
  6. Colors.purple[200],
  7. Colors.purple[300],
  8. Colors.purple[400],
  9. Colors.purple[500],
  10. Colors.purple[600],
  11. Colors.purple[700],
  12. Colors.purple[800],
  13. Colors.purple[900],
  14. ];
  15. @override
  16. Widget build(BuildContext context) {
  17. return Container(
  18. height: 200,
  19. child: ListView(
  20. padding: EdgeInsets.symmetric(horizontal: 5),
  21. children: data
  22. .map((color) => Container(
  23. alignment: Alignment.center,
  24. width: 100,
  25. height: 50,
  26. color: color,
  27. child: Text(
  28. colorString(color),
  29. style: TextStyle(color: Colors.white, shadows: [
  30. Shadow(
  31. color: Colors.black,
  32. offset: Offset(.5, .5),
  33. blurRadius: 2)
  34. ]),
  35. ),
  36. ))
  37. .toList(),
  38. ),
  39. );
  40. }
  41. String colorString(Color color) =>
  42. "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
  43. }

ListView横向滑动

【scrollDirection】 : 滑动方向 【Axis】
【reverse】 : 是否反向滑动 【bool】
【shrinkWrap】 : 无边界时是否包裹 【bool】
2.gif

import 'package:flutter/material.dart';
class HorizontalListView extends StatelessWidget {
  final data = <Color>[
    Colors.purple[50],
    Colors.purple[100],
    Colors.purple[200],
    Colors.purple[300],
    Colors.purple[400],
    Colors.purple[500],
    Colors.purple[600],
    Colors.purple[700],
    Colors.purple[800],
    Colors.purple[900],
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: ListView(
        reverse: true,
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        children: data
            .map((color) => Container(
          alignment: Alignment.center,
          width: 100,
          height: 50,
          color: color,
          child: Text(
            colorString(color),
            style: TextStyle(color: Colors.white, shadows: [
              Shadow(
                  color: Colors.black,
                  offset: Offset(.5, .5),
                  blurRadius: 2)
            ]),
          ),
        ))
            .toList(),
      ),
    );
  }

  String colorString(Color color) =>
      "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
}

ListView.separated构造

【separatorBuilder】 : 条目构造器 【IndexedWidgetBuilder】
1.gif

import 'package:flutter/material.dart';
class SeparatedListView extends StatelessWidget {
  final data = <Color>[
    Colors.purple[50],
    Colors.purple[100],
    Colors.purple[200],
    Colors.purple[300],
    Colors.purple[400],
    Colors.purple[500],
    Colors.purple[600],
    Colors.purple[700],
    Colors.purple[800],
    Colors.purple[900],
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: ListView.separated(
        separatorBuilder: (context, index) => Divider(
          thickness: 1,
          height: 1,
          color: Colors.orange,
        ),
        itemCount: data.length,
        itemBuilder: (context, index) => _buildItem(data[index]),
      ),
    );
  }

  String colorString(Color color) =>
      "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";

  Widget _buildItem(Color color) => Container(
    alignment: Alignment.center,
    width: 100,
    height: 50,
    color: color,
    child: Text(
      colorString(color),
      style: TextStyle(color: Colors.white, shadows: [
        Shadow(color: Colors.black, offset: Offset(.5, .5), blurRadius: 2)
      ]),
    ),
  );
}

ListView.builder构造

【itemCount】 : 条目个数 【int】
【itemBuilder】 : 条目构造器 【IndexedWidgetBuilder】
3.gif

import 'package:flutter/material.dart';
class BuilderListView extends StatelessWidget {
  final data = <Color>[
    Colors.purple[50],
    Colors.purple[100],
    Colors.purple[200],
    Colors.purple[300],
    Colors.purple[400],
    Colors.purple[500],
    Colors.purple[600],
    Colors.purple[700],
    Colors.purple[800],
    Colors.purple[900],
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: ListView.builder(
        itemCount: data.length,
        itemBuilder: (context, index) => _buildItem(data[index]),
      ),
    );
  }

  String colorString(Color color) =>
      "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";

  Widget _buildItem(Color color) => Container(
    alignment: Alignment.center,
    width: 100,
    height: 50,
    color: color,
    child: Text(
      colorString(color),
      style: TextStyle(color: Colors.white, shadows: [
        Shadow(color: Colors.black, offset: Offset(.5, .5), blurRadius: 2)
      ]),
    ),
  );
}