欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)

 更新時(shí)間:2023年02月17日 14:08:43   作者:SoaringHeart  
這篇文章主要為大家介紹了Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、需求來源

工作中偶爾會(huì)用到枚舉值和 int 的互相轉(zhuǎn)化,今天總結(jié)一下;

二、搞清楚 Flutter 枚舉屬性和方法

三、實(shí)現(xiàn)需求(以 PageView 滾動(dòng)方式為例)

枚舉值轉(zhuǎn) int:在當(dāng)前索引值后加 .index 即可(默認(rèn)從 0 開始);

int 轉(zhuǎn)枚舉值:需要擴(kuò)展枚舉方法實(shí)現(xiàn),實(shí)現(xiàn)如下;

定義枚舉 PageViewScrollType

/// PageView 滾動(dòng)方式
enum PageViewScrollType {
  /// 整屏滑動(dòng)
  full,
  /// 拖拽滑動(dòng)
  drag,
  /// 禁用滑動(dòng)
  none,
}
extension PageViewScrollType_IntExt on int{
  /// int 轉(zhuǎn)枚舉
  PageViewScrollType? toPageViewScrollType([bool isClamp = true]){
    final allCases = PageViewScrollType.values;
    if (!isClamp) {
      if (this < 0 || this > allCases.length - 1) {
        return null;
      }
      return allCases[this];
    }
    final index = this.clamp(0, allCases.length - 1);
    return allCases[index];
  }
  /// int 轉(zhuǎn)枚舉
  PageViewScrollType get pageViewScrollType{
    final allCases = PageViewScrollType.values;
    // final index = this.clamp(0, allCases.length - 1);
    // return allCases[index];
    return this.toPageViewScrollType(true) ?? allCases.first;
  }
}

最后

如此就實(shí)現(xiàn)了 枚舉值和 int的互相轉(zhuǎn)化,打印如下:

print("枚舉值索引: ${PageViewScrollType.full.index}");
print("枚舉值字符串: ${PageViewScrollType.drag.toString()}");
print("枚舉集合: ${PageViewScrollType.values}");
print("int 轉(zhuǎn)枚舉: ${0.toPageViewScrollType()}");

//枚舉值索引: 0

//枚舉值字符串: PageViewScrollType.drag

//枚舉集合: [ PageViewScrollType.full, PageViewScrollType.drag, PageViewScrollType.none ]

//int 轉(zhuǎn)枚舉: PageViewScrollType.full

以上就是Flutter 枚舉值enum和int互相轉(zhuǎn)化總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Flutter枚舉值enum int互相轉(zhuǎn)化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論