美文网首页
Flutter Getx 简单记录(纯属粘贴)

Flutter Getx 简单记录(纯属粘贴)

作者: 只会ctrl_c_v | 来源:发表于2021-08-10 14:59 被阅读0次

    中文介绍

    三大功能

    一、响应式状态管理器

    这就是你的计数变量。
    var name = 'Jonatas Borges';
    要想让它变得可观察,你只需要在它的末尾加上".obs"
    var name = 'Jonatas Borges'.obs;
    而在UI中,当你想显示该值并在值变化时更新页面,只需这样做。
    Obx(() => Text("${controller.name}"));

    二、路由管理
    跳转路由

    Get.to(NextScreen());
    Get.toNamed('/details');
    进入下一个页面,但没有返回上一个页面的选项(用于闪屏页,登录页面等)。
    Get.off(NextScreen());
    进入下一个页面并取消之前的所有路由(在购物车、投票和测试中很有用)。
    Get.offAll(NextScreen());

    返回

    Get.back();

    void gotoPage<T>(T page, {dynamic arguments}) async {
       // 通过这样,介绍返回值 Get.back(result: value);
       // 走非Get.back(result: value)返回方式,会返回null
       var value = await Get.to(() => page, arguments: arguments);
       print("A页面的返回值 $value");
     }
    
    三、依赖管理

    // 使用Get.put()实例化你的类,使其对当下的所有子路由可用。
    final Controller c = Get.put(Controller());

    // 使用Obx(()=>每当改变计数时,就更新Text()。
    appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))),

    // 你可以让Get找到一个正在被其他页面使用的Controller,并将它返回给你。
    final Controller c = Get.find();

    高级API

    // 给出当前页面的args。
    Get.arguments
    
    //给出以前的路由名称
    Get.previousRoute
    
    // 给出要访问的原始路由,例如,rawRoute.isFirst()
    Get.rawRoute
    
    // 允许从GetObserver访问Rounting API。
    Get.routing
    
    // 检查 snackbar 是否打开
    Get.isSnackbarOpen
    
    // 检查 dialog 是否打开
    Get.isDialogOpen
    
    // 检查 bottomsheet 是否打开
    Get.isBottomSheetOpen
    
    // 删除一个路由。
    Get.removeRoute()
    
    //反复返回,直到表达式返回真。
    Get.until()
    
    // 转到下一条路由,并删除所有之前的路由,直到表达式返回true。
    Get.offUntil()
    
    // 转到下一个命名的路由,并删除所有之前的路由,直到表达式返回true。
    Get.offNamedUntil()
    
    //检查应用程序在哪个平台上运行。
    GetPlatform.isAndroid
    GetPlatform.isIOS
    GetPlatform.isMacOS
    GetPlatform.isWindows
    GetPlatform.isLinux
    GetPlatform.isFuchsia
    
    //检查设备类型
    GetPlatform.isMobile
    GetPlatform.isDesktop
    //所有平台都是独立支持web的!
    //你可以知道你是否在浏览器内运行。
    //在Windows、iOS、OSX、Android等系统上。
    GetPlatform.isWeb
    
    
    // 相当于.MediaQuery.of(context).size.height,
    //但不可改变。
    Get.height
    Get.width
    
    // 提供当前上下文。
    Get.context
    
    // 在你的代码中的任何地方,在前台提供 snackbar/dialog/bottomsheet 的上下文。
    Get.contextOverlay
    
    // 注意:以下方法是对上下文的扩展。
    // 因为在你的UI的任何地方都可以访问上下文,你可以在UI代码的任何地方使用它。
    
    // 如果你需要一个可改变的高度/宽度(如桌面或浏览器窗口可以缩放),你将需要使用上下文。
    context.width
    context.height
    
    // 让您可以定义一半的页面、三分之一的页面等。
    // 对响应式应用很有用。
    // 参数: dividedBy (double) 可选 - 默认值:1
    // 参数: reducedBy (double) 可选 - 默认值:0。
    context.heightTransformer()
    context.widthTransformer()
    
    /// 类似于 MediaQuery.of(context).size。
    context.mediaQuerySize()
    
    /// 类似于 MediaQuery.of(context).padding。
    context.mediaQueryPadding()
    
    /// 类似于 MediaQuery.of(context).viewPadding。
    context.mediaQueryViewPadding()
    
    /// 类似于 MediaQuery.of(context).viewInsets。
    context.mediaQueryViewInsets()
    
    /// 类似于 MediaQuery.of(context).orientation;
    context.orientation()
    
    ///检查设备是否处于横向模式
    context.isLandscape()
    
    ///检查设备是否处于纵向模式。
    context.isPortrait()
    
    ///类似于MediaQuery.of(context).devicePixelRatio。
    context.devicePixelRatio()
    
    ///类似于MediaQuery.of(context).textScaleFactor。
    context.textScaleFactor()
    
    ///查询设备最短边。
    context.mediaQueryShortestSide()
    
    ///如果宽度大于800,则为真。
    context.showNavbar()
    
    ///如果最短边小于600p,则为真。
    context.isPhone()
    
    ///如果最短边大于600p,则为真。
    context.isSmallTablet()
    
    ///如果最短边大于720p,则为真。
    context.isLargeTablet()
    
    ///如果当前设备是平板电脑,则为真
    context.isTablet()
    
    ///根据页面大小返回一个值<T>。
    ///可以给值为:
    ///watch:如果最短边小于300
    ///mobile:如果最短边小于600
    ///tablet:如果最短边(shortestSide)小于1200
    ///desktop:如果宽度大于1200
    context.responsiveValue<T>()
    

    相关文章

      网友评论

          本文标题:Flutter Getx 简单记录(纯属粘贴)

          本文链接:https://www.haomeiwen.com/subject/bynxbltx.html