Flutter工具集插件ns_utils的使用

Flutter工具集插件ns_utils的使用

ns_utils - Flutter Utility Library

  • 版本: 1.1.2
  • 许可证: MIT
  • 风格: flutter_lints

引用示例代码

import 'package:ns_utils/src.dart';

整理后的内容

ns_utils 是一个强大的 Flutter 工具库,简化并增强您的 Flutter 应用开发体验。 它提供了一系列方法和扩展来简化您的代码,使其更易读且高效。无论您需要响应式设计、日期和时间处理、地图操作、字符串操作还是自定义控件,ns_utils 都能满足需求。

获取开始

  1. 打开项目中的 pubspec.yaml 文件。
  2. 将 ns_utils 添加为依赖项,并 替换 [version] 为最新版本。
dependencies:
  flutter:
    sdk: flutter
  ns_utils: ^[version]
  1. 运行 flutter pub get 获取包。

导入包

import 'package:ns_utils/src.dart';

包含的内容 ns_utils 提供了多种功能来简化您的 Flutter 开发过程:

CustomErrorWidget

了解更多,请参阅文章 Flutter -: KILL THE RED SCREEN OF DEATH


##### Sizes

![](https://miro.medium.com/max/2160/1*zNcRtlhzm9407KJWtAFFw.png)
有关 Flutter 中响应式应用设计的更多信息,请参阅文章 <a href="https://medium.com/nonstopio/let-make-responsive-app-in-flutter-e48428795476" rel="ugc">Responsive App in Flutter</a>
BuildContext Extensions

简化代码的方法:

  • 替换冗长的 Navigator.of(context).push(...)context.push(...)
  • 更直观的方法如 context.replace(...), context.makeFirst(...), 和 context.pop(...)
  • 轻松访问设备尺寸:context.mq.sizeX.widthcontext.mq.sizeX.height
  • 简化焦点管理:context.setFocus(focusNode: focusNode)
DateTime Extensions

增强日期和时间处理的扩展:

  • dayDifference: 获取当前日期与给定日期之间的天数差
  • toServerFormat(): 获取 ISO-8601 格式的日期字符串
  • isToday, isYesterday, isTomorrow: 检查日期是否为今天、昨天或明天
  • tomorrow()yesterday(): 获取第二天或前一天
Map Extensions

简化 Map 对象的操作:

  • 使用方法如 getBool('key'), getInt('key'), getDouble('key'), getString('key'), getList('key'), 和 getMap('key') 来检索值(带可选默认值)
  • 使用 toJson() 将 Map 转换为 JSON 字符串
  • 使用 toPretty() 美化 Map 输出
List Extensions

轻松操纵列表:

  • 使用 toJson() 将列表转换为 JSON 字符串
String Extensions

简化字符串操作:

  • toMap(): 解析 JSON 字符串为 Map
  • toList(): 解析 JSON 字符串为 List
  • isEmptyOrNull: 检查字符串是否为空或 null
  • isNotBlank: 检查字符串是否不为空、不为 null 并且不是纯空格
  • toINTtoDOUBLE: 将字符串解析为 int 或 double
  • asBool: 将字符串转换为布尔值
Double/Int Extensions

增强数值操作的扩展:

  • asBool: 将整数转换为布尔值
  • 分数运算:tenth, fourth, third, half, doubled, tripled
Widget Extensions & Spacers

轻松自定义控件:

  • 在无需复杂嵌套的情况下添加提示和手势到控件上
  • 使用 ContainerSizedBox 等控件时简化语法
  • 期待未来更新中更多的控件增强

贡献

您可以以多种方式贡献给 ns_utils:

  • 提出新功能或改进
  • 报告错误
  • 修复错误
  • 参与讨论并帮助决策
  • 改进文档
  • 发送包含贡献的 pull requests

感谢所有贡献者!


完整示例 demo

import 'package:flutter/material.dart';
import 'package:ns_utils/src.dart';
import 'package:ns_utils/widgets/custom_error_widget.dart';

void main() {
  //&lt;editor-fold desc="Override ErrorWidget"&gt;
  ErrorWidget.builder = (FlutterErrorDetails errorDetails) {
    return CustomErrorWidget(
      errorDetails: errorDetails,
      logoAsset: '&lt;Your app logo asset&gt;',
      showErrorDetails: true, // this should be set to false in production
      onRestart: (BuildContext context) {
        context.makeFirst(const HomeScreen());
      },
    );
  };
  //&lt;/editor-fold&gt;

  runApp(const MyApp());
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  [@override](/user/override)
  State&lt;HomeScreen&gt; createState() =&gt; _HomeScreenState();
}

class _HomeScreenState extends State&lt;HomeScreen&gt; {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Placeholder(),
    );
  }
}

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

1 回复

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


当然,ns_utils 是一个 Flutter 插件工具集,它提供了一系列实用的功能来简化开发工作。假设你已经通过 pubspec.yaml 文件添加了 ns_utils 依赖,并运行了 flutter pub get 命令来安装它。以下是如何在 Flutter 项目中使用 ns_utils 的一些代码示例。

首先,确保你的 pubspec.yaml 文件中包含如下依赖:

dependencies:
  flutter:
    sdk: flutter
  ns_utils: ^最新版本号

然后,在 Dart 文件中导入 ns_utils 包:

import 'package:ns_utils/ns_utils.dart';

示例代码

1. 使用 NSStringUtils 进行字符串操作

void main() {
  final NSStringUtils stringUtils = NSStringUtils();

  // 转换为首字母大写
  String capitalized = stringUtils.capitalize("hello world");
  print(capitalized); // 输出: Hello World

  // 反转字符串
  String reversed = stringUtils.reverse("hello");
  print(reversed); // 输出: olleh
}

2. 使用 NSDateUtils 进行日期操作

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final NSDateUtils dateUtils = NSDateUtils();

    // 获取当前日期和时间
    DateTime now = DateTime.now();
    print("Current Date and Time: ${dateUtils.format(now, 'yyyy-MM-dd HH:mm:ss')}");

    // 格式化日期
    DateTime specificDate = DateTime(2023, 10, 5, 14, 30, 0);
    String formattedDate = dateUtils.format(specificDate, 'EEEE, MMMM dd, yyyy');
    print("Formatted Date: $formattedDate"); // 输出: Thursday, October 05, 2023

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Date Utils Demo'),
        ),
        body: Center(
          child: Text('Check the console for date outputs.'),
        ),
      ),
    );
  }
}

3. 使用 NSNumberUtils 进行数字操作

void main() {
  final NSNumberUtils numberUtils = NSNumberUtils();

  // 数字格式化
  double number = 1234567.89;
  String formattedNumber = numberUtils.formatNumber(number, locale: 'en_US');
  print(formattedNumber); // 输出: 1,234,567.89

  // 转换为货币格式
  double amount = 999999.99;
  String formattedCurrency = numberUtils.formatCurrency(amount, locale: 'en_US');
  print(formattedCurrency); // 输出: $999,999.99
}

4. 使用 NSDeviceUtils 获取设备信息

void main() {
  final NSDeviceUtils deviceUtils = NSDeviceUtils();

  // 获取设备型号
  String deviceModel = deviceUtils.deviceModel;
  print("Device Model: $deviceModel");

  // 获取设备平台
  String platform = deviceUtils.platform;
  print("Platform: $platform");

  // 获取设备系统版本
  String osVersion = deviceUtils.osVersion;
  print("OS Version: $osVersion");

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Device Utils Demo'),
        ),
        body: Center(
          child: Text('Check the console for device information.'),
        ),
      ),
    );
  }
}

请注意,这些示例代码假设 ns_utils 插件提供了上述功能类和方法。实际使用时,请查阅 ns_utils 的官方文档以获取最新的 API 参考和可用功能。

另外,由于插件的功能和 API 可能会随着版本更新而变化,建议在实际项目中始终参考插件的最新版本文档。

回到顶部