Flutter可滚动标题图片卡片插件scrollable_titled_image_cards的使用
Flutter可滚动标题图片卡片插件scrollable_titled_image_cards的使用
概述
ScrollableTitledImageCards
是一个用于滚动显示带标题图片卡片的 Flutter 包。用户可以自定义卡片上的标题和其他信息。
开始使用
1. 添加依赖
首先,在你的 pubspec.yaml
文件中添加 scrollable_titled_image_cards
依赖:
dependencies:
scrollable_titled_image_cards: ^0.0.1
2. 安装依赖
在命令行中运行以下命令以安装依赖:
$ flutter pub get
3. 导入包
在你的项目文件中导入该包:
import 'package:scrollable_titled_image_cards/scrollable_titled_image_cards.dart';
使用方法
水平滚动视图
ScrollableTitledImageCards(
scrollDirection: Axis.horizontal, // 设置滚动方向为水平
imagesList: imagesList, // 图片列表
width: 250, // 卡片宽度
height: 350, // 卡片高度
onTap: (index) {
print('$index pressed'); // 点击卡片时触发的回调函数
},
overlays: [
for (var title in titlesList)
// 自定义叠加层,例如标题
Container(
color: Colors.black.withOpacity(0.6), // 半透明背景
constraints: const BoxConstraints.expand(), // 扩展到卡片大小
child: Center(
child: Text(
title,
style: const TextStyle(
fontSize: 24,
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
)
],
),
],
)
垂直滚动视图
ScrollableTitledImageCards(
imagesList: imagesList, // 图片列表
width: double.infinity, // 卡片宽度
height: 190, // 卡片高度
onTap: (index) {
print('$index pressed'); // 点击卡片时触发的回调函数
},
),
类、参数和叠加选项
类:ScrollableTitledImageCards
参数 | 描述 |
---|---|
imagesList (必需) | 代表卡片图片路径或URL的字符串列表 |
scrollDirection (可选,默认为 Axis.vertical) | 卡片的滚动方向,可以是 Axis.horizontal 或 Axis.vertical |
width (可选) | 每个卡片的宽度 |
height (可选) | 每个卡片的高度 |
margin (可选) | 应用到每个卡片的边距 |
borderRadius (可选) | 应用到每个卡片的圆角半径 |
overlays (可选) | 显示在图片顶部的叠加层列表,长度必须与 imagesList 相同 |
onTap (可选) | 当卡片被点击时触发的回调函数,接收点击卡片的索引作为参数 |
叠加选项
叠加层 | 描述 |
---|---|
TitleOverlay | 自定义标题叠加层 |
TitleSubtitleOverlay | 自定义标题和副标题叠加层 |
TitleSubtitleDescriptionOverlay | 自定义标题、副标题和描述叠加层 |
TitleDescriptionOverlay | 自定义标题和描述叠加层 |
DescriptionOverlay | 自定义描述叠加层 |
ClickableDescriptionOverlay | 状态叠加层,点击时显示描述 |
ClickableTitleDescriptionOverlay | 状态叠加层,点击时显示标题和描述 |
ClickableTitleSubtitleOverlay | 状态叠加层,点击时显示标题和副标题 |
ClickableTitleSubtitleDescriptionOverlay | 状态叠加层,点击时显示标题、副标题和描述 |
示例代码
以下是完整的示例代码:
import 'package:flutter/material.dart';
import 'package:scrollable_titled_image_cards/scrollable_titled_image_cards.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 Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
final List<String> imagesList = [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg',
];
final List<String> titlesList = [
'Title 1',
'Title 2',
'Title 3',
];
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Scrollable Titled Image Cards'),
),
body: Column(
children: [
// 水平滚动视图
ScrollableTitledImageCards(
scrollDirection: Axis.horizontal,
imagesList: imagesList,
width: 250,
height: 350,
onTap: (index) {
print('$index pressed');
},
overlays: [
for (var title in titlesList)
Container(
color: Colors.black.withOpacity(0.6),
constraints: const BoxConstraints.expand(),
child: Center(
child: Text(
title,
style: const TextStyle(
fontSize: 24,
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
)
],
],
),
// 垂直滚动视图
ScrollableTitledImageCards(
imagesList: imagesList,
width: double.infinity,
height: 190,
onTap: (index) {
print('$index pressed');
},
),
],
),
);
}
}
更多关于Flutter可滚动标题图片卡片插件scrollable_titled_image_cards的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter可滚动标题图片卡片插件scrollable_titled_image_cards的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
scrollable_titled_image_cards
是一个 Flutter 插件,用于创建带有标题和图片的可滚动卡片列表。它非常适合展示图片和相关信息,例如产品展示、新闻列表等。以下是如何使用 scrollable_titled_image_cards
插件的基本步骤。
1. 添加依赖
首先,在你的 pubspec.yaml
文件中添加 scrollable_titled_image_cards
依赖:
dependencies:
flutter:
sdk: flutter
scrollable_titled_image_cards: ^1.0.0 # 请根据最新版本号进行替换
然后运行 flutter pub get
来获取依赖。
2. 导入插件
在你的 Dart 文件中导入插件:
import 'package:scrollable_titled_image_cards/scrollable_titled_image_cards.dart';
3. 使用 ScrollableTitledImageCards
组件
你可以在你的 widget 树中使用 ScrollableTitledImageCards
组件。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:scrollable_titled_image_cards/scrollable_titled_image_cards.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Scrollable Titled Image Cards Example'),
),
body: ScrollableTitledImageCards(
cards: [
TitledImageCard(
image: AssetImage('assets/image1.jpg'),
title: 'Card 1',
onTap: () {
print('Card 1 tapped');
},
),
TitledImageCard(
image: AssetImage('assets/image2.jpg'),
title: 'Card 2',
onTap: () {
print('Card 2 tapped');
},
),
TitledImageCard(
image: AssetImage('assets/image3.jpg'),
title: 'Card 3',
onTap: () {
print('Card 3 tapped');
},
),
],
),
),
);
}
}
4. 配置卡片
ScrollableTitledImageCards
组件接受一个 cards
参数,它是一个 List<TitledImageCard>
。每个 TitledImageCard
可以配置以下属性:
image
: 卡片的图片,可以是AssetImage
或NetworkImage
。title
: 卡片的标题。onTap
: 点击卡片时的回调函数。
5. 自定义样式
你可以通过 ScrollableTitledImageCards
的其他属性来自定义样式,例如:
cardHeight
: 卡片的高度。cardWidth
: 卡片的宽度。cardMargin
: 卡片之间的间距。cardBorderRadius
: 卡片的圆角半径。titleStyle
: 标题的文本样式。titlePadding
: 标题的内边距。
例如:
ScrollableTitledImageCards(
cardHeight: 200.0,
cardWidth: 150.0,
cardMargin: EdgeInsets.all(8.0),
cardBorderRadius: BorderRadius.circular(10.0),
titleStyle: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
color: Colors.white,
),
titlePadding: EdgeInsets.all(8.0),
cards: [
// Your cards here
],
)