Flutter网格视图滑动选择插件swipe_select_grid_view的使用

发布于 1周前 作者 h691938207 来自 Flutter

Flutter网格视图滑动选择插件swipe_select_grid_view的使用

swipe_select_grid_view 是一个支持滑动和点击来选择项目的网格视图,类似于 iOS 照片应用。

安装

首先,在 pubspec.yaml 文件中添加依赖:

dependencies:
  swipe_select_grid_view: 1.1.1

然后运行 flutter pub get 下载依赖。

示例代码

以下是一个完整的示例代码,展示如何使用 swipe_select_grid_view 插件:

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

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Swipe select'),
        ),
        body: SwipeSelectGridView(
          padding: const EdgeInsets.all(10),
          itemCount: 100,
          itemBuilder: (context, index, selected) {
            // 构建每个网格项
            return Container(
              color: Colors.grey,
              child: Stack(
                children: [
                  // 显示默认图片
                  Image.asset('images/avatar.png'),
                  // 如果选中,则显示半透明背景
                  Visibility(
                    visible: selected,
                    child: Container(
                      color: const Color(0x66FFFFFF),
                    ),
                  ),
                  // 右上角显示选中标记
                  Align(
                    alignment: Alignment.topRight,
                    child: Visibility(
                      visible: selected,
                      child: SizedBox(
                        width: 50,
                        height: 50,
                        child: Center(
                          child: Image.asset(
                            'images/icon_selected.png',
                            width: 30,
                            height: 30,
                            fit: BoxFit.fill,
                            color: Colors.blue,
                          ),
                        ),
                      ),
                    ),
                  ),
                  // 右上角显示默认标记
                  Align(
                    alignment: Alignment.topRight,
                    child: Visibility(
                      visible: !selected,
                      child: SizedBox(
                        width: 50,
                        height: 50,
                        child: Center(
                          child: Image.asset(
                            'images/icon_default.png',
                            width: 30,
                            height: 30,
                            fit: BoxFit.fill,
                            color: Colors.grey,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            );
          },
          // 设置网格布局
          gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
            maxCrossAxisExtent: 150,
            crossAxisSpacing: 10,
            mainAxisSpacing: 10,
          ),
        ),
      ),
    );
  }
}

更多关于Flutter网格视图滑动选择插件swipe_select_grid_view的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter网格视图滑动选择插件swipe_select_grid_view的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter中使用swipe_select_grid_view插件的示例代码。这个插件允许你在网格视图中通过滑动来选择项目。请确保你已经在pubspec.yaml文件中添加了swipe_select_grid_view依赖,并运行flutter pub get来安装它。

首先,确保你的pubspec.yaml包含以下依赖:

dependencies:
  flutter:
    sdk: flutter
  swipe_select_grid_view: ^最新版本号  # 请替换为实际最新版本号

然后,你可以在你的Flutter项目中这样使用swipe_select_grid_view

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Swipe Select Grid View Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<String> items = List<String>.generate(25, (i) => "Item $i");
  List<int> selectedIndices = [];

  void onSelectionChanged(List<int> indices) {
    setState(() {
      selectedIndices = indices;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Swipe Select Grid View Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: SwipeSelectGridView(
          itemCount: items.length,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            crossAxisSpacing: 4.0,
            mainAxisSpacing: 4.0,
            childAspectRatio: 1.0,
          ),
          itemBuilder: (context, index) {
            bool isSelected = selectedIndices.contains(index);
            return Card(
              color: isSelected ? Colors.blueAccent : Colors.white,
              child: Center(
                child: Text(
                  items[index],
                  style: TextStyle(color: isSelected ? Colors.white : Colors.black),
                ),
              ),
            );
          },
          onSelectionChanged: onSelectionChanged,
        ),
      ),
    );
  }
}

代码说明:

  1. 依赖添加:确保在pubspec.yaml中添加了swipe_select_grid_view依赖。
  2. 主应用MyApp是主应用类,设置了应用的标题和主题。
  3. 主页MyHomePage是一个有状态的Widget,它包含了一个网格视图。
  4. 数据列表items列表包含了要在网格视图中显示的项。
  5. 选择状态selectedIndices列表用于存储用户选择的项的索引。
  6. 选择变化回调onSelectionChanged方法在用户选择变化时被调用,更新selectedIndices
  7. 网格视图SwipeSelectGridView用于创建可滑动的网格视图。
    • itemCount:项的总数。
    • gridDelegate:定义了网格布局,这里使用SliverGridDelegateWithFixedCrossAxisCount来创建固定列数的网格。
    • itemBuilder:构建每个网格项,根据是否选中改变颜色。
    • onSelectionChanged:用户选择变化时的回调。

运行这段代码,你将看到一个3列的网格视图,每个项可以通过滑动来选择,选中的项会高亮显示。这个示例展示了如何使用swipe_select_grid_view插件来创建一个基本的滑动选择网格视图。

回到顶部