Flutter Getx Dialog 的使用

发布于 1 年前 作者 phonegap100 888 次浏览 最后一次编辑是 1 年前 来自 分享

Flutter Getx 对应视频教程访问https://www.itying.com/goods-1176.html

一 、 Getx安装

将 Get 添加到你的 pubspec.yaml 文件中。

dependencies:
  get: ^4.6.5

在需要用到的文件中导入,它将被使用。

import 'package:get/get.dart';

二、 Getx 使用 Dialog

一、设置应用程序入口

当我们导入依赖后,在应用程序顶层把GetMaterialApp 作为顶层,如下所示

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: "GetX",
      home: Scaffold(
        appBar: AppBar(title: Text("GetX Title"),),
      ),
    );
  }
}

二、调用BottomSheet 以及改变主题

我们可以通过GetX很轻松的调用bottomSheet(),而且无需传入context,下面我给出一个例子,使用GetX弹出bottomSheet并很轻松的实现切换主题 。

我们可以通过Get.bottomSheet() 来显示 BottomSheet ,通过Get.back()实现路由返回,通过Get.changeTheme(ThemeData.dark())切换皮肤主题,通过Get.isDarkMode判断主题样式。

ElevatedButton(
                onPressed: () {
                  Get.bottomSheet(Container(
                    color: Get.isDarkMode ? Colors.black12 : Colors.white,
                    height: 200,
                    child: Column(
                      children: [
                        ListTile(
                          leading: Icon(Icons.wb_sunny_outlined,
                              color:
                                  Get.isDarkMode ? Colors.white : Colors.black),
                          title: Text("白天模式",
                              style: TextStyle(
                                  color: Get.isDarkMode
                                      ? Colors.white
                                      : Colors.black)),
                          onTap: () {
                            Get.changeTheme(ThemeData.light());
                            Get.back();
                          },
                        ),
                        ListTile(
                          leading: Icon(Icons.wb_sunny,
                              color:
                                  Get.isDarkMode ? Colors.white : Colors.black),
                          title: Text("黑夜模式",
                              style: TextStyle(
                                  color: Get.isDarkMode
                                      ? Colors.white
                                      : Colors.black)),
                          onTap: () {
                            Get.changeTheme(ThemeData.dark());
                            Get.back();
                          },
                        )
                      ],
                    ),
                  ));
                },
                child: const Text("Show BottomSheet"))

BottomSheet属性和说明

字段 属性 描述
bottomsheet Widget 弹出的Widget组件
backgroundColor Color bottomsheet的背景颜色
elevation double bottomsheet的阴影
persistent bool 是否添加到路由中
shape ShapeBorder 边框形状,一般用于圆角效果
clipBehavior Clip 裁剪的方式
barrierColor Color 弹出层的背景颜色
ignoreSafeArea bool 是否忽略安全适配
isScrollControlled bool 是否支持全屏弹出,默认false
useRootNavigator bool 是否使用根导航
isDismissible bool 点击背景是否可关闭,默认ture
enableDrag bool 是否可以拖动关闭,默认true
settings RouteSettings 路由设置
enterBottomSheetDuration Duration bottomsheet进入时的动画时间
exitBottomSheetDuration Duration bottomsheet退出时的动画时间

三、调用snackbar

Snackbar和我们前面讲的toast有点相似, 如果想在应用程序中触发某些特定的事件后,需要弹出快捷消息,那么使用Snackbar则是最佳的选择。

我们可以通过Get.snackbar() 来显示 snackbar ,如下所示

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: "GetX",
      home: Scaffold(
        appBar: AppBar(
          title: Text("GetX Title"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () {
                  Get.snackbar("Snackbar 标题", "欢迎使用Snackbar");
                },
                child: Text("显示 Snackbar"))
            ],
          ),
        ),
      ),
    );
  }
}

如果您运行了代码,那么恭喜你,你已经会用GetX 来展示snackbar 了。你将得到下面的结果

Snackbar属性和说明

字段 属性 描述
title String 弹出的标题文字
message String 弹出的消息文字
colorText Color title和message的文字颜色
duration Duration Snackbar弹出的持续时间(默认3秒)
instantInit bool 当false可以把snackbar 放在initState,默认true
snackPosition SnackPosition 弹出时的位置,有两个选项【TOP,BOTTOM】默认TOP
titleText Widget 弹出标题的组件,设置该属性会导致title属性失效
messageText Widget 弹出消息的组件,设置该属性会导致messageText属性失效
icon Widget 弹出时图标,显示在title和message的左侧
shouldIconPulse bool 弹出时图标是否闪烁,默认false
maxWidth double Snackbar最大的宽度
margin EdgeInsets Snackbar外边距,默认zero
padding EdgeInsets Snackbar内边距,默认EdgeInsets.all(16)
borderRadius double 边框圆角大小,默认15
borderColor Color 边框的颜色,必须设置borderWidth,否则无效果
borderWidth double 边框的线条宽度
backgroundColor Color Snackbar背景颜色,默认Colors.grey.withOpacity(0.2)
leftBarIndicatorColor Color 左侧指示器的颜色
boxShadows List Snackbar阴影颜色
backgroundGradient Gradient 背景的线性颜色
mainButton TextButton 主要按钮,一般显示发送、确认按钮
onTap OnTap 点击Snackbar事件回调
isDismissible bool 是否开启Snackbar手势关闭,可配合dismissDirection使用
showProgressIndicator bool 是否显示进度条指示器,默认false
dismissDirection SnackDismissDirection Snackbar关闭的方向
progressIndicatorController AnimationController 进度条指示器的动画控制器
progressIndicatorBackgroundColor Color 进度条指示器的背景颜色
progressIndicatorValueColor Animation 进度条指示器的背景颜色,Animation
snackStyle SnackStyle Snackbar是否会附加到屏幕边缘
forwardAnimationCurve Curve Snackbar弹出的动画,默认Curves.easeOutCirc
reverseAnimationCurve Curve Snackbar消失的动画,默认Curves.easeOutCirc
animationDuration Duration Snackbar弹出和小时的动画时长,默认1秒
barBlur double Snackbar背景的模糊度
overlayBlur double 弹出时的毛玻璃效果值,默认0
snackbarStatus SnackbarStatusCallback Snackbar弹出或消失时的事件回调(即将打开、已打开、即将关闭、已关闭)
overlayColor Color 弹出时的毛玻璃的背景颜色
userInputForm Form 用户输入表单

四、调用defaultDialog

ElevatedButton(
                onPressed: () {
                  Get.defaultDialog(
                      title: "提示",
                      middleText: "您确定退出登录?",
                      confirm: ElevatedButton(
                          onPressed: () {
                            print("确定");
                            Get.back();
                          },
                          child: const Text("确定")),
                      cancel: ElevatedButton(
                          onPressed: () {
                            print("取消");
                            Get.back();
                          },
                          child: const Text("取消")));
                },
                child: const Text("显示默认的Dialog"))

Dialog属性和说明

字段 属性 描述
title String 弹出的标题,默认(Alert)
titlePadding EdgeInsetsGeometry 标题的内边距,默认(EdgeInsets.all(8))
titleStyle TextStyle 标题的样式
middleText String 中间内容区域显示的文字
middleTextStyle TextStyle 中间内容区域显示的文字样式
content Widget 弹出的内容,该值设置后middleText将无效
contentPadding EdgeInsetsGeometry 内容的内边距,默认(EdgeInsets.all(8))
onConfirm VoidCallback 确认按钮回调
onCancel VoidCallback 取消按钮回调
onCustom VoidCallback 自定义按钮回调
cancelTextColor Color 取消按钮文字的颜色
confirmTextColor Color 确认按钮文字的颜色
textConfirm String 确认按钮的文字
textCancel String 取消按钮的文字
textCustom String 自定义按钮的文字
confirm Widget 确认按钮的组件
cancel Widget 取消按钮的组件
custom Widget 自定义按钮的组件
backgroundColor Color 弹出框的背景颜色
barrierDismissible bool 是否可以通过点击背景关闭弹窗
buttonColor Color 按钮的文字颜色,根据按钮类型来设定不同的位置
radius double 弹出框的圆角大小,默认20
actions List 增加额外的子组件
onWillPop WillPopCallback 拦截关闭之前做一些操作
navigatorKey GlobalKey 用于打开对话框的key
回到顶部