Flutter UI组件工具插件ui_widgets_utils的使用

Flutter UI组件工具插件ui_widgets_utils的使用

UI Widgets Utilities

内容概览

SnappingListScrollPhysics

SnappingListScrollPhysics 是一个滚动物理类,用于在滚动列表时实现自动吸附效果。

VarStr

VarStr 是一个字符串处理类,可以用于处理和格式化字符串。

useWhiteForeground

useWhiteForeground 是一个函数,用于判断是否使用白色前景色。

showBox

showBox 是一个异步函数,用于显示一个简单的对话框。

dialogBox

dialogBox 是一个函数,用于显示一个自定义对话框。

MsgBox

MsgBox 是一个对话框类,用于显示带按钮的对话框。

DialogBox

DialogBox 是一个对话框类,用于显示带按钮的对话框。


示例代码

import 'src/imports.dart';

void main() => runApp(const MyApp());

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

  [@override](/user/override)
  Widget build(BuildContext context) => MaterialApp(
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true,
        ),
        home: const MyHomePage(),
      );
}

///
class MyHomePage extends StatefulWidget {
  ///
  const MyHomePage({super.key});

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends StateX<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('ui_widgets_utils Demo App'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              key: const Key('showBox'),
              onPressed: () async {
                final result = await showBox(
                  context: context,
                  text: 'This is a test.',
                  button01: const Option(key: Key('OK'), text: 'OK', result: true),
                  button02: const Option(
                      key: Key('Cancel'), text: 'Cancel', result: false),
                  useMaterial: true,
                );
              },
              child: const Text('showBox'),
            ),
            ElevatedButton(
              key: const Key('showDialogBox'),
              onPressed: () async {
                final result = await showDialogBox<bool>(
                  context,
                  content: const Text('This is a test.'),
                  actions: [
                    TextButton(
                      key: const Key('OK'),
                      style: TextButton.styleFrom(
                        textStyle: Theme.of(context).textTheme.labelLarge,
                      ),
                      child: const Text('OK'),
                      onPressed: () {
                        Navigator.pop(context, true);
                      },
                    ),
                    TextButton(
                      key: const Key('Cancel'),
                      style: TextButton.styleFrom(
                        textStyle: Theme.of(context).textTheme.labelLarge,
                      ),
                      child: const Text('Cancel'),
                      onPressed: () {
                        Navigator.pop(context, false);
                      },
                    ),
                  ],
                );
              },
              child: const Text('showDialogBox'),
            ),
            ElevatedButton(
              key: const Key('dialogBox'),
              onPressed: () {
                dialogBox(
                  context,
                  title: 'This is a test.',
                  button01: const Option(key: Key('OK'), text: 'OK', result: true),
                  button02: const Option(
                      key: Key('Cancel'), text: 'Cancel', result: false),
                );
              },
              child: const Text('dialogBox'),
            ),
            ElevatedButton(
              key: const Key('msgbox'),
              onPressed: () {
                MsgBox(
                  context: context,
                  title: 'This is a test.',
                  actions: [
                    TextButton(
                      key: const Key('OK'),
                      style: TextButton.styleFrom(
                        textStyle: Theme.of(context).textTheme.labelLarge,
                      ),
                      child: const Text('OK'),
                      onPressed: () {
                        Navigator.pop(context, true);
                      },
                    ),
                    TextButton(
                      key: const Key('Cancel'),
                      style: TextButton.styleFrom(
                        textStyle: Theme.of(context).textTheme.labelLarge,
                      ),
                      child: const Text('Cancel'),
                      onPressed: () {
                        Navigator.pop(context, false);
                      },
                    ),
                  ],
                ).show();
              },
              child: const Text('MsgBox'),
            ),
            ElevatedButton(
              key: const Key('classdialogbox'),
              onPressed: () {
                DialogBox(
                  context: context,
                  title: 'This is a test.',
                  button01: const Option(key: Key('OK'), text: 'OK', result: true),
                  button02: const Option(
                      key: Key('Cancel'), text: 'Cancel', result: false),
                ).show();
              },
              child: const Text('DialogBox'),
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


ui_widgets_utils 是一个 Flutter 插件,旨在提供一组常用的 UI 组件和工具,帮助开发者更高效地构建 Flutter 应用。以下是如何使用 ui_widgets_utils 的基本步骤和一些常见组件的示例。

1. 添加依赖

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

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

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

2. 导入包

在需要使用 ui_widgets_utils 的 Dart 文件中导入包:

import 'package:ui_widgets_utils/ui_widgets_utils.dart';

3. 使用组件和工具

ui_widgets_utils 提供了多种常用的 UI 组件和工具,以下是一些常见的用法示例:

3.1. CustomButton

CustomButton 是一个可定制的按钮组件,支持多种样式和点击事件。

CustomButton(
  text: 'Click Me',
  onPressed: () {
    print('Button Pressed!');
  },
  backgroundColor: Colors.blue,
  textColor: Colors.white,
  borderRadius: 8.0,
);

3.2. CustomTextField

CustomTextField 是一个可定制的文本输入框组件,支持占位符、边框样式、输入验证等功能。

CustomTextField(
  hintText: 'Enter your name',
  onChanged: (value) {
    print('Text changed: $value');
  },
  borderColor: Colors.grey,
  borderRadius: 4.0,
);

3.3. CustomDialog

CustomDialog 是一个可定制的对话框组件,支持标题、内容、按钮等。

CustomDialog(
  title: 'Alert',
  content: 'This is a custom dialog.',
  onConfirm: () {
    print('Confirmed!');
  },
  onCancel: () {
    print('Cancelled!');
  },
);

3.4. CustomProgressIndicator

CustomProgressIndicator 是一个可定制的进度指示器组件,支持颜色、大小等。

CustomProgressIndicator(
  color: Colors.blue,
  size: 40.0,
);

3.5. CustomSnackbar

CustomSnackbar 是一个可定制的 Snackbar 组件,支持消息、背景颜色、持续时间等。

CustomSnackbar.show(
  context: context,
  message: 'This is a custom snackbar!',
  backgroundColor: Colors.green,
  duration: Duration(seconds: 3),
);

4. 工具函数

ui_widgets_utils 还提供了一些工具函数,例如:

4.1. Utils.showToast

显示一个简单的 Toast 消息。

Utils.showToast(context, 'This is a toast message!');

4.2. Utils.formatDate

格式化日期。

String formattedDate = Utils.formatDate(DateTime.now(), 'yyyy-MM-dd');
print(formattedDate); // 输出: 2023-10-05

5. 自定义主题

ui_widgets_utils 允许你通过 CustomTheme 来自定义应用的主题。

CustomTheme(
  primaryColor: Colors.blue,
  accentColor: Colors.green,
  textTheme: TextTheme(
    bodyText1: TextStyle(fontSize: 16.0, color: Colors.black),
  ),
  child: MyApp(),
);
回到顶部