Flutter全屏图片展示插件custom_full_image_screen的使用

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

Flutter全屏图片展示插件custom_full_image_screen的使用

full_screen_image 包是一个新的 Flutter 包,用于实现全屏图片功能。

pub.dev

开始使用

你是否尝试过将图片设置为全屏?别担心,我们在这里帮助你。我们带来了一个新的库,简单易用,可扩展性强且易于集成。

预览

预览图

开发者

开发者联系信息:

完整示例代码

以下是使用 custom_full_image_screen 插件的完整示例代码:

import 'dart:io';

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

// 自定义 HTTP 覆盖类,处理 HTTPS 证书问题
class MyHttpOverrides extends HttpOverrides {
  [@override](/user/override)
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
  }
}

void main() {
  HttpOverrides.global = new MyHttpOverrides();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // 这个小部件是你的应用的根
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: '全屏图片示例'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, this.title}) : super(key: key);

  final String? title;

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

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title!),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                SizedBox(
                  height: 20,
                ),
                Text(
                  '图片资源弹出框',
                  style: TextStyle(color: Colors.blue, fontSize: 18),
                ),
                // 使用 ImageAssetsFullscreen 显示本地图片
                ImageAssetsFullscreen(
                  imagePath: 'assets/default_image.png',
                  imageBorderRadius: 200,
                  imageWidth: 200,
                  imageHeight: 200,
                  iconBackButtonColor: Colors.red,
                  imageDetailsHeight: 500,
                  imageDetailsWidth: 200,
                  hideBackButtonDetails: true,
                  appBarBackgroundColorDetails: Colors.red,
                  backgroundColorDetails: Colors.green,
                  imageDetailsFit: BoxFit.cover,
                  hideAppBarDetails: true,
                  imageFit: BoxFit.fill,
                  withHeroAnimation: true,
                ),
                Divider(
                  height: 25,
                ),
                Text(
                  '网络图片弹出框',
                  style: TextStyle(color: Colors.blue, fontSize: 18),
                ),
                // 使用 ImageNetworkFullscreen 显示网络图片
                ImageNetworkFullscreen(
                  imageUrl: 'https://picsum.photos/id/1/200/300',
                  imageBorderRadius: 20,
                  imageWidth: 200,
                  imageHeight: 200,
                ),
                Divider(
                  height: 25,
                ),
                Text(
                  '缓存图片弹出框',
                  style: TextStyle(color: Colors.blue, fontSize: 18),
                ),
                // 使用 ImageCachedFullscreen 显示缓存图片
                ImageCachedFullscreen(
                  imageUrl: 'https://picsum.photos/200/300',
                  imageBorderRadius: 20,
                  imageWidth: 200,
                  imageHeight: 200,
                  withHeroAnimation: true,
                  placeholder: Container(
                    child: Icon(Icons.check),
                  ),
                  errorWidget: Container(
                    child: Icon(Icons.error),
                  ),
                  placeholderDetails: Container(),
                ),
                Divider(
                  height: 25,
                ),
                Text(
                  '带有自定义选项的缓存图片弹出框',
                  style: TextStyle(color: Colors.blue, fontSize: 18),
                ),
                // 使用 ImageCachedFullscreen 显示带有自定义选项的缓存图片
                ImageCachedFullscreen(
                  imageUrl: 'https://picsum.photos/id/15/400/600',
                  imageBorderRadius: 20,
                  imageWidth: 200,
                  imageHeight: 200,
                  placeholder: Container(
                    child: Icon(Icons.check),
                  ),
                  errorWidget: Container(
                    child: Icon(
                      Icons.error,
                      size: 68,
                      color: Colors.red,
                    ),
                  ),
                  iconBackButtonColor: Colors.red,
                  imageDetailsHeight: 500,
                  imageDetailsWidth: 200,
                  hideBackButtonDetails: true,
                  appBarBackgroundColorDetails: Colors.red,
                  backgroundColorDetails: Colors.cyan,
                  imageDetailsFit: BoxFit.cover,
                  hideAppBarDetails: true,
                  imageFit: BoxFit.fill,
                  withHeroAnimation: true,
                  placeholderDetails: Center(child: CircularProgressIndicator()),
                ),
                SizedBox(
                  height: 20,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter项目中使用custom_full_image_screen插件来实现全屏图片展示的示例代码。首先,你需要确保你的Flutter项目中已经添加了这个插件。如果还没有添加,可以在你的pubspec.yaml文件中添加以下依赖:

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

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

接下来,你可以在你的Flutter应用中实现全屏图片展示。以下是一个简单的示例代码:

main.dart

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

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

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

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

class _MyHomePageState extends State<MyHomePage> {
  final List<String> imageUrls = [
    'https://example.com/image1.jpg',
    'https://example.com/image2.jpg',
    'https://example.com/image3.jpg',
    // 添加更多图片URL
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('全屏图片展示示例'),
      ),
      body: GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          crossAxisSpacing: 4.0,
          mainAxisSpacing: 4.0,
        ),
        itemCount: imageUrls.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => CustomFullImageScreen(
                    imageUrl: imageUrls[index],
                    // 可选参数:设置全屏图片展示的标题
                    title: '图片 ${index + 1}',
                  ),
                ),
              );
            },
            child: Image.network(
              imageUrls[index],
              fit: BoxFit.cover,
            ),
          );
        },
      ),
    );
  }
}

custom_full_image_screen.dart(插件实现示例,如果插件已经实现,则不需要此文件)

注意:通常情况下,你不需要自己实现插件,因为custom_full_image_screen插件应该已经包含了全屏图片展示的功能。但如果你有兴趣了解如何自己实现一个类似的功能,这里是一个基本的框架:

import 'package:flutter/material.dart';

class CustomFullImageScreen extends StatelessWidget {
  final String imageUrl;
  final String? title;

  CustomFullImageScreen({required this.imageUrl, this.title});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title ?? '全屏图片'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.close),
            onPressed: () => Navigator.pop(context),
          ),
        ],
      ),
      body: Center(
        child: Image.network(
          imageUrl,
          fit: BoxFit.cover,
          loadingBuilder: (context, widget, loadingProgress) {
            if (loadingProgress == null) return widget;
            return CircularProgressIndicator(
              value: loadingProgress.expectedTotalBytes != null
                  ? loadingProgress.cumulativeBytesLoaded.toDouble() /
                      loadingProgress.expectedTotalBytes!.toDouble()
                  : null,
            );
          },
        ),
      ),
    );
  }
}

然而,在大多数情况下,你只需要使用插件提供的CustomFullImageScreen组件,并传入相应的图片URL即可,如上面的main.dart示例所示。

确保你替换了示例中的图片URL为实际可用的图片URL,并运行你的Flutter应用以查看效果。

回到顶部