• flutter ListView横向列表&不定长列表&网格


    ListView横向列表

    效果:

    (可以左右滑动)

    代码形式1:

    main.dart

    import 'package:flutter/material.dart';
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'dasldjalsdjasldj',
          home: Scaffold(
            appBar: new AppBar(title: new Text('new texrdasdasd'),),
            body: Center(
              child: Container(
                height: 200.0,
                child: new ListView(
                  scrollDirection: Axis.horizontal,
                  children: [
                    new Container(
                       120,
                      color: Colors.lime,
                    ),
                    new Container(
                       120,
                      color: Colors.pinkAccent,
                    ),
                    new Container(
                       120,
                      color: Colors.purpleAccent,
                    ),
                    new Container(
                       120,
                      color: Colors.deepPurple,
                    ),                                                
                  ],
                ),
              ),
            )
          ),
        );
      }
    }
    
    void main() {
      runApp(MyApp());
    }

    代码形式2:

    main.dart

    import 'package:flutter/material.dart';
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'dasldjalsdjasldj',
          home: Scaffold(
            appBar: new AppBar(title: new Text('new texrdasdasd'),),
            body: Center(
              child: Container(
                height: 200.0,
                child: Mylist(),
              ),
            )
          ),
        );
      }
    }
    
    
    class Mylist extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return ListView(
          scrollDirection: Axis.horizontal,
          children: [
            new Container(
               120,
              color: Colors.lime,
            ),
            new Container(
               120,
              color: Colors.pinkAccent,
            ),
            new Container(
               120,
              color: Colors.purpleAccent,
            ),
            new Container(
               120,
              color: Colors.deepPurple,
            ),
          ],
        );
      }
    }
    void main() {
      runApp(MyApp());
    }

    ListView不定长列表

    效果:

    代码:

    main.dart

    import 'package:flutter/material.dart';
    
    class MyApp extends StatelessWidget {
    
      final List<String> items;
      MyApp({required this.items});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'dasldjalsdjasldj',
          home: Scaffold(
            appBar: new AppBar(title: new Text('new texrdasdasd'),),
            body: new ListView.builder(
              itemCount: items.length,
              itemBuilder: (context, index) {
              return new ListTile(
                title: new Text('${items[index]}'),
              );
            })
          ),
        );
      }
    }
    void main() {
      runApp(MyApp(items: new List<String>.generate(20, (index) => "物品 $index"),));
    }

    网格

    import 'package:flutter/material.dart';
    
    class MyApp extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'dasldjalsdjasldj',
          home: Scaffold(
            appBar: new AppBar(title: new Text('new texrdasdasd'),),
            body: GridView.count(
              crossAxisCount: 3,
              children: [
                const Text('hello'),
                const Text('23'),
                const Text('2323'),
                const Text('323'),
                const Text('he2323llo'),
                const Text('h322ello'),
                const Text('he23llo'),
                const Text('he23llo'),
                const Text('2323'),
                const Text('LAST'),
              ],)
          ),
        );
      }
    }
    void main() {
      runApp(MyApp());
    }
  • 相关阅读:
    双链表
    单链表
    二叉树的遍历
    leetcode-9. 回文数
    leetcode-8. 字符串转换整数 (atoi)
    leetcode-7. 整数反转
    leetcode-6. Z 字形变换
    leetcode-5. 最长回文子串
    manacher-线性查找算法-(最长回文子串问题)
    bfprt-线性查找算法-(topK问题)
  • 原文地址:https://www.cnblogs.com/xkxf/p/15477729.html
Copyright © 2020-2023  润新知