تتيح القوائم المنسدلة للمستخدمين النقر على رمز أو حقل نصي أو مكوّن آخر، ثم الاختيار من قائمة من الخيارات على سطح مؤقت. يصف هذا الدليل كيفية إنشاء قوائم أساسية وقوائم أكثر تعقيدًا باستخدام الفواصل والرموز.
مساحة عرض واجهة برمجة التطبيقات
استخدِم المكوّنات DropdownMenu وDropdownMenuItem وIconButton وExposedDropdownMenuBox لتنفيذ القوائم المنسدلة. يتم استخدام المكوّنين DropdownMenu وDropdownMenuItem لعرض عناصر القائمة، بينما يمكن أن يؤدي المكوّن IconButton أو مكوّنات أخرى إلى عرض القائمة.
يُستخدَم ExposedDropdownMenuBox لإنشاء قائمة مرتبطة بحقل نصي.
تشمل المَعلمات الرئيسية للمكوّن DropdownMenu ما يلي:
-
expanded: يشير إلى ما إذا كانت القائمة مرئية. -
onDismissRequest: يُستخدَم هذا المعامل لمعالجة إغلاق القائمة. -
content: المحتوى القابل للإنشاء في القائمة، والذي يحتوي عادةً على عناصرDropdownMenuItemقابلة للإنشاء.
تشمل المَعلمات الرئيسية لـ DropdownMenuItem ما يلي:
-
text: تحدّد هذه السمة المحتوى المعروض في عنصر القائمة. -
onClick: دالة ردّ الاتصال لمعالجة التفاعل مع العنصر في القائمة
إنشاء قائمة منسدلة أساسية
يوضّح المقتطف التالي الحد الأدنى من تنفيذ DropdownMenu:
@Composable fun MinimalDropdownMenu() { var expanded by remember { mutableStateOf(false) } Box( modifier = Modifier .padding(16.dp) ) { IconButton(onClick = { expanded = !expanded }) { Icon(painter = rememberVectorPainter(AppIcons.MoreVert), contentDescription = "More options") } DropdownMenu( expanded = expanded, onDismissRequest = { expanded = false } ) { DropdownMenuItem( text = { Text("Option 1") }, onClick = { /* Do something... */ } ) DropdownMenuItem( text = { Text("Option 2") }, onClick = { /* Do something... */ } ) } } }
النقاط الرئيسية حول الرمز
- يحدّد هذا المثال
DropdownMenuأساسيًا يحتوي على عنصرَي قائمة. - تتحكّم المَعلمة
expandedفي ظهور القائمة كقائمة موسّعة أو مصغّرة. - تحدّد المَعلمة
onDismissRequestدالة رد اتصال يتم تنفيذها عندما يغلق المستخدم القائمة. - تمثّل الدالة المركّبة
DropdownMenuItemالعناصر القابلة للاختيار في القائمة المنسدلة. - يؤدي النقر على
IconButtonإلى توسيع القائمة وتصغيرها.
النتيجة
إنشاء قائمة منسدلة أطول
يمكن التنقّل في "DropdownMenu" تلقائيًا إذا تعذّر عرض جميع خيارات القائمة
في الوقت نفسه. ينشئ المقتطف التالي قائمة منسدلة أطول قابلة للتمرير:
@Composable fun LongBasicDropdownMenu() { var expanded by remember { mutableStateOf(false) } // Placeholder list of 100 strings for demonstration val menuItemData = List(100) { "Option ${it + 1}" } Box( modifier = Modifier .padding(16.dp) ) { IconButton(onClick = { expanded = !expanded }) { Icon(painter = rememberVectorPainter(AppIcons.MoreVert), contentDescription = "More options") } DropdownMenu( expanded = expanded, onDismissRequest = { expanded = false } ) { menuItemData.forEach { option -> DropdownMenuItem( text = { Text(option) }, onClick = { /* Do something... */ } ) } } } }
النقاط الرئيسية حول الرمز
- يمكن الانتقال في
DropdownMenuعندما يتجاوز الارتفاع الإجمالي لمحتواه المساحة المتاحة. تنشئ هذه التعليمة البرمجيةDropdownMenuقابلة للتمرير تعرض 100 عنصر نائب. - تنشئ حلقة
forEachالمركّباتDropdownMenuItemديناميكيًا. لا يتم إنشاء العناصر بشكل كسول، مما يعني أن جميع العناصر المنسدلة البالغ عددها 100 يتم إنشاؤها وموجودة في التركيب. - يؤدي النقر على
IconButtonإلى توسيعDropdownMenuوتصغيره. - تتيح لك دالة lambda
onClickضمن كلDropdownMenuItemتحديد الإجراء الذي يتم تنفيذه عندما يختار المستخدم عنصر قائمة.
النتيجة
ينتج عن مقتطف الرمز السابق القائمة التالية القابلة للتمرير:
إنشاء قائمة منسدلة أطول باستخدام فواصل
يعرض المقتطف التالي تنفيذًا أكثر تقدّمًا لقائمة منسدلة. In this snippet, leading and trailing icons are added to menu items, and dividers separate groups of menu items.
@Composable fun DropdownMenuWithDetails() { var expanded by remember { mutableStateOf(false) } Box( modifier = Modifier .fillMaxWidth() .padding(16.dp) ) { IconButton(onClick = { expanded = !expanded }) { Icon(painter = rememberVectorPainter(AppIcons.MoreVert), contentDescription = "More options") } DropdownMenu( expanded = expanded, onDismissRequest = { expanded = false } ) { // First section DropdownMenuItem( text = { Text("Profile") }, leadingIcon = { Icon(painter = rememberVectorPainter(AppIcons.Person), contentDescription = null) }, onClick = { /* Do something... */ } ) DropdownMenuItem( text = { Text("Settings") }, leadingIcon = { Icon(painter = rememberVectorPainter(AppIcons.Settings), contentDescription = null) }, onClick = { /* Do something... */ } ) HorizontalDivider() // Second section DropdownMenuItem( text = { Text("Send Feedback") }, leadingIcon = { Icon(painter = rememberVectorPainter(AppIcons.Feedback), contentDescription = null) }, trailingIcon = { Icon(painter = rememberVectorPainter(AppIcons.Send), contentDescription = null) }, onClick = { /* Do something... */ } ) HorizontalDivider() // Third section DropdownMenuItem( text = { Text("About") }, leadingIcon = { Icon(painter = rememberVectorPainter(AppIcons.Info), contentDescription = null) }, onClick = { /* Do something... */ } ) DropdownMenuItem( text = { Text("Help") }, leadingIcon = { Icon(painter = rememberVectorPainter(AppIcons.Help), contentDescription = null) }, trailingIcon = { Icon(painter = rememberVectorPainter(AppIcons.OpenInNew), contentDescription = null) }, onClick = { /* Do something... */ } ) } } }
يحدّد هذا الرمز DropdownMenu ضمن Box.
النقاط الرئيسية حول الرمز
- تضيف المَعلمتان
leadingIconوtrailingIconرموزًا إلى بدايةDropdownMenuItemونهايته. - يؤدي النقر على
IconButtonإلى توسيع القائمة. - يحتوي
DropdownMenuعلى عدة دوال مركّبة من النوعDropdownMenuItem، يمثّل كل منها إجراءً يمكن اختياره. - تُدرج الدوال المركّبة
HorizontalDividerخطًا أفقيًا لفصل مجموعات عناصر القائمة.
النتيجة
ينتج عن المقتطف السابق قائمة منسدلة تحتوي على رموز وفواصل:
تحديث M3 Expressive
يقدّم Material 3 Expressive مكوّنات DropdownMenuPopup وDropdownMenuGroup وCheckableDropdownMenuItem كخيارات إضافية إلى جانب DropdownMenu وDropdownMenuItem لإنشاء قوائم مقسّمة تحتوي على عناصر مجمّعة وفواصل وحالات قابلة للاختيار.
لمزيد من المعلومات حول تعديلات M3 Expressive للقوائم، يُرجى الاطّلاع على إرشادات قوائم التصميم المتعدد الأبعاد 3.
القوائم المقسّمة
تجمع القوائم المقسّمة الإجراءات ذات الصلة في أقسام مرئية مميزة مفصولة بفواصل أو فواصل. يوضّح المقتطف التالي كيفية تنفيذ قائمة منسدلة مقسّمة باستخدام DropdownMenuPopup وDropdownMenuGroup وCheckableDropdownMenuItem:
@OptIn(ExperimentalMaterial3Api::class) @Composable fun GroupedMenuSample() { val groupInteractionSource = remember { MutableInteractionSource() } var expanded by remember { mutableStateOf(false) } val groupLabels = listOf("Modification", "Navigation") val groupItemLabels = listOf(listOf("Edit", "Settings"), listOf("Home", "More Options")) val groupItemLeadingIcons = listOf(listOf(AppIcons.Edit, AppIcons.Settings), listOf(null, AppIcons.Info)) val groupItemCheckedLeadingIcons = listOf( listOf(AppIcons.EditFilled, AppIcons.SettingsFilled), listOf(AppIcons.Check, AppIcons.InfoFilled), ) val groupItemTrailingIcons: List<List<ImageVector?>> = listOf(listOf(null, null), listOf(AppIcons.Home, AppIcons.MoreVert)) val groupItemCheckedTrailingIcons: List<List<ImageVector?>> = listOf(listOf(null, null), listOf(AppIcons.HomeFilled, AppIcons.MoreVert)) val groupItemSupportingText: List<List<String?>> = listOf(listOf("Edit mode", null), listOf(null, "Opens menu")) val checked = remember { listOf(mutableStateListOf(false, false), mutableStateListOf(false, false)) } Box(modifier = Modifier.fillMaxSize().wrapContentSize(Alignment.TopStart)) { // Icon button should have a tooltip associated with it for a11y. TooltipBox( positionProvider = TooltipDefaults.rememberTooltipPositionProvider(TooltipAnchorPosition.Above), tooltip = { PlainTooltip { Text("Localized description") } }, state = rememberTooltipState(), ) { IconButton(onClick = { expanded = true }) { Icon(painter = rememberVectorPainter(AppIcons.MoreVert), contentDescription = "Localized description") } } DropdownMenuPopup(expanded = expanded, onDismissRequest = { expanded = false }) { val groupCount = groupLabels.size groupLabels.fastForEachIndexed { groupIndex, label -> DropdownMenuGroup( shapes = MenuDefaults.groupShape(groupIndex, groupCount), interactionSource = groupInteractionSource, ) { MenuDefaults.DropdownMenuGroupLabel { Text(label) } HorizontalDivider(modifier = Modifier.padding(MenuDefaults.HorizontalDividerPadding)) val groupItemCount = groupItemLabels[groupIndex].size groupItemLabels[groupIndex].fastForEachIndexed { itemIndex, itemLabel -> CheckableDropdownMenuItem( text = { Text(itemLabel) }, supportingText = groupItemSupportingText[groupIndex][itemIndex]?.let { supportingText -> { Text(supportingText) } }, shapes = MenuDefaults.itemShape(itemIndex, groupItemCount), leadingIcon = groupItemLeadingIcons[groupIndex][itemIndex]?.let { iconData -> { Icon( painter = rememberVectorPainter(iconData), modifier = Modifier.size(MenuDefaults.LeadingIconSize), contentDescription = null, ) } }, checkedLeadingIcon = { Icon( painter = rememberVectorPainter(groupItemCheckedLeadingIcons[groupIndex][itemIndex]), modifier = Modifier.size(MenuDefaults.LeadingIconSize), contentDescription = null, ) }, trailingContent = if (checked[groupIndex][itemIndex]) { groupItemCheckedTrailingIcons[groupIndex][itemIndex]?.let { iconData -> { Icon( painter = rememberVectorPainter(iconData), modifier = Modifier.size(MenuDefaults.TrailingIconSize), contentDescription = null, ) } } } else { groupItemTrailingIcons[groupIndex][itemIndex]?.let { iconData -> { Icon( painter = rememberVectorPainter(iconData), modifier = Modifier.size(MenuDefaults.TrailingIconSize), contentDescription = null, ) } } }, checked = checked[groupIndex][itemIndex], onCheckedChange = { checked[groupIndex][itemIndex] = it }, ) } } if (groupIndex != groupCount - 1) { Spacer(Modifier.height(MenuDefaults.GroupSpacing)) } } if (checked.last().last()) { DropdownMenuButtonGroup() } } } } @Composable private fun DropdownMenuButtonGroup() { ButtonGroup( overflowIndicator = { menuState -> ButtonGroupDefaults.OverflowIndicator(menuState) }, modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(2.dp, Alignment.CenterHorizontally), ) { customItem( buttonGroupContent = { FilledIconButton( onClick = {}, colors = IconButtonDefaults.filledIconButtonColors( containerColor = MenuDefaults.groupStandardContainerColor ), ) { Icon(painter = rememberVectorPainter(AppIcons.ThumbUp), contentDescription = "Localized description") } }, menuContent = { DropdownMenuItem( leadingIcon = { Icon(painter = rememberVectorPainter(AppIcons.ThumbUp), contentDescription = null) }, text = { Text("Thumbs up") }, onClick = {}, ) }, ) customItem( buttonGroupContent = { FilledIconButton( onClick = {}, colors = IconButtonDefaults.filledIconButtonColors( containerColor = MenuDefaults.groupStandardContainerColor ), ) { Icon(painter = rememberVectorPainter(AppIcons.ThumbDown), contentDescription = "Localized description") } }, menuContent = { DropdownMenuItem( leadingIcon = { Icon(painter = rememberVectorPainter(AppIcons.ThumbDown), contentDescription = null) }, text = { Text("Thumbs down") }, onClick = {}, ) }, ) customItem( buttonGroupContent = { FilledIconButton( onClick = {}, colors = IconButtonDefaults.filledIconButtonColors( containerColor = MenuDefaults.groupStandardContainerColor ), ) { Icon(painter = rememberVectorPainter(AppIcons.TagFaces), contentDescription = "Localized description") } }, menuContent = { DropdownMenuItem( leadingIcon = { Icon(painter = rememberVectorPainter(AppIcons.TagFaces), contentDescription = null) }, text = { Text("Emotes") }, onClick = {}, ) }, ) } }
مراجع إضافية
- التصميم المتعدد الأبعاد: القوائم