Flutter图片展示插件flutter_lightbox的使用

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

Flutter图片展示插件flutter_lightbox的使用

flutter_lightbox 是一个简单易用的Flutter插件,用于展示图片,并支持滑动切换、缩略图点击等功能。本文将详细介绍如何使用该插件。

安装

首先,您需要在项目中添加 flutter_lightbox 依赖:

flutter pub add flutter_lightbox

然后,在Dart代码中导入该插件:

import 'package:flutter_lightbox/flutter_lightbox.dart';

功能特性

  • 支持本地资源图片(Image.Asset)和网络图片(Image.Network)
  • 左右滑动切换图片
  • 切换动画效果
  • 可点击的缩略图

示例Demo

下面是一个完整的示例代码,展示了如何使用 flutter_lightbox 插件来展示图片。

示例代码

import 'package:flutter/material.dart';
import 'package:flutter_lightbox/flutter_lightbox.dart';
import 'package:flutter_lightbox/image_type.dart';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter LightBox Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter LightBox Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<String> images = [
    'https://picsum.photos/250?image=10',
    'https://picsum.photos/250?image=13',
    'https://picsum.photos/250?image=15',
  ];

  final List<String> assetImages = [
    'assets/images/pikachu_1.png',
    'assets/images/pikachu_2.png',
    'assets/images/pikachu_3.png',
  ];

  Widget buildImageList(List<String> images, ImageType imageType) {
    return SizedBox(
      height: 85,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: images.length,
        itemBuilder: (BuildContext context, int index) {
          return InkWell(
            onTap: () {
              showGeneralDialog(
                context: context,
                pageBuilder: (BuildContext context, Animation animation,
                    Animation secondaryAnimation) {
                  return LightBox(
                    initialIndex: index,
                    images: images,
                    imageType: imageType,
                  );
                },
              );
            },
            child: Container(
              width: 70,
              height: 65,
              decoration: BoxDecoration(
                border: Border.all(
                  color: const Color.fromRGBO(246, 246, 246, 1),
                  width: 2,
                ),
                borderRadius: BorderRadius.circular(5),
              ),
              child: FittedBox(
                fit: BoxFit.contain,
                child: imageType == ImageType.network
                    ? Image.network(images[index])
                    : Image.asset(images[index]),
              ),
            ),
          );
        },
      ),
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            const Text("Network Images Example"),
            buildImageList(images, ImageType.network),
            const SizedBox(height: 30),
            const Text("Asset Images Example"),
            buildImageList(assetImages, ImageType.imageAsset),
            const SizedBox(height: 30),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter图片展示插件flutter_lightbox的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter图片展示插件flutter_lightbox的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中使用flutter_lightbox插件来展示图片的示例代码。flutter_lightbox是一个流行的Flutter插件,用于创建图片弹窗展示功能。

首先,你需要在pubspec.yaml文件中添加flutter_lightbox依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_lightbox: ^0.8.0  # 请检查最新版本号并替换

然后运行flutter pub get来安装依赖。

接下来,我们来看一个完整的示例代码,展示如何使用flutter_lightbox来展示图片。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Lightbox Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final List<String> imageUrls = [
    'https://via.placeholder.com/600x400?text=Image+1',
    'https://via.placeholder.com/600x400?text=Image+2',
    'https://via.placeholder.com/600x400?text=Image+3',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Lightbox Example'),
      ),
      body: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 4.0,
          mainAxisSpacing: 4.0,
        ),
        itemCount: imageUrls.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => Lightbox(
                    images: imageUrls.map((url) => LightboxImage.network(url)).toList(),
                    initialImageIndex: index,
                  ),
                ),
              );
            },
            child: Image.network(
              imageUrls[index],
              fit: BoxFit.cover,
            ),
          );
        },
      ),
    );
  }
}

解释

  1. 依赖安装:在pubspec.yaml中添加flutter_lightbox依赖,并运行flutter pub get

  2. 主函数MyApp是我们的主应用类,它包含一个MaterialApp,并设置了一个主题和主页MyHomePage

  3. 图片列表:在MyHomePage类中,我们定义了一个包含图片URL的列表imageUrls

  4. GridView:使用GridView.builder来创建一个网格视图,每个单元格显示一张图片。SliverGridDelegateWithFixedCrossAxisCount用于定义网格布局。

  5. 点击事件:在GestureDetectoronTap回调中,我们创建一个新的路由,并使用Lightbox来展示图片弹窗。Lightbox接收一个图片列表images和初始图片索引initialImageIndex

  6. LightboxImage:我们使用LightboxImage.network来加载网络图片。

这个示例展示了如何在一个网格视图中显示图片,并在点击图片时弹出一个包含图片的全屏弹窗。你可以根据需要调整代码,例如使用本地图片而不是网络图片,或者添加更多的自定义样式和功能。

回到顶部