|
| 1 | +# ComposeCalculator |
| 2 | +基于 Compose With Material Design 3 的计算器 |
| 3 | +<br> |
| 4 | +<br> |
| 5 | +.webp?raw=true) |
| 6 | +.webp?raw=true) |
| 7 | +<br> |
| 8 | +## 简介 |
| 9 | +### UI |
| 10 | +直接抄谷歌自家那个的计算器, |
| 11 | +不过横屏为了实现一套布局自适应横竖屏就没一样了。 |
| 12 | +## 实现 |
| 13 | +个人觉得最有意思的是通过自定义布局实现一套布局自适应横竖屏。 |
| 14 | +> 多说无益,上代码。 |
| 15 | +### TheLayout 示例调用 |
| 16 | +```kotlin |
| 17 | +@Preview(showBackground = true) |
| 18 | +@Composable |
| 19 | +fun TheLayoutPreview() { |
| 20 | + |
| 21 | + ComposeCalculatorTheme { |
| 22 | + // 自定义布局,自适应横竖屏 |
| 23 | + TheLayout(modifier = Modifier.fillMaxSize()) { |
| 24 | + |
| 25 | + // 在横竖屏下不变的组件 |
| 26 | + Column( |
| 27 | + modifier = Modifier |
| 28 | + .weight(1f) |
| 29 | + .fillMaxSize() |
| 30 | + .background(Color.White) |
| 31 | + ) { |
| 32 | + Text(text = "Column1") |
| 33 | + } |
| 34 | + |
| 35 | + // 根据屏幕方向切换不同内容的组件 |
| 36 | + Row( |
| 37 | + modifier = Modifier |
| 38 | + .weight(1f) |
| 39 | + .fillMaxSize() |
| 40 | + .background(Color.LightGray) |
| 41 | + ) { |
| 42 | + when (isHorizontal()) { |
| 43 | + true -> { |
| 44 | + Text(text = "Row with Horizontal") |
| 45 | + } |
| 46 | + false -> { |
| 47 | + Text(text = "Row with Vertical") |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // 仅根据屏幕方向对布局配置做差异化的组件 |
| 53 | + Column( |
| 54 | + modifier = Modifier |
| 55 | + .isHorizontal { |
| 56 | + weight(1f) |
| 57 | + } |
| 58 | + .isNotHorizontal { |
| 59 | + weight(2f) |
| 60 | + } |
| 61 | + .fillMaxSize() |
| 62 | + .background(Color.White) |
| 63 | + ) { |
| 64 | + // 不在横屏透明度就会变 0.1 的组件 |
| 65 | + Text( |
| 66 | + text = "Only Horizontal", |
| 67 | + modifier = Modifier.isNotHorizontal { alpha(0.1f) }) |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +<br> |
| 79 | + |
| 80 | +### 实现思路.1 |
| 81 | +> 首先需要获取屏幕方向 |
| 82 | +```kotlin |
| 83 | +@Composable |
| 84 | +@Stable |
| 85 | +fun isHorizontal() = when (LocalConfiguration.current.orientation) { |
| 86 | + Configuration.ORIENTATION_LANDSCAPE -> true |
| 87 | + // Other wise |
| 88 | + else -> false |
| 89 | +} |
| 90 | +``` |
| 91 | +虽然 Compose 没直接提供屏幕数据的Api,但能通过 Activity 的 Configuration 判断方向 |
| 92 | + |
| 93 | +<br> |
| 94 | + |
| 95 | +### 实现思路.2 |
| 96 | +> 然后就是根据屏幕方向切换相应布局 |
| 97 | +
|
| 98 | + |
| 99 | +```kotlin |
| 100 | +@Composable |
| 101 | +fun TheLayout( |
| 102 | + content: @Composable TheLayoutScopeInstance.() -> Unit |
| 103 | +) { |
| 104 | + |
| 105 | + val scope by remember { |
| 106 | + mutableStateOf(TheLayoutScopeInstance(null, null)) |
| 107 | + } |
| 108 | + |
| 109 | + when (isHorizontal()) { |
| 110 | + |
| 111 | + true -> { |
| 112 | + Row(···) { |
| 113 | + scope.apply { |
| 114 | + rowScope = this@Row |
| 115 | + content() |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Other wise |
| 121 | + false -> { |
| 122 | + Column(···) { |
| 123 | + scope.apply { |
| 124 | + columnScope = this@Column |
| 125 | + content() |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | +} |
| 135 | +``` |
| 136 | +<br> |
| 137 | +<br> |
| 138 | +但会发现 Column 和 Row 混用导致像是 Modifier.weight() 之类由 布局Scope 提供的 Modifier 扩展函数没法用。 |
| 139 | +<br> |
| 140 | +不过翻源码实现可以发现扩展函数是由 ColumnScope 和 RowScope 提供的,只要自己也实现一个自己 Scope 的 Modifier 扩展函数就行。 |
| 141 | +<br> |
| 142 | +<br> |
| 143 | + |
| 144 | +```kotlin |
| 145 | +@LayoutScopeMarker |
| 146 | +@Immutable |
| 147 | +class TheLayoutScopeInstance( |
| 148 | + var columnScope: ColumnScope?, var rowScope: RowScope? |
| 149 | +) : TheLayoutScope { |
| 150 | + |
| 151 | + ··· |
| 152 | + |
| 153 | + @Stable |
| 154 | + override fun Modifier.weight(weight: Float, fill: Boolean): Modifier { |
| 155 | + val modifier = this |
| 156 | + columnScope?.apply { |
| 157 | + return modifier.weight(weight, fill) |
| 158 | + } |
| 159 | + rowScope?.apply { |
| 160 | + return modifier.weight(weight, fill) |
| 161 | + } |
| 162 | + return modifier |
| 163 | + } |
| 164 | + |
| 165 | + ··· |
| 166 | + |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +然后再加上自定义 Modifier 的扩展函数就差不多了 |
| 171 | + |
| 172 | +> 非必要实现,但对于实际使用来说很方便 |
| 173 | +
|
| 174 | +```kotlin |
| 175 | +@Stable |
| 176 | +fun Modifier.isHorizontal(doWork: Modifier.() -> Modifier) = composed { |
| 177 | + if (isHorizontal()) doWork(this) |
| 178 | + else this |
| 179 | +} |
| 180 | + |
| 181 | +@Stable |
| 182 | +fun Modifier.isNotHorizontal(doWork: Modifier.() -> Modifier) = composed { |
| 183 | + if (!isHorizontal()) doWork(this) |
| 184 | + else this |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +## 虽然关系不大但还是想加上的几张图 |
| 189 | + |
| 190 | + |
| 191 | +.webp?raw=true) |
| 192 | +.webp?raw=true) |
0 commit comments