Flutter自定义弹窗插件flutter_ritl_alert的使用

Flutter自定义弹窗插件flutter_ritl_alert的使用

引用

pubspec.yaml文件中添加依赖:

dependencies:
  flutter_ritl_alert: ^0.0.1

或者从GitHub直接引用:

dependencies:
  flutter_ritl_alert:
    git: git@github.com:RITL/flutter_ritl_alert.git

然后运行 flutter packages get

无按钮

不带按钮的弹窗示例:

RITLAlert(
  context: context,
  title: "我是RITLAlert",
  message: "开启一下GPS定位吧。",
).show();

默认方式

带有两个按钮(一个普通按钮和一个破坏性按钮)的弹窗示例:

RITLAlert(
  context: context,
  title: "我是RITLAlert",
  message: "开启一下GPS定位吧。",
  buttons: [
    RITLAlertAction(
      title: "知道啦",
      type: RITLAlertActionType.normal,
      onPressed: (alert, action) => print("我点击啦知道啦"),
    ),
    RITLAlertAction(
      title: "取消吧",
      type: RITLAlertActionType.destructive,
      onPressed: (alert, action) => print("我点击啦取消吧"),
    ),
  ],
).show();

自定义方式

带有自定义样式的按钮的弹窗示例:

RITLAlert(
    context: context,
    title: "我是RITLAlert",
    message: "开启一下GPS定位吧。",
    buttons: [
      RITLAlertAction(
        title: "自定义啦",
        type: RITLAlertActionType.custom,
        style: TextStyle(
          color: Color.fromRGBO(47, 189, 75, 1), // 自定义颜色
          fontWeight: FontWeight.w500, // 自定义字体粗细
        ),
        onPressed: (alert, action) => print("我点击自定义"),
      ),
      RITLAlertAction(
        title: "知道啦",
        type: RITLAlertActionType.normal,
        onPressed: (alert, action) => print("我点击啦知道啦"),
      ),
      RITLAlertAction(
        title: "取消吧",
        type: RITLAlertActionType.destructive,
        onPressed: (alert, action) => print("我点击啦取消吧"),
      ),
    ]).show();

自定义Style

自定义弹窗背景颜色、分隔线颜色及文本样式:

RITLAlert(
  context: context,
  title: "我是RITLAlert",
  message: "开启一下GPS定位吧。",
  buttons: [
    RITLAlertAction(
      title: "知道啦",
      type: RITLAlertActionType.normal,
      onPressed: (alert, action) => print("我点击啦知道啦"),
    ),
    RITLAlertAction(
      title: "取消吧",
      type: RITLAlertActionType.destructive,
      onPressed: (alert, action) => print("我点击啦取消吧"),
    ),
  ],
  style: RITLAlertStyle(
    backgroundColor: CupertinoColors.black,
    separateColor: CupertinoColors.white,
    titleStyle: TextStyle(color: CupertinoColors.white),
    messageStyle: TextStyle(color: CupertinoColors.white),
  ),
).show();

完整示例Demo

以下是一个完整的示例代码,展示了如何在Flutter应用中使用flutter_ritl_alert插件来创建不同样式的弹窗:

import 'package:flutter/cupertino.dart';
import 'package:flutter_ritl_alert/flutter_ritl_alert.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return CupertinoApp(
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            CupertinoButton(
              child: Text("RITLAlert(无buttons)"),
              onPressed: () {
                _alertWithNoButtons();
              },
            ),
            CupertinoButton(
              child: Text("RITLAlert(default)"),
              onPressed: () {
                _alertWithDefaultButtons();
              },
            ),
            CupertinoButton(
              child: Text("RITLAlert(Custom)"),
              onPressed: () {
                _alertWithCustom();
              },
            ),
            CupertinoButton(
              child: Text("RITLAlert(UI)"),
              onPressed: () {
                _alertWithUI();
              },
            ),
          ],
        ),
      ),
    );
  }

  // 无按钮的弹出
  _alertWithNoButtons() {
    RITLAlert(
      context: context,
      title: "我是RITLAlert",
      message: "开启一下GPS定位吧。",
    ).show();
  }

  // 默认的按钮弹出
  _alertWithDefaultButtons() {
    RITLAlert(
      context: context,
      title: "我是RITLAlert",
      message: "开启一下GPS定位吧。",
      buttons: [
        RITLAlertAction(
          title: "知道啦",
          type: RITLAlertActionType.normal,
          onPressed: (alert, action) => print("我点击啦知道啦"),
        ),
        RITLAlertAction(
          title: "取消吧",
          type: RITLAlertActionType.destructive,
          onPressed: (alert, action) => print("我点击啦取消吧"),
        ),
      ],
    ).show();
  }

  _alertWithCustom() {
    RITLAlert(
        context: context,
        title: "我是RITLAlert",
        message: "开启一下GPS定位吧。",
        buttons: [
          RITLAlertAction(
            title: "自定义啦",
            type: RITLAlertActionType.custom,
            style: TextStyle(
              color: Color.fromRGBO(47, 189, 75, 1),
              fontWeight: FontWeight.w500,
            ),
            onPressed: (alert, action) => print("我点击自定义"),
          ),
          RITLAlertAction(
            title: "知道啦",
            type: RITLAlertActionType.normal,
            onPressed: (alert, action) => print("我点击啦知道啦"),
          ),
          RITLAlertAction(
            title: "取消吧",
            type: RITLAlertActionType.destructive,
            onPressed: (alert, action) => print("我点击啦取消吧"),
          ),
        ]).show();
  }

  // 设置UI
  _alertWithUI() {
    RITLAlert(
      context: context,
      title: "我是RITLAlert",
      message: "开启一下GPS定位吧。",
      buttons: [
        RITLAlertAction(
          title: "知道啦",
          type: RITLAlertActionType.normal,
          onPressed: (alert, action) => print("我点击啦知道啦"),
        ),
        RITLAlertAction(
          title: "取消吧",
          type: RITLAlertActionType.destructive,
          onPressed: (alert, action) => print("我点击啦取消吧"),
        ),
      ],
      style: RITLAlertStyle(
        backgroundColor: CupertinoColors.black,
        separateColor: CupertinoColors.white,
        titleStyle: TextStyle(color: CupertinoColors.white),
        messageStyle: TextStyle(color: CupertinoColors.white),
      ),
    ).show();
  }
}

更多关于Flutter自定义弹窗插件flutter_ritl_alert的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义弹窗插件flutter_ritl_alert的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flutter_ritl_alert 是一个自定义的 Flutter 弹窗插件,它允许你创建各种风格的弹窗。以下是如何使用 flutter_ritl_alert 插件的详细步骤。

1. 添加依赖

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

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

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

2. 导入包

在你的 Dart 文件中导入 flutter_ritl_alert 包:

import 'package:flutter_ritl_alert/flutter_ritl_alert.dart';

3. 使用 RitlAlert 弹窗

flutter_ritl_alert 提供了多种方法来显示不同类型的弹窗。以下是几种常见的用法:

3.1 显示一个基本的弹窗

RitlAlert.show(
  context,
  title: "提示",
  message: "这是一个基本弹窗",
  confirmText: "确定",
  onConfirm: () {
    print("点击了确定按钮");
  },
);

3.2 显示带有取消按钮的弹窗

RitlAlert.show(
  context,
  title: "提示",
  message: "这是一个带有取消按钮的弹窗",
  confirmText: "确定",
  cancelText: "取消",
  onConfirm: () {
    print("点击了确定按钮");
  },
  onCancel: () {
    print("点击了取消按钮");
  },
);

3.3 显示自定义样式的弹窗

你可以通过 style 参数来自定义弹窗的样式:

RitlAlert.show(
  context,
  title: "提示",
  message: "这是一个自定义样式的弹窗",
  confirmText: "确定",
  style: RitlAlertStyle(
    backgroundColor: Colors.blue,
    titleStyle: TextStyle(color: Colors.white, fontSize: 20),
    messageStyle: TextStyle(color: Colors.white, fontSize: 16),
    confirmButtonColor: Colors.green,
    cancelButtonColor: Colors.red,
  ),
  onConfirm: () {
    print("点击了确定按钮");
  },
);

3.4 显示带有图标的弹窗

你可以在弹窗中添加图标:

RitlAlert.show(
  context,
  title: "提示",
  message: "这是一个带有图标的弹窗",
  icon: Icon(Icons.info, color: Colors.blue),
  confirmText: "确定",
  onConfirm: () {
    print("点击了确定按钮");
  },
);

4. 自定义弹窗内容

如果你需要更复杂的弹窗内容,可以使用 RitlAlert.custom 方法:

RitlAlert.custom(
  context,
  content: Column(
    children: [
      Text("自定义内容"),
      ElevatedButton(
        onPressed: () {
          Navigator.pop(context);
        },
        child: Text("关闭"),
      ),
    ],
  ),
);

5. 关闭弹窗

你可以通过 Navigator.pop(context); 来手动关闭弹窗。

6. 其他配置

flutter_ritl_alert 还支持其他一些配置,例如设置弹窗的宽度、高度、动画效果等。你可以根据需要在 RitlAlert.showRitlAlert.custom 方法中进行配置。

7. 示例代码

以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Flutter RitlAlert Example')),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              RitlAlert.show(
                context,
                title: "提示",
                message: "这是一个基本弹窗",
                confirmText: "确定",
                onConfirm: () {
                  print("点击了确定按钮");
                },
              );
            },
            child: Text("显示弹窗"),
          ),
        ),
      ),
    );
  }
}
回到顶部