網站首頁 編程語言 正文
一、需求來源
工作中偶爾會用到枚舉值和 int 的互相轉化,今天總結一下;
二、搞清楚 Flutter 枚舉屬性和方法
三、實現需求(以 PageView 滾動方式為例)
枚舉值轉 int:在當前索引值后加 .index 即可(默認從 0 開始);
int 轉枚舉值:需要擴展枚舉方法實現,實現如下;
定義枚舉 PageViewScrollType
/// PageView 滾動方式
enum PageViewScrollType {
/// 整屏滑動
full,
/// 拖拽滑動
drag,
/// 禁用滑動
none,
}
extension PageViewScrollType_IntExt on int{
/// int 轉枚舉
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 轉枚舉
PageViewScrollType get pageViewScrollType{
final allCases = PageViewScrollType.values;
// final index = this.clamp(0, allCases.length - 1);
// return allCases[index];
return this.toPageViewScrollType(true) ?? allCases.first;
}
}
最后
如此就實現了 枚舉值和 int的互相轉化,打印如下:
print("枚舉值索引: ${PageViewScrollType.full.index}");
print("枚舉值字符串: ${PageViewScrollType.drag.toString()}");
print("枚舉集合: ${PageViewScrollType.values}");
print("int 轉枚舉: ${0.toPageViewScrollType()}");
//枚舉值索引: 0
//枚舉值字符串: PageViewScrollType.drag
//枚舉集合: [ PageViewScrollType.full, PageViewScrollType.drag, PageViewScrollType.none ]
//int 轉枚舉: PageViewScrollType.full
原文鏈接:https://juejin.cn/post/7200221724045623355
相關推薦
- 2022-04-11 error: failed to push some refs to解決方法
- 2022-09-12 C++數據結構哈希表詳解_C 語言
- 2023-04-29 Python腳本修改Maya?ASCII文件路徑方法實現_python
- 2022-10-31 一文詳解如何使用Redis實現分布式鎖_Redis
- 2022-10-27 教你快速搭建?React?Native?開發環境_React
- 2022-09-01 ASP.NET輕量級MVC框架Nancy的基本用法_實用技巧
- 2023-03-20 C#如何判斷.Net?Framework版本是否滿足軟件運行需要的版本_C#教程
- 2022-12-15 Python?OpenCV中cv2.minAreaRect實例解析_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支