Flutter卡片展示插件card_package的使用

Flutter卡片展示插件card_package的使用

功能

  • 在Flutter中创建信用卡。
  • 自定义你的信用卡。

使用

/example文件夹内有一个如何使用该包的例子。

额外信息

此包是从symforms原始包继承而来,并进行了一些定制化修改,以便在我的项目中使用。

示例代码

import 'package:card_package/card_package.dart';
import 'package:card_package/glassmorphism_config.dart';
import 'package:flutter/material.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(
        primarySwatch: Colors.blue,
      ),
      initialRoute: 'page1',
      routes: {
        'page1': ((context) => const Page1()),
      },
    );
  }
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    final height = MediaQuery.of(context).size.height;

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('Page1'),
        backgroundColor: Colors.transparent,
      ),
      body: Center(
        child: CustomCreditCardWidget(
          padding: 40, // 设置卡片边距
          height: height * 0.215, // 设置卡片高度
          cardBgColor: Colors.green, // 设置卡片背景颜色
          obscureCardNumber: false, // 不隐藏卡号
          obscureCardCvv: false, // 不隐藏CVV码
          chipSizedBoxHeight: 20, // 设置芯片高度
          sizedBoxCardName: 10, // 设置卡名大小
          sizedBoxCardNumber: 10, // 设置卡号大小
          cardNumber: '4444 4444 4444 4444', // 卡号
          expiryDate: '05/26', // 到期日期
          textStyle: Theme.of(context)
              .textTheme
              .labelLarge!
              .copyWith(fontSize: 20, color: Colors.red), // 设置文本样式
          cardHolderName: 'Santiago Albisser', // 持卡人姓名
          glassmorphismConfig: Glassmorphism( // 设置玻璃效果
            blurX: 40.0,
            blurY: 25.0,
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [Colors.blueGrey, Colors.blue],
              stops: const [0.5, 0],
            ),
          ),
          isHolderNameVisible: false, // 是否显示持卡人姓名
          isChipVisible: true, // 是否显示芯片
          cvvCode: '424', // CVV码
          showBackView: false, // 是否显示背面视图
          onCreditCardWidgetChange: (CreditCardBrand) {}, // 当需要显示CVV(背面)视图时触发
        ),
      ),
    );
  }
}

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

1 回复

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


card_package 是一个用于在 Flutter 中轻松创建和展示卡片的插件。它提供了一些预定义的卡片样式和布局选项,使开发者能够快速构建美观的卡片界面。以下是如何使用 card_package 的基本步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 card_package 依赖。假设你已经知道插件的版本,可以像这样添加:

dependencies:
  flutter:
    sdk: flutter
  card_package: ^1.0.0  # 请使用最新版本

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

2. 导入包

在你的 Dart 文件中导入 card_package

import 'package:card_package/card_package.dart';

3. 使用卡片组件

card_package 提供了几种不同类型的卡片组件,你可以根据需求选择合适的组件。

基本卡片

CardWidget(
  title: 'Card Title',
  subtitle: 'This is a subtitle',
  description: 'This is a description of the card. It can be longer and more detailed.',
  imageUrl: 'https://example.com/image.jpg',
  onPressed: () {
    print('Card Pressed');
  },
);

带图标的卡片

IconCardWidget(
  icon: Icons.star,
  title: 'Icon Card',
  subtitle: 'This card has an icon',
  description: 'This is a description of the icon card.',
  onPressed: () {
    print('Icon Card Pressed');
  },
);

带按钮的卡片

ButtonCardWidget(
  title: 'Button Card',
  subtitle: 'This card has a button',
  description: 'This is a description of the button card.',
  buttonText: 'Click Me',
  onButtonPressed: () {
    print('Button Pressed');
  },
);

4. 自定义卡片

如果你需要更多的自定义选项,可以使用 CardWidget 并提供自定义的 child

CardWidget(
  title: 'Custom Card',
  subtitle: 'This is a custom card',
  child: Column(
    children: [
      Text('Custom Content'),
      ElevatedButton(
        onPressed: () {
          print('Custom Button Pressed');
        },
        child: Text('Custom Button'),
      ),
    ],
  ),
);

5. 运行应用

现在你可以运行你的 Flutter 应用,查看 card_package 提供的卡片组件。

6. 进一步自定义

如果你需要进一步自定义卡片的外观,可以查看 card_package 的文档,了解更多的属性和样式选项。

示例代码

以下是一个完整的示例代码,展示了如何使用 card_package 中的不同卡片组件:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Card Package Demo'),
        ),
        body: ListView(
          children: [
            CardWidget(
              title: 'Basic Card',
              subtitle: 'This is a basic card',
              description: 'This is a description of the basic card.',
              imageUrl: 'https://example.com/image.jpg',
              onPressed: () {
                print('Basic Card Pressed');
              },
            ),
            IconCardWidget(
              icon: Icons.star,
              title: 'Icon Card',
              subtitle: 'This card has an icon',
              description: 'This is a description of the icon card.',
              onPressed: () {
                print('Icon Card Pressed');
              },
            ),
            ButtonCardWidget(
              title: 'Button Card',
              subtitle: 'This card has a button',
              description: 'This is a description of the button card.',
              buttonText: 'Click Me',
              onButtonPressed: () {
                print('Button Pressed');
              },
            ),
            CardWidget(
              title: 'Custom Card',
              subtitle: 'This is a custom card',
              child: Column(
                children: [
                  Text('Custom Content'),
                  ElevatedButton(
                    onPressed: () {
                      print('Custom Button Pressed');
                    },
                    child: Text('Custom Button'),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部