Flutter工具类插件ccci_utils的使用

Flutter工具类插件ccci_utils的使用

CCCI Utils

本文档提供了CCCI Utils的概述,包括自定义小部件、实用类和网络处理。

目录

  1. 自定义小部件

  2. 实用类

自定义小部件

CCard小部件

CCard小部件是一个可自定义样式的卡片小部件,提供了诸如内边距、外边距、圆角半径、轮廓、渐变和背景颜色等灵活的样式选项。它旨在用于显示具有自定义样式的卡片内容。

使用方法:

CCard(
  child: Text('This is a card content'),
  borderRadius: BorderRadius.circular(10),
  color: Colors.white,
  hasOutline: true,
  outlineColor: Colors.grey,
);

CButton小部件

CButton小部件是一个支持加载状态、轮廓样式和渐变的小部件。它通过提供加载状态、轮廓样式和渐变背景选项来扩展了ElevatedButton的功能。它利用了CCard小部件作为其背景和样式。

使用方法:

CButton(
  label: 'Submit',
  onPressed: () {
    // 处理按钮点击事件
  },
  isLoading: false,
  isOutline: false,
  gradient: LinearGradient(
    colors: [Colors.blue, Colors.green],
  ),
);

CircularLoading小部件

CircularLoading小部件是一个类似于CupertinoActivityIndicator的圆形加载指示器。它用于在应用程序中指示加载状态。它将加载指示器封装在一个居中的容器中,以便于集成。

使用方法:

CircularLoading(
  color: Colors.blue,
  strokeWidth: 3,
);

实用类

AppLogger

AppLogger类是一个用于在应用程序中以不同严重级别记录消息的实用类。它提供了用于记录调试、信息、警告、错误和详细消息的静态方法。此外,它还包括了用于记录API请求、响应和错误的方法。

使用方法:

AppLogger.debug('Debug message');
AppLogger.error('Error message');
AppLogger.logApiRequest(url: 'https://api.example.com', method: 'GET');

RouteHandler

RouteHandler类是一个用于处理应用程序内部导航操作的实用类。它提供了用于在导航堆栈中推送、弹出和替换路由的方法。这些方法有助于在应用程序的不同屏幕和组件之间进行无缝导航。

使用方法:

RouteHandler.push(context, HomePage());
RouteHandler.pop(context);

DioExceptions

DioExceptions类是一个处理来自Dio请求的错误的自定义异常类。它根据遇到的不同类型的Dio异常提供了定制的错误消息,包括连接超时、接收超时、不良响应和未知错误。

使用方法:

try {
  // Dio请求
} catch (e) {
  if (e is DioException) {
    final dioException = DioExceptions.fromDioError(e);
    print(dioException.message);
  }
}

DioClient

DioClient类封装了使用Dio包进行HTTP请求的功能。它包括了带有可配置选项和拦截器的GET、POST、PUT和DELETE请求方法。该类提供了一种方便且高效的方式来与API交互并在应用程序中处理网络请求。

Dart依赖项

要使用DioClient,你需要在你的pubspec.yaml文件中包含以下Dart包:

dependencies:
    dio
    flutter_dotenv

配置文件

确保你有必要的配置文件:

  1. .env

    使用方法:

    BASE_URL=<your_base_url>
    
  2. config.json

    使用方法:

    {
      "development": {
        "current": true,
        "baseUrl": "https://dev.example.com"
      },
      "production": {
        "current": false,
        "baseUrl": "https://api.example.com"
      }
    }
    

更多关于Flutter工具类插件ccci_utils的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter工具类插件ccci_utils的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


ccci_utils 是一个 Flutter 插件,提供了一些常用的工具类和方法,可以帮助开发者更方便地进行 Flutter 应用开发。以下是一些常见的使用方法和示例:

1. 添加依赖

首先,在你的 pubspec.yaml 文件中添加 ccci_utils 依赖:

dependencies:
  ccci_utils: ^1.0.0 # 请使用最新版本

然后运行 flutter pub get 来获取依赖包。

2. 常用工具类和方法

2.1 字符串工具类

StringUtils 提供了一些常用的字符串处理方法。

import 'package:ccci_utils/ccci_utils.dart';

void main() {
  String str = "  Hello, World!  ";

  // 去除前后空格
  String trimmedStr = StringUtils.trim(str);
  print(trimmedStr); // 输出: "Hello, World!"

  // 判断字符串是否为空
  bool isEmpty = StringUtils.isEmpty(str);
  print(isEmpty); // 输出: false

  // 判断字符串是否为数字
  bool isNumeric = StringUtils.isNumeric("123");
  print(isNumeric); // 输出: true
}

2.2 日期工具类

DateUtils 提供了一些常用的日期处理方法。

import 'package:ccci_utils/ccci_utils.dart';

void main() {
  DateTime now = DateTime.now();

  // 格式化日期
  String formattedDate = DateUtils.formatDate(now, "yyyy-MM-dd HH:mm:ss");
  print(formattedDate); // 输出: "2023-10-05 14:30:00"

  // 获取当前时间戳
  int timestamp = DateUtils.getCurrentTimestamp();
  print(timestamp); // 输出: 1696504200

  // 时间戳转换为日期
  DateTime dateFromTimestamp = DateUtils.fromTimestamp(timestamp);
  print(dateFromTimestamp); // 输出: 2023-10-05 14:30:00.000
}

2.3 网络工具类

NetworkUtils 提供了一些常用的网络请求方法。

import 'package:ccci_utils/ccci_utils.dart';

void main() async {
  // GET 请求
  String url = "https://jsonplaceholder.typicode.com/posts";
  var response = await NetworkUtils.get(url);
  print(response);

  // POST 请求
  var postResponse = await NetworkUtils.post(url, body: {
    "title": "foo",
    "body": "bar",
    "userId": 1,
  });
  print(postResponse);
}

2.4 文件工具类

FileUtils 提供了一些常用的文件操作方法。

import 'package:ccci_utils/ccci_utils.dart';

void main() async {
  // 读取文件
  String path = "example.txt";
  String content = await FileUtils.readFile(path);
  print(content);

  // 写入文件
  await FileUtils.writeFile(path, "Hello, Flutter!");
}

2.5 加密工具类

EncryptUtils 提供了一些常用的加密方法。

import 'package:ccci_utils/ccci_utils.dart';

void main() {
  String text = "Hello, Flutter!";

  // MD5 加密
  String md5 = EncryptUtils.md5(text);
  print(md5);

  // Base64 编码
  String base64 = EncryptUtils.encodeBase64(text);
  print(base64);

  // Base64 解码
  String decodedText = EncryptUtils.decodeBase64(base64);
  print(decodedText);
}
回到顶部