iOS 风格弹框组件集 (Compose版)

打印 上一主题 下一主题

主题 1697|帖子 1697|积分 5091

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
iOSStyleDialogs.kt

  1. import androidx.compose.foundation.layout.*
  2. import androidx.compose.foundation.shape.RoundedCornerShape
  3. import androidx.compose.material3.*
  4. import androidx.compose.runtime.*
  5. import androidx.compose.ui.Alignment
  6. import androidx.compose.ui.Modifier
  7. import androidx.compose.ui.graphics.Color
  8. import androidx.compose.ui.text.style.TextAlign
  9. import androidx.compose.ui.tooling.preview.Preview
  10. import androidx.compose.ui.unit.dp
  11. import androidx.compose.ui.window.DialogProperties
  12. // 1. 基础确认对话框
  13. @OptIn(ExperimentalMaterial3Api::class)
  14. @Composable
  15. fun IOSConfirmDialog(
  16.     title: String,
  17.     message: String,
  18.     onDismiss: () -> Unit,
  19.     confirmText: String = "确认",
  20.     cancelText: String = "取消",
  21.     confirmColor: Color = MaterialTheme.colorScheme.primary,
  22.     cancelColor: Color = MaterialTheme.colorScheme.onSurface,
  23.     onConfirm: () -> Unit,
  24.     onCancel: () -> Unit = onDismiss
  25. ) {
  26.     AlertDialog(
  27.         onDismissRequest = onDismiss,
  28.         properties = DialogProperties(
  29.             dismissOnBackPress = true,
  30.             dismissOnClickOutside = false
  31.         )
  32.     ) {
  33.         Surface(
  34.             shape = RoundedCornerShape(12.dp),
  35.             tonalElevation = 6.dp
  36.         ) {
  37.             Column {
  38.                 // 标题
  39.                 Text(
  40.                     text = title,
  41.                     style = MaterialTheme.typography.headlineSmall,
  42.                     textAlign = TextAlign.Center,
  43.                     modifier = Modifier
  44.                         .fillMaxWidth()
  45.                         .padding(top = 24.dp, start = 24.dp, end = 24.dp)
  46.                 )
  47.                 // 消息内容
  48.                 Text(
  49.                     text = message,
  50.                     style = MaterialTheme.typography.bodyMedium,
  51.                     textAlign = TextAlign.Center,
  52.                     modifier = Modifier
  53.                         .fillMaxWidth()
  54.                         .padding(24.dp)
  55.                 )
  56.                 // 按钮区域
  57.                 Divider(color = Color.LightGray.copy(alpha = 0.3f))
  58.                 Row {
  59.                     TextButton(
  60.                         onClick = {
  61.                             onCancel()
  62.                             onDismiss()
  63.                         },
  64.                         modifier = Modifier
  65.                             .weight(1f)
  66.                     ) {
  67.                         Text(
  68.                             text = cancelText,
  69.                             color = cancelColor,
  70.                             style = MaterialTheme.typography.labelLarge
  71.                         )
  72.                     }
  73.                     Divider(
  74.                         color = Color.LightGray.copy(alpha = 0.3f),
  75.                         modifier = Modifier
  76.                             .width(1.dp)
  77.                             .height(44.dp)
  78.                     )
  79.                     TextButton(
  80.                         onClick = {
  81.                             onConfirm()
  82.                             onDismiss()
  83.                         },
  84.                         modifier = Modifier
  85.                             .weight(1f)
  86.                     ) {
  87.                         Text(
  88.                             text = confirmText,
  89.                             color = confirmColor,
  90.                             style = MaterialTheme.typography.labelLarge
  91.                         )
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
  98. // 2. 带输入框的对话框
  99. @OptIn(ExperimentalMaterial3Api::class)
  100. @Composable
  101. fun IOSInputDialog(
  102.     title: String,
  103.     message: String,
  104.     onDismiss: () -> Unit,
  105.     confirmText: String = "确认",
  106.     cancelText: String = "取消",
  107.     confirmColor: Color = MaterialTheme.colorScheme.primary,
  108.     cancelColor: Color = MaterialTheme.colorScheme.onSurface,
  109.     initialValue: String = "",
  110.     placeholder: String = "请输入",
  111.     onConfirm: (String) -> Unit,
  112.     onCancel: () -> Unit = onDismiss
  113. ) {
  114.     var inputValue by remember { mutableStateOf(initialValue) }
  115.     AlertDialog(
  116.         onDismissRequest = onDismiss,
  117.         properties = DialogProperties(
  118.             dismissOnBackPress = true,
  119.             dismissOnClickOutside = false
  120.         )
  121.     ) {
  122.         Surface(
  123.             shape = RoundedCornerShape(12.dp),
  124.             tonalElevation = 6.dp
  125.         ) {
  126.             Column {
  127.                 // 标题
  128.                 Text(
  129.                     text = title,
  130.                     style = MaterialTheme.typography.headlineSmall,
  131.                     textAlign = TextAlign.Center,
  132.                     modifier = Modifier
  133.                         .fillMaxWidth()
  134.                         .padding(top = 24.dp, start = 24.dp, end = 24.dp)
  135.                 )
  136.                 // 消息内容和输入框
  137.                 Column(
  138.                     modifier = Modifier.padding(horizontal = 24.dp)
  139.                 ) {
  140.                     Text(
  141.                         text = message,
  142.                         style = MaterialTheme.typography.bodyMedium,
  143.                         textAlign = TextAlign.Center,
  144.                         modifier = Modifier
  145.                             .fillMaxWidth()
  146.                             .padding(bottom = 12.dp)
  147.                     )
  148.                     OutlinedTextField(
  149.                         value = inputValue,
  150.                         onValueChange = { inputValue = it },
  151.                         modifier = Modifier.fillMaxWidth(),
  152.                         singleLine = true,
  153.                         placeholder = { Text(placeholder) },
  154.                         shape = RoundedCornerShape(8.dp)
  155.                     )
  156.                 }
  157.                 // 按钮区域
  158.                 Divider(color = Color.LightGray.copy(alpha = 0.3f))
  159.                 Row {
  160.                     TextButton(
  161.                         onClick = {
  162.                             onCancel()
  163.                             onDismiss()
  164.                         },
  165.                         modifier = Modifier
  166.                             .weight(1f)
  167.                     ) {
  168.                         Text(
  169.                             text = cancelText,
  170.                             color = cancelColor,
  171.                             style = MaterialTheme.typography.labelLarge
  172.                         )
  173.                     }
  174.                     Divider(
  175.                         color = Color.LightGray.copy(alpha = 0.3f),
  176.                         modifier = Modifier
  177.                             .width(1.dp)
  178.                             .height(44.dp)
  179.                     )
  180.                     TextButton(
  181.                         onClick = {
  182.                             onConfirm(inputValue)
  183.                             onDismiss()
  184.                         },
  185.                         modifier = Modifier
  186.                             .weight(1f)
  187.                     ) {
  188.                         Text(
  189.                             text = confirmText,
  190.                             color = confirmColor,
  191.                             style = MaterialTheme.typography.labelLarge
  192.                         )
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.     }
  198. }
  199. // 3. 单按钮提示框
  200. @OptIn(ExperimentalMaterial3Api::class)
  201. @Composable
  202. fun IOSInfoDialog(
  203.     title: String,
  204.     message: String,
  205.     onDismiss: () -> Unit,
  206.     buttonText: String = "确定",
  207.     buttonColor: Color = MaterialTheme.colorScheme.primary,
  208.     onConfirm: () -> Unit = onDismiss
  209. ) {
  210.     AlertDialog(
  211.         onDismissRequest = onDismiss,
  212.         properties = DialogProperties(
  213.             dismissOnBackPress = true,
  214.             dismissOnClickOutside = false
  215.         )
  216.     ) {
  217.         Surface(
  218.             shape = RoundedCornerShape(12.dp),
  219.             tonalElevation = 6.dp
  220.         ) {
  221.             Column {
  222.                 // 标题
  223.                 Text(
  224.                     text = title,
  225.                     style = MaterialTheme.typography.headlineSmall,
  226.                     textAlign = TextAlign.Center,
  227.                     modifier = Modifier
  228.                         .fillMaxWidth()
  229.                         .padding(top = 24.dp, start = 24.dp, end = 24.dp)
  230.                 )
  231.                 // 消息内容
  232.                 Text(
  233.                     text = message,
  234.                     style = MaterialTheme.typography.bodyMedium,
  235.                     textAlign = TextAlign.Center,
  236.                     modifier = Modifier
  237.                         .fillMaxWidth()
  238.                         .padding(24.dp)
  239.                 )
  240.                 // 按钮区域
  241.                 Divider(color = Color.LightGray.copy(alpha = 0.3f))
  242.                 TextButton(
  243.                     onClick = {
  244.                         onConfirm()
  245.                         onDismiss()
  246.                     },
  247.                     modifier = Modifier.fillMaxWidth()
  248.                 ) {
  249.                     Text(
  250.                         text = buttonText,
  251.                         color = buttonColor,
  252.                         style = MaterialTheme.typography.labelLarge
  253.                     )
  254.                 }
  255.             }
  256.         }
  257.     }
  258. }
  259. // 预览
  260. @Preview
  261. @Composable
  262. private fun PreviewDialogs() {
  263.     MaterialTheme {
  264.         Column(
  265.             modifier = Modifier
  266.                 .fillMaxSize()
  267.                 .padding(16.dp),
  268.             verticalArrangement = Arrangement.spacedBy(16.dp),
  269.             horizontalAlignment = Alignment.CenterHorizontally
  270.         ) {
  271.             // 预览确认对话框
  272.             var showConfirm by remember { mutableStateOf(false) }
  273.             Button(onClick = { showConfirm = true }) {
  274.                 Text("显示确认对话框")
  275.             }
  276.             if (showConfirm) {
  277.                 IOSConfirmDialog(
  278.                     title = "删除确认",
  279.                     message = "确定要删除这条数据吗?删除后不可恢复",
  280.                     onDismiss = { showConfirm = false },
  281.                     onConfirm = { /* 确认操作 */ }
  282.                 )
  283.             }
  284.             // 预览输入对话框
  285.             var showInput by remember { mutableStateOf(false) }
  286.             Button(onClick = { showInput = true }) {
  287.                 Text("显示输入对话框")
  288.             }
  289.             if (showInput) {
  290.                 IOSInputDialog(
  291.                     title = "修改昵称",
  292.                     message = "请输入新的昵称",
  293.                     onDismiss = { showInput = false },
  294.                     onConfirm = { input -> println("新昵称: $input") }
  295.                 )
  296.             }
  297.             // 预览提示对话框
  298.             var showInfo by remember { mutableStateOf(false) }
  299.             Button(onClick = { showInfo = true }) {
  300.                 Text("显示提示对话框")
  301.             }
  302.             if (showInfo) {
  303.                 IOSInfoDialog(
  304.                     title = "操作成功",
  305.                     message = "您的资料已提交审核",
  306.                     onDismiss = { showInfo = false }
  307.                 )
  308.             }
  309.         }
  310.     }
  311. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表