flutter_imagecards
是一个用于在 Flutter 应用中展示图片卡片的插件。它可以帮助你快速创建一个美观的图片卡片布局,支持自定义图片、标题、描述等信息。
安装
首先,你需要在 pubspec.yaml
文件中添加 flutter_imagecards
依赖:
dependencies:
flutter:
sdk: flutter
flutter_imagecards: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
基本用法
flutter_imagecards
提供了 ImageCard
组件,你可以通过简单的配置来展示图片卡片。
import 'package:flutter/material.dart';
import 'package:flutter_imagecards/flutter_imagecards.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Image Cards Example'),
),
body: ListView(
children: [
ImageCard(
imageProvider: AssetImage('assets/image1.jpg'), // 图片资源
title: 'Card Title 1', // 卡片标题
description: 'This is a description for the first card.', // 卡片描述
onTap: () {
print('Card 1 tapped'); // 点击事件
},
),
ImageCard(
imageProvider: NetworkImage('https://via.placeholder.com/150'), // 网络图片
title: 'Card Title 2',
description: 'This is a description for the second card.',
onTap: () {
print('Card 2 tapped');
},
),
],
),
),
);
}
}
自定义属性
ImageCard
提供了多个属性来定制卡片的外观和行为:
imageProvider
: 图片资源,可以是 AssetImage
或 NetworkImage
。
title
: 卡片的标题。
description
: 卡片的描述。
onTap
: 卡片的点击事件回调。
backgroundColor
: 卡片的背景颜色。
titleStyle
: 标题的文本样式。
descriptionStyle
: 描述的文本样式。
borderRadius
: 卡片的圆角半径。
elevation
: 卡片的阴影高度。
示例代码
以下是一个更完整的示例,展示了如何使用 ImageCard
组件:
import 'package:flutter/material.dart';
import 'package:flutter_imagecards/flutter_imagecards.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Image Cards Example'),
),
body: ListView(
children: [
ImageCard(
imageProvider: AssetImage('assets/image1.jpg'),
title: 'Card Title 1',
description: 'This is a description for the first card.',
onTap: () {
print('Card 1 tapped');
},
backgroundColor: Colors.blue[50],
titleStyle: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.blue[900],
),
descriptionStyle: TextStyle(
fontSize: 16,
color: Colors.blue[800],
),
borderRadius: BorderRadius.circular(10),
elevation: 5,
),
ImageCard(
imageProvider: NetworkImage('https://via.placeholder.com/150'),
title: 'Card Title 2',
description: 'This is a description for the second card.',
onTap: () {
print('Card 2 tapped');
},
backgroundColor: Colors.green[50],
titleStyle: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.green[900],
),
descriptionStyle: TextStyle(
fontSize: 16,
color: Colors.green[800],
),
borderRadius: BorderRadius.circular(10),
elevation: 5,
),
],
),
),
);
}
}