Flutter工具集插件es_utils的功能使用

ES-Utils for Flutter

一个用于几个UI组件的Flutter包,如确认对话框、输入对话框、按钮等

pub package

Flutter CI

开发所需的工具

Visual Studio Code

Visual Studio Code扩展使用的工具

Dart

Flutter

使用的语言

Flutter

Flutter包依赖项

URL Launcher

截图

确认对话框   输入对话框   列表对话框   主界面   进度对话框

确认对话框

ESButton(
    'Show Confirm Dialog',
    onPressed: () => ESMessage.showConfirmDialog(
        context: context,
        title: 'Confirm Dialog',
        message: 'Are you sure?',
        buttonLeft: 'OK',
        buttonRight: 'Cancel',
    ),
)

列表对话框

final List<bool> varList = [false, false, true, true, false];
ESButton(
    'List Dialog',
    onPressed: () => ESMessage.showListDialog(
        context: context,
        suffixText: 'Rows',
        selectedValue: 20,
        title: 'Row Count',
        multiSelectValues: varList,
        options: const [10, 20, 30, 50, 100],
    ),
)

输入对话框

ESButton(
    'Show Input Dialog',
    onPressed: () => ESMessage.showInputDialog(
        context: context,
        title: 'Title',
        defaultValue: 'Default Value',
        hitText: 'Enter Value',
        onSubmit: (TextEditingController controller) => ESMessage.showInfoMessage(
        context,
        message: controller.text,
        ),
    ),
)

完整示例代码

以下是一个完整的示例代码,展示如何使用es_utils插件中的各种功能。

/*
 * Developed by Ajmal Muhammad P
 * Contact me @ support@erratums.com
 * Date created: 09-May-2021
 */

import 'package:es_utils/es_utils.dart';
import 'package:flutter/material.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: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const MyHomePage(),
    );
  }
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    final List<bool> varList = [false, false, true, true, false];
    return Scaffold(
      appBar: AppBar(
        title: const Text('ES-Utils Demo'),
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(
            horizontal: ESPlatform.isSmallScreen(context) ? 50 : 30),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              ESButton(
                'Progress Dialog',
                onPressed: () => Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => FutureBuilder(
                      future: Future.delayed(const Duration(seconds: 2)),
                      builder: (context, AsyncSnapshot snapshot) {
                        if (snapshot.connectionState != ConnectionState.done) {
                          return ESMessage.showProgressIndicator(
                              title: 'New Screen');
                        }
                        return Scaffold(
                          appBar: AppBar(title: const Text('New Screen')),
                          body: const Text('New Screen'),
                        );
                      },
                    ),
                  ),
                ),
              ),
              ESButton(
                'Show Input Dialog',
                onPressed: () => ESMessage.showInputDialog(
                  context: context,
                  title: 'Title',
                  defaultValue: 'Default Value',
                  hitText: 'Enter Value',
                  onSubmit: (TextEditingController controller) =>
                      ESMessage.showInfoMessage(
                    context,
                    message: controller.text,
                  ),
                ),
              ),
              ESButton(
                'Show Confirm Dialog',
                onPressed: () => ESMessage.showConfirmDialog(
                  context: context,
                  title: 'Confirm Dialog',
                  message: 'Are you sure?',
                  buttonLeft: 'OK',
                  buttonRight: 'Cancel',
                ),
              ),
              ESButton(
                'List Dialog',
                onPressed: () => ESMessage.showListDialog(
                  context: context,
                  suffixText: 'Rows',
                  selectedValue: 20,
                  title: 'Row Count',
                  multiSelectValues: varList,
                  options: const [10, 20, 30, 50, 100],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


es_utils 是一个为 Flutter 开发者提供的实用工具集插件,它包含了许多常用的功能,可以帮助开发者更高效地编写代码。以下是一些 es_utils 插件的主要功能及其使用方法:

1. 安装 es_utils 插件

首先,你需要在 pubspec.yaml 文件中添加 es_utils 插件的依赖:

dependencies:
  es_utils: ^latest_version

然后运行 flutter pub get 来安装插件。

2. 常用功能及使用示例

2.1 字符串工具 (StringUtils)

StringUtils 提供了一些常用的字符串操作方法。

import 'package:es_utils/es_utils.dart';

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

  // 检查字符串是否为空
  bool isEmpty = StringUtils.isEmpty(str);
  print(isEmpty); // false

  // 检查字符串是否为非空
  bool isNotEmpty = StringUtils.isNotEmpty(str);
  print(isNotEmpty); // true

  // 字符串反转
  String reversedStr = StringUtils.reverse(str);
  print(reversedStr); // "!dlroW ,olleH"

  // 字符串截取
  String substring = StringUtils.substring(str, start: 7, end: 12);
  print(substring); // "World"
}

2.2 日期工具 (DateUtils)

DateUtils 提供了一些常用的日期操作方法。

import 'package:es_utils/es_utils.dart';

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

  // 格式化日期
  String formattedDate = DateUtils.formatDate(now, pattern: 'yyyy-MM-dd');
  print(formattedDate); // "2023-10-05"

  // 获取当前时间戳
  int timestamp = DateUtils.currentTimeMillis();
  print(timestamp); // 1696521600000

  // 日期加减
  DateTime tomorrow = DateUtils.addDays(now, 1);
  print(tomorrow); // 2023-10-06 12:00:00.000
}

2.3 文件工具 (FileUtils)

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

import 'package:es_utils/es_utils.dart';

void main() async {
  // 检查文件是否存在
  bool fileExists = await FileUtils.exists('/path/to/file.txt');
  print(fileExists); // true or false

  // 读取文件内容
  String content = await FileUtils.readFile('/path/to/file.txt');
  print(content);

  // 写入文件内容
  await FileUtils.writeFile('/path/to/file.txt', 'Hello, World!');

  // 删除文件
  await FileUtils.deleteFile('/path/to/file.txt');
}

2.4 网络工具 (NetworkUtils)

NetworkUtils 提供了一些常用的网络操作方法。

import 'package:es_utils/es_utils.dart';

void main() async {
  // 检查网络连接
  bool isConnected = await NetworkUtils.isConnected();
  print(isConnected); // true or false

  // 获取当前网络类型
  String networkType = await NetworkUtils.getNetworkType();
  print(networkType); // "WiFi" or "Mobile"

  // 发送HTTP GET请求
  var response = await NetworkUtils.get('https://jsonplaceholder.typicode.com/posts/1');
  print(response.body);

  // 发送HTTP POST请求
  var postResponse = await NetworkUtils.post('https://jsonplaceholder.typicode.com/posts', body: {
    'title': 'foo',
    'body': 'bar',
    'userId': 1,
  });
  print(postResponse.body);
}

2.5 设备信息工具 (DeviceUtils)

DeviceUtils 提供了一些获取设备信息的方法。

import 'package:es_utils/es_utils.dart';

void main() {
  // 获取设备ID
  String deviceId = DeviceUtils.getDeviceId();
  print(deviceId);

  // 获取设备型号
  String deviceModel = DeviceUtils.getDeviceModel();
  print(deviceModel);

  // 获取系统版本
  String osVersion = DeviceUtils.getOsVersion();
  print(osVersion);
}
回到顶部