iT邦幫忙

2022 iThome 鐵人賽

DAY 27
1

前言

今天要介紹flutter cookbook裡的list技巧,list相關的技巧也非常容易被使用到

Create a grid list

有時候可能會想用grid layout的方式顯示list,在flutter裡如果想用grid layout顯示widget的話,使用GridView widget非常方便,使用grid最簡單的方法是使用GridView.count()構造函數,因為它可以讓你指定所需的row數或column數

GridView.count(
  // Create a grid with 2 columns. If you change the scrollDirection to
  // horizontal, this produces 2 rows.
  crossAxisCount: 2,
  // Generate 100 widgets that display their index in the List.
  children: List.generate(100, (index) {
    return Center(
      child: Text(
        'Item $index',
        style: Theme.of(context).textTheme.headline5,
      ),
    );
  }),
),

example

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    const title = 'Grid List';

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

output

gridList

參考資料:
https://docs.flutter.dev/cookbook/lists/grid-lists


上一篇
dart&flutter學習之旅-Day26
下一篇
dart&flutter學習之旅-Day28
系列文
dart&flutter學習之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言