Flutter图片折叠效果插件image_collapse的使用

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

Flutter图片折叠效果插件image_collapse的使用

👏 Gallery Collapse

Open Source Love

Say Thanks!

这是一个用于根据与英雄动画相关的图片数量来展示库折叠效果的Flutter插件。

源代码为100% Dart,并且所有内容都位于 /lib 文件夹内。

🔅 Gifs

💻 安装

在你的 pubspec.yaml 文件的 dependencies: 部分添加以下行:

dependencies:
  gallery_collapse: <latest version>

❔ 使用

导入此类
import 'package:image_collapse/image_collapse.dart';
before after
ImageCollapse(
  imageUrls: listNetworkImages,
)

🎨 自定义和属性

所有可自定义的BeforeAfter Widget属性:

属性名称 示例值 描述
imageUrls (List<String>) [‘https://pub.dev/flutter_logo.png’] 设置来自网络的图片列表
titleGallery (String?) Gallery 点击图片时显示的标题
remainNumberStyle (TextStyle?) false 设置剩余图片数量的样式
crossAxisCount (int) 6.0 设置图片之间的跨轴计数
mainAxisSpacing (double) 4.0 设置主轴间距
crossAxisSpacing (double) 4.0 设置跨轴间距
backgroundImageView (BoxDecoration?) BoxDecoration(color: Color(0xff374056)) 设置点击图片后显示的背景颜色
fadingColorCollapse (Color?) 16.0 设置剩余图片数量的背景颜色
appBarColor (Color?) Colors.yellow 设置显示图片时的AppBar颜色

👍 如何贡献

  1. Fork 该项目
  2. 创建新分支(git checkout -b my-new-feature)
  3. 提交更改(git commit -am ‘Add some feature’)
  4. 推送到分支(git push origin my-new-feature)
  5. 创建新的Pull Request

Getting Started

要开始使用Flutter,请参阅我们的在线文档

要编辑包代码,请参阅文档

示例代码

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Gallery Collapse',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Gallery Collapse'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  static const List<String> listOfUrls = [
    "https://img.pikbest.com/png-images/plane-flying-on-location-and-map-with-forest-element_1493374.png!bw340",
    "https://img.pikbest.com/05/81/63/10VpIkbEsT5s3.jpg-0.jpg!bw340",
    "https://img.pikbest.com/01/42/43/44apIkbEsT5gu.jpg-0.jpg!bw340",
    "https://img.pikbest.com/pikbest/pic/00/32/88/08I888piCYQN.jpg-0.jpg!bw340",
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: const Color(0xff374056),
        title: Text(widget.title),
      ),
      body: const Center(
        child: ImageCollapse(
          imageUrls: listOfUrls,
        ),
      ),
    );
  }
}

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

1 回复

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


当然,下面是一个关于如何在Flutter项目中使用image_collapse插件来实现图片折叠效果的代码案例。image_collapse插件(假设存在,因为实际中可能需要自己实现或查找类似的插件)通常用于在滚动视图中实现图片的折叠和展开动画效果。

首先,确保在pubspec.yaml文件中添加依赖项(这里假设插件名为image_collapse,实际使用时请替换为真实插件名):

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

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

接下来,在您的Dart文件中使用ImageCollapse组件(假设该插件提供了一个名为ImageCollapse的组件)。以下是一个示例代码,展示了如何在ListView中实现图片折叠效果:

import 'package:flutter/material.dart';
import 'package:image_collapse/image_collapse.dart'; // 假设插件提供了这个import路径

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Image Collapse Example'),
        ),
        body: ListView.builder(
          itemCount: 10,
          itemBuilder: (context, index) {
            return Padding(
              padding: const EdgeInsets.symmetric(vertical: 8.0),
              child: ImageCollapse(
                image: NetworkImage('https://via.placeholder.com/300'), // 替换为实际图片URL
                collapsedHeight: 100, // 折叠时的高度
                expandedHeight: 300, // 展开时的高度
                header: Text('Image $index'), // 可选的头部内容
                onCollapseStateChanged: (isCollapsed) {
                  // 可选的回调,当折叠状态改变时触发
                  print('Image $index is ${isCollapsed ? 'collapsed' : 'expanded'}');
                },
              ),
            );
          },
        ),
      ),
    );
  }
}

// 假设image_collapse插件提供了ImageCollapse组件的定义如下(实际使用时请参考插件文档)
// class ImageCollapse extends StatefulWidget {
//   final ImageProvider image;
//   final double collapsedHeight;
//   final double expandedHeight;
//   final Widget header;
//   final ValueChanged<bool> onCollapseStateChanged;
//
//   ImageCollapse({
//     required this.image,
//     required this.collapsedHeight,
//     required this.expandedHeight,
//     this.header,
//     this.onCollapseStateChanged,
//   });
//
//   @override
//   _ImageCollapseState createState() => _ImageCollapseState();
// }
//
// class _ImageCollapseState extends State<ImageCollapse> with SingleTickerProviderStateMixin {
//   late AnimationController _controller;
//   late Animation<double> _animation;
//   bool _isCollapsed = true;
//
//   @override
//   void initState() {
//     super.initState();
//     _controller = AnimationController(
//       duration: const Duration(milliseconds: 300),
//       vsync: this,
//     )..addListener(() {
//       setState(() {});
//     });
//     _animation = Tween<double>(begin: widget.collapsedHeight, end: widget.expandedHeight).animate(_controller);
//   }
//
//   @override
//   void dispose() {
//     _controller.dispose();
//     super.dispose();
//   }
//
//   void _toggleCollapse() {
//     setState(() {
//       _isCollapsed = !_isCollapsed;
//       _controller.value = _isCollapsed ? 0.0 : 1.0;
//       if (widget.onCollapseStateChanged != null) {
//         widget.onCollapseStateChanged!(_isCollapsed);
//       }
//     });
//   }
//
//   @override
//   Widget build(BuildContext context) {
//     return GestureDetector(
//       onTap: _toggleCollapse,
//       child: Stack(
//         children: [
//           Positioned.fill(
//             child: AnimatedContainer(
//               duration: const Duration(milliseconds: 300),
//               height: _animation.value,
//               decoration: BoxDecoration(
//                 image: DecorationImage(image: widget.image, fit: BoxFit.cover),
//               ),
//             ),
//           ),
//           if (widget.header != null)
//             Positioned(
//               top: 0,
//               left: 0,
//               right: 0,
//               child: widget.header,
//             ),
//         ],
//       ),
//     );
//   }
// }

注意:上面的ImageCollapse类是一个假设的实现,实际使用时,请查阅image_collapse插件的官方文档以获取正确的使用方法和组件。如果找不到完全匹配的插件,您可能需要自定义实现上述功能。

在实际项目中,您可能需要根据具体需求调整动画、布局和交互逻辑。

回到顶部