通用类/函数,从多个小部件/状态中调用

huangapple go评论77阅读模式
英文:

Common class/function called from multiple Widget/State

问题

我有一个函数,被一些小部件或状态类使用。

例如像这样,它检查存储在设备中的常见参数。

我想从多个页面或小部件类中使用这个函数。

对此有什么最佳实践吗?

Future<bool> _getCommonParam() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  if (prefs.getBool('param') == null) {
    return true;
  } else {
    return (prefs.getBool('param'));
  }
}
英文:

I have a function which is used from a few widget or State class.

For example like this, It checks the common parameters which are stored in device.

I want to use this function from multiple pages or widget class.

What is the best practice for this??

  Future&lt;bool&gt; _getCommonParam() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if (prefs.getBool(&#39;param&#39;) == null){
      return true;
    }
    else {
      return (prefs.getBool(&#39;param&#39;));
    }
  }

答案1

得分: 1

你可以将它声明在一个单独的类中,如下所示:

import 'package:shared_preferences/shared_preferences.dart';

class AppPrefs {
  static Future<bool> getCommonParam() async {
    var prefs = await SharedPreferences.getInstance();
    return prefs.getBool('param') ?? true;
  } 
}

然后,只要导入该类,你就可以从任何地方调用 AppPrefs.getCommonParam()

注意:?? 运算符在左侧表达式为 null 时返回右侧表达式。

英文:

You could declare it in a separate class, as such :

import &#39;package:shared_preferences/shared_preferences.dart&#39;;

class AppPrefs {
  static Future&lt;bool&gt; getCommonParam() async {
    var prefs = await SharedPreferences.getInstance();
    return prefs.getBool(&#39;param&#39;) ?? true;
  } 
}

You can then call AppPrefs.getCommonParam() from anywhere as long as you import the class.

Note : The ?? operator returns the right expression if the left expression is null.

答案2

得分: 1

创建具有特定名称和功能的类,并实现与相关类或小部件的方法,就像以下示例一样:

我创建了一个名为AppTheme的类:

class AppTheme {
  static final primaryFontFamily = "CerbriSans";

  static Color mainThemeColor() {
    return HexColor("#e62129");
  }
  
  static TextStyle tabTextStyle() {
    return TextStyle(
      fontFamily: AppTheme.primaryFontFamily,
      fontSize: 14,
      fontWeight: FontWeight.normal,
      color: AppTheme.mainThemeColor()
    );
  }
}

然后,我将在另一个类中使用这个类,就像这样:

Text(
  "示例",
  style: AppTheme.tabTextStyle(),
)

您只需导入与此类相关的库即可。

注意:此示例仅用于理想目的/仅用于概念。

英文:

Create Class with specific name and function and implement methods to related classes or widgets

like below example

I created a class with the name of

class AppTheme {
      static final primaryFontFaimly = &quot;CerbriSans&quot;;

      static Color mainThemeColor() {
        return HexColor(&quot;#e62129&quot;);
      }
      
      static TextStyle tabTextStyle() {
      return TextStyle(
        fontFamily: AppTheme.primaryFontFaimly,
        fontSize: 14,
        fontWeight: FontWeight.normal,
        color: AppTheme.mainThemeColor()
      );
     }
    }

and this class I will use in Another class like this

                  Text(
                        &quot;Example&quot;,
                        style: AppTheme.tabTextStyle(),
                      ),

you have to just import library to related this class

Note: This example only for the ideal purpose/only for idea

huangapple
  • 本文由 发表于 2020年1月3日 19:15:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定