Flutter快捷操作插件easy_actions的使用

Flutter快捷操作插件easy_actions的使用

简介

easy_actions 是一个用于在 Flutter 应用程序中创建简单易用的操作按钮(如按钮、复选框和开关)的包。

特性

  • Elevated Button
  • Outlined Button
  • Icon Button

开始使用

首先,在你的 Flutter 项目中添加 easy_actions 包作为依赖项。

dependencies:
  easy_actions: '^1.0.0'

使用示例

导入库

import 'package:easy_actions/easy_actions.dart';

Elevated Button

EasyElevatedButton(
  label: 'Hello World!',
  isRounded: true,
  onPressed: () {},
),

Outlined Button

EasyOutlinedButton(
  label: 'Hello World!',
  isRounded: true,
  onPressed: () {},
),

Icon Button

EasyIconButton(
  icon: Icons.add,
  onPressed: () {},
),

完整示例

以下是一个完整的示例代码,展示了如何在应用程序中使用这些按钮。

import 'package:easy_actions/easy_actions.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(
      debugShowCheckedModeBanner: false,
      title: 'Easy Actions Demo',
      theme: ThemeData(
        primarySwatch: Colors.purple,
      ),
      home: const HomePage(),
    );
  }
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Easy Actions',
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.fromLTRB(20, 12, 20, 0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const Text(
              'Elevated Buttons',
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(
              height: 8,
            ),
            Wrap(
              spacing: 12,
              runSpacing: 12,
              children: [
                EasyElevatedButton(
                  label: 'Simple',
                  onPressed: () {},
                ),
                EasyElevatedButton(
                  label: 'Rounded',
                  isRounded: true,
                  onPressed: () {},
                ),
                EasyElevatedButton(
                  label: 'Elevated',
                  isRounded: true,
                  elevation: 12,
                  onPressed: () {},
                ),
                EasyElevatedButton(
                  label: 'Leading Icon',
                  isRounded: true,
                  icon: const Icon(
                    Icons.add,
                    size: 20,
                  ),
                  onPressed: () {},
                ),
                EasyElevatedButton(
                  label: 'Trailing Icon',
                  isRounded: true,
                  icon: const Icon(
                    Icons.add,
                    size: 20,
                  ),
                  isTrailingIcon: true,
                  onPressed: () {},
                ),
                EasyElevatedButton(
                  label: 'Custom Color',
                  isRounded: true,
                  icon: const Icon(
                    Icons.add,
                    size: 20,
                  ),
                  isTrailingIcon: true,
                  color: Colors.red,
                  onPressed: () {},
                ),
              ],
            ),
            const SizedBox(
              height: 24,
            ),
            const Text(
              'Outlined Buttons',
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(
              height: 8,
            ),
            Wrap(
              spacing: 12,
              runSpacing: 12,
              children: [
                EasyOutlinedButton(
                  label: 'Simple',
                  onPressed: () {},
                ),
                EasyOutlinedButton(
                  label: 'Rounded',
                  isRounded: true,
                  onPressed: () {},
                ),
                EasyOutlinedButton(
                  label: 'Leading Icon',
                  isRounded: true,
                  icon: const Icon(
                    Icons.add,
                    size: 20,
                  ),
                  onPressed: () {},
                ),
                EasyOutlinedButton(
                  label: 'Trailing Icon',
                  isRounded: true,
                  icon: const Icon(
                    Icons.add,
                    size: 20,
                  ),
                  isTrailingIcon: true,
                  onPressed: () {},
                ),
                EasyOutlinedButton(
                  label: 'Custom Color',
                  isRounded: true,
                  icon: const Icon(
                    Icons.add,
                    size: 20,
                    color: Colors.red,
                  ),
                  isTrailingIcon: true,
                  color: Colors.red,
                  labelColor: Colors.red,
                  onPressed: () {},
                ),
              ],
            ),
            const SizedBox(
              height: 24,
            ),
            const Text(
              'Icon Buttons',
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(
              height: 8,
            ),
            Wrap(
              spacing: 12,
              runSpacing: 12,
              children: [
                EasyIconButton(
                  icon: Icons.add,
                  onPressed: () {},
                ),
                EasyIconButton(
                  icon: Icons.add,
                  isRounded: true,
                  onPressed: () {},
                ),
                EasyIconButton(
                  icon: Icons.add,
                  elevation: 0,
                  borderRadius: 2,
                  onPressed: () {},
                ),
                EasyIconButton(
                  icon: Icons.add,
                  isRounded: true,
                  elevation: 12,
                  onPressed: () {},
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter快捷操作插件easy_actions的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter快捷操作插件easy_actions的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


easy_actions 是一个 Flutter 插件,旨在简化常见的操作,如显示对话框、导航、处理权限等。它提供了一组快捷方法,帮助开发者更高效地编写代码。以下是如何使用 easy_actions 插件的详细步骤和示例。

1. 添加依赖

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

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

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

2. 导入包

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

import 'package:easy_actions/easy_actions.dart';

3. 使用 EasyActions 提供的快捷操作

easy_actions 提供了多种快捷操作,以下是常见的几种使用方式:

3.1 显示对话框

使用 EasyActions.showDialog 来快速显示一个对话框:

ElevatedButton(
  onPressed: () {
    EasyActions.showDialog(
      context,
      title: '提示',
      message: '这是一个示例对话框',
      positiveText: '确定',
      onPositivePressed: () {
        print('确定按钮被点击');
      },
    );
  },
  child: Text('显示对话框'),
);

3.2 导航到新页面

使用 EasyActions.navigateTo 来快速导航到新页面:

ElevatedButton(
  onPressed: () {
    EasyActions.navigateTo(context, MyNewPage());
  },
  child: Text('导航到新页面'),
);

3.3 请求权限

使用 EasyActions.requestPermission 来请求权限:

ElevatedButton(
  onPressed: () async {
    bool permissionGranted = await EasyActions.requestPermission(
      context,
      permission: Permission.camera,
    );
    if (permissionGranted) {
      print('相机权限已授予');
    } else {
      print('相机权限被拒绝');
    }
  },
  child: Text('请求相机权限'),
);

3.4 显示 SnackBar

使用 EasyActions.showSnackBar 来快速显示一个 SnackBar:

ElevatedButton(
  onPressed: () {
    EasyActions.showSnackBar(
      context,
      message: '这是一个 SnackBar 示例',
    );
  },
  child: Text('显示 SnackBar'),
);

3.5 显示底部弹窗

使用 EasyActions.showBottomSheet 来显示一个底部弹窗:

ElevatedButton(
  onPressed: () {
    EasyActions.showBottomSheet(
      context,
      builder: (context) {
        return Container(
          height: 200,
          child: Center(
            child: Text('这是底部弹窗内容'),
          ),
        );
      },
    );
  },
  child: Text('显示底部弹窗'),
);

4. 自定义配置

easy_actions 允许你通过全局配置来定制默认行为。例如,你可以设置默认的对话框样式、SnackBar 样式等。

void main() {
  EasyActionsConfig(
    dialogConfig: DialogConfig(
      positiveText: 'OK',
      negativeText: 'Cancel',
    ),
    snackBarConfig: SnackBarConfig(
      duration: Duration(seconds: 3),
    ),
  );

  runApp(MyApp());
}

5. 处理错误

easy_actions 还提供了错误处理的快捷方法,例如显示错误对话框或错误 SnackBar:

ElevatedButton(
  onPressed: () {
    try {
      // 模拟一个错误
      throw Exception('发生了一个错误');
    } catch (e) {
      EasyActions.showErrorDialog(context, e.toString());
    }
  },
  child: Text('显示错误对话框'),
);
回到顶部