ย้ายข้อมูลจาก Material 2.5 ไปยัง Material 3 ใน Compose สำหรับ Wear OS

Material 3 Expressive คือการพัฒนา Material Design ขั้นถัดไป ซึ่งรวมถึง การจัดธีม คอมโพเนนต์ และฟีเจอร์การปรับเปลี่ยนในแบบของคุณที่อัปเดตแล้ว เช่น สีแบบเปลี่ยนอัตโนมัติ

คู่มือนี้มุ่งเน้นการย้ายข้อมูลจากไลบรารี Jetpack ของ Wear Compose Material 2.5 (androidx.wear.compose) ไปยังไลบรารี Jetpack ของ Wear Compose Material 3 (androidx.wear.compose.material3) สำหรับ แอป

แนวทาง

หากต้องการย้ายข้อมูลโค้ดแอปจาก M2.5 ไปยัง M3 ให้ทำตาม แนวทางเดียวกันกับที่อธิบายไว้ในคำแนะนำในการย้ายข้อมูล Compose Material สำหรับโทรศัพท์ โดยเฉพาะอย่างยิ่ง

การขึ้นต่อกัน

M3 มีแพ็กเกจและเวอร์ชันแยกต่างหากจาก M2.5 ดังนี้

M2.5

implementation("androidx.wear.compose:compose-material:1.4.0")

M3

implementation("androidx.wear.compose:compose-material3:1.5.0-beta05")

ดู M3 เวอร์ชันล่าสุดได้ในหน้ารุ่น Wear Compose Material 3

ไลบรารี Wear Compose Foundation เวอร์ชัน 1.5.0-beta01 มีคอมโพเนนต์ใหม่บางอย่างที่ออกแบบมาให้ทำงานร่วมกับคอมโพเนนต์ Material 3 ในทำนองเดียวกัน SwipeDismissableNavHost จากไลบรารี Wear Compose Navigation มีภาพเคลื่อนไหวที่อัปเดตเมื่อเรียกใช้ใน Wear OS 6 (API ระดับ 36) หรือ สูงกว่า เมื่ออัปเดตเป็น Wear Compose Material 3 เราขอแนะนำให้คุณอัปเดตไลบรารี Wear Compose Foundation และ Navigation ด้วย

implementation("androidx.wear.compose:compose-foundation:1.5.0-beta05")
implementation("androidx.wear.compose:compose-navigation:1.5.0-beta05")

ธีม

ทั้งใน M2.5 และ M3 องค์ประกอบธีมที่ใช้ร่วมกันได้มีชื่อว่า MaterialTheme แต่ แพ็กเกจการนำเข้าและพารามิเตอร์จะแตกต่างกัน ใน M3 เราได้เปลี่ยนชื่อพารามิเตอร์ Colors เป็น ColorScheme และได้เปิดตัว MotionScheme สำหรับการใช้การเปลี่ยน

M2.5

import androidx.wear.compose.material.MaterialTheme

MaterialTheme(
        colors = AppColors,
        typography = AppTypography,
        shapes = AppShapes,
        content = content
)

M3

import androidx.wear.compose.material3.MaterialTheme

MaterialTheme(
        colorScheme = AppColorScheme,
        typography = AppTypography,
        shapes = AppShapes,
        motionScheme = AppMotionScheme,
        content = content
)

สี

ระบบสีใน M3 แตกต่างจาก M2.5 อย่างมาก พารามิเตอร์จำนวนสี เพิ่มขึ้น โดยมีชื่อแตกต่างกันและแมปกับคอมโพเนนต์ M3 แตกต่างกัน ใน Compose การตั้งค่านี้จะมีผลกับคลาส M2.5 Colors, คลาส M3 ColorScheme และฟังก์ชันที่เกี่ยวข้อง

M2.5

import androidx.wear.compose.material.Colors

val appColorScheme: Colors = Colors(
   // M2.5 Color parameters
)

M3

import androidx.wear.compose.material3.ColorScheme

val appColorScheme: ColorScheme = ColorScheme(
   // M3 ColorScheme parameters
)

ตารางต่อไปนี้อธิบายความแตกต่างที่สําคัญระหว่าง M2.5 กับ M3

M2.5

M3

Color

เปลี่ยนชื่อเป็น ColorScheme แล้ว

13 สี

28 สี

ไม่มี

การใช้ธีมสีแบบเปลี่ยนอัตโนมัติใหม่

ไม่มี

สีขั้นที่ 3 ใหม่เพื่อการแสดงออกที่มากขึ้น

การใช้ธีมสีแบบเปลี่ยนอัตโนมัติ

ฟีเจอร์ใหม่ใน M3 คือการกำหนดธีมสีแบบไดนามิก หากผู้ใช้เปลี่ยน สีหน้าปัด สีใน UI จะเปลี่ยนให้ตรงกัน

ใช้ฟังก์ชัน dynamicColorScheme เพื่อใช้รูปแบบสีแบบไดนามิก และระบุ defaultColorScheme เป็นตัวเลือกสำรองในกรณีที่รูปแบบสีแบบไดนามิก ไม่พร้อมใช้งาน

@Composable
fun myApp() {
  val myColorScheme = myBrandColors()
  val dynamicColorScheme = dynamicColorScheme(LocalContext.current)
  MaterialTheme(colorScheme = dynamicColorScheme ?: myBrandColors) {...}
}

การพิมพ์

ระบบการพิมพ์ใน M3 แตกต่างจาก M2 และมีฟีเจอร์ต่อไปนี้

  • รูปแบบข้อความใหม่ 9 แบบ
  • แบบอักษรแบบยืดหยุ่นซึ่งช่วยให้ปรับแต่งสเกลแบบอักษรสำหรับ ความหนา ความกว้าง และความกลมต่างๆ ได้
  • AnimatedText ซึ่งใช้แบบอักษรแบบยืดหยุ่น

M2.5

import androidx.wear.compose.material.Typography

val Typography = Typography(
   // M2.5 TextStyle parameters
)

M3

import androidx.wear.compose.material3.Typography

val Typography = Typography(
   // M3 TextStyle parameters
)

แบบอักษร Flex

แบบอักษรแบบยืดหยุ่นช่วยให้นักออกแบบระบุความกว้างและน้ำหนักของแบบอักษรสำหรับขนาดที่เฉพาะเจาะจงได้

รูปแบบข้อความ

TextStyles ต่อไปนี้พร้อมใช้งานใน M3 โดยค่าเริ่มต้น M3 จะใช้คอมโพเนนต์ต่างๆ ดังนี้

การพิมพ์

TextStyle

จอแสดงผล

displayLarge, displayMedium, displaySmall

ชื่อ

titleLarge, titleMedium, titleSmall

ป้ายกำกับ

labelLarge, labelMedium, labelSmall

เนื้อความ

bodyLarge, bodyMedium, bodySmall, bodyExtraSmall

ตัวเลข

numeralExtraLarge, numeralLarge, numeralMedium, numeralSmall, numeralExtraSmall

เส้นโค้ง

arcLarge, arcMedium, arcSmall

รูปทรง

ระบบรูปร่างใน M3 แตกต่างจาก M2 จำนวนพารามิเตอร์รูปร่าง เพิ่มขึ้น มีการตั้งชื่อพารามิเตอร์แตกต่างกัน และมีการแมปกับคอมโพเนนต์ M3 แตกต่างกัน ขนาดรูปร่างที่พร้อมใช้งานมีดังนี้

  • เล็กพิเศษ
  • เล็ก
  • ปานกลาง
  • ใหญ่
  • ใหญ่พิเศษ

ใน Compose จะมีผลกับคลาส M2 Shapes และคลาส M3 Shapes ดังนี้

M2.5

import androidx.wear.compose.material.Shapes

val Shapes = Shapes(
   // M2.5 Shapes parameters
)

M3

import androidx.wear.compose.material3.Shapes

val Shapes = Shapes(
   // M3 Shapes parameters
)

ใช้การแมปพารามิเตอร์รูปร่างจากย้ายข้อมูลจาก Material 2 ไปยัง Material 3 ใน Compose เป็นจุดเริ่มต้น

การมอร์ฟรูปร่าง

M3 เปิดตัวการเปลี่ยนรูปร่าง: ตอนนี้รูปร่างจะเปลี่ยนไปตามการโต้ตอบ

ลักษณะการทำงานของการเปลี่ยนรูปร่างมีให้ใช้งานเป็นรูปแบบต่างๆ ในปุ่มกลมหลายปุ่ม ดังนี้

ปุ่ม

ฟังก์ชันการเปลี่ยนรูปร่าง

IconButton

IconButtonDefaults.animatedShape() จะสร้างภาพเคลื่อนไหวของปุ่มไอคอนเมื่อกด

IconToggleButton

IconToggleButtonDefaults.animatedShape() จะเคลื่อนไหวปุ่มสลับไอคอนเมื่อกดและ

IconToggleButtonDefaults.variantAnimatedShapes() จะสร้างภาพเคลื่อนไหวให้กับปุ่มสลับไอคอนเมื่อกดและเลือก/ยกเลิกการเลือก

TextButton

TextButtonDefaults.animatedShape() จะทำให้ปุ่มข้อความเคลื่อนไหวเมื่อกด

TextToggleButton

TextToggleButtonDefaults.animatedShapes() จะทำให้การสลับข้อความเคลื่อนไหวเมื่อกด และ TextToggleButtonDefaults.variantAnimatedShapes() จะทำให้การสลับข้อความเคลื่อนไหวเมื่อกดและเลือก/ยกเลิกการเลือก

คอมโพเนนต์และเลย์เอาต์

คอมโพเนนต์และเลย์เอาต์ส่วนใหญ่จาก M2.5 พร้อมใช้งานใน M3 อย่างไรก็ตาม M2.5 ไม่มีคอมโพเนนต์และเลย์เอาต์บางอย่างของ M3 นอกจากนี้ คอมโพเนนต์ M3 บางรายการ ยังมีตัวแปรมากกว่าคอมโพเนนต์ที่เทียบเท่าใน M2.5

แม้ว่าคอมโพเนนต์บางอย่างจะต้องพิจารณาเป็นพิเศษ แต่เราขอแนะนำให้ใช้การแมปฟังก์ชันต่อไปนี้เป็นจุดเริ่มต้น

Material 2.5

Material 3

androidx.wear.compose.material.dialog.Alert

androidx.wear.compose.material3.AlertDialog

androidx.wear.compose.material.Button

androidx.wear.compose.material3.IconButton หรือ androidx.wear.compose.material3.TextButton

androidx.wear.compose.material.Card

androidx.wear.compose.material3.Card

androidx.wear.compose.material.TitleCard

androidx.wear.compose.material3.TitleCard

androidx.wear.compose.material.AppCard

androidx.wear.compose.material3.AppCard

androidx.wear.compose.material.Checkbox

ไม่มีฟีเจอร์ที่เทียบเท่ากับ M3 ให้ย้ายข้อมูลไปที่ androidx.wear.compose.material3.CheckboxButton หรือ androidx.wear.compose.material3.SplitCheckboxButton

androidx.wear.compose.material.Chip

androidx.wear.compose.material3.Button หรือ
androidx.wear.compose.material3.OutlinedButton หรือ
androidx.wear.compose.material3.FilledTonalButton หรือ
androidx.wear.compose.material3.ChildButton

androidx.wear.compose.material.CompactChip

androidx.wear.compose.material3.CompactButton

androidx.wear.compose.material.InlineSlider

androidx.wear.compose.material3.Slider

androidx.wear.compose.material.LocalContentAlpha

ถูกนำออกเนื่องจาก Text หรือ Icon ไม่ได้ใช้ใน Material 3

androidx.wear.compose.material.PositionIndicator

androidx.wear.compose.material3.ScrollIndicator

androidx.wear.compose.material.RadioButton

ไม่มีฟีเจอร์ที่เทียบเท่ากับ M3 ให้ย้ายข้อมูลไปที่ androidx.wear.compose.material3.RadioButton หรือ androidx.wear.compose.material3.SplitRadioButton

androidx.wear.compose.material.SwipeToRevealCard

androidx.wear.compose.material3.SwipeToReveal

androidx.wear.compose.material.SwipeToRevealChip

androidx.wear.compose.material3.SwipeToReveal

android.wear.compose.material.Scaffold

androidx.wear.compose material3.AppScaffold และ androidx.wear.compose.material3.ScreenScaffold

androidx.wear.compose.material.SplitToggleChip

ไม่มีฟีเจอร์ที่เทียบเท่า M3 ให้ย้ายข้อมูลไปที่ androidx.wear.compose.material3.SplitCheckboxButton, androidx.wear.compose.material3.SplitSwitchButton หรือ androidx.wear.compose.material3.SplitRadioButton

androidx.wear.compose.material.Switch

ไม่มีฟีเจอร์ที่เทียบเท่า M3 ให้ย้ายข้อมูลไปที่ androidx.wear.compose.material3.SwitchButton หรือ androidx.wear.compose.material3.SplitSwitchButton

androidx.wear.compose.material.ToggleButton

androidx.wear.compose.material3.IconToggleButton หรือ androidx.wear.compose.material3.TextToggleButton

androidx.wear.compose.material.ToggleChip

androidx.wear.compose.material3.CheckboxButton หรือ
androidx.wear.compose.material3.RadioButton หรือ
androidx.wear.compose.material3.SwitchButton

androidx.wear.compose.material.Vignette

นำออกเนื่องจากไม่ได้รวมอยู่ในดีไซน์ Material 3 Expressive สำหรับ Wear OS

รายการคอมโพเนนต์ Material 3 ทั้งหมดมีดังนี้

Material 3

คอมโพเนนต์ที่เทียบเท่า Material 2.5 (หากไม่ใช่คอมโพเนนต์ใหม่ใน M3)

androidx.wear.compose.material3.AlertDialog

androidx.wear.compose.material.dialog.Alert

androix.wear.compose.material3.AnimatedPage

ใหม่

androidx.wear.compose.material3.AnimatedText

ใหม่

androidx.wear.compose material3.AppScaffold

android.wear.compose.material.Scaffold (พร้อม androidx.wear.compose.material3.ScreenScaffold )

androidx.wear.compose.material3.Button

androidx.wear.compose.material.Chip

androidx.wear.compose.material3.ButtonGroup

ใหม่

androidx.wear.compose.material3.Card

androidx.wear.compose.material.Card

androidx.wear.compose.material3.CheckboxButton

androidx.wear.compose.material.ToggleChip ที่มีตัวควบคุมการเปิด/ปิดช่องทำเครื่องหมาย

androidx.wear.compose.material3.ChildButton

androidx.wear.compose.material.Chip (เฉพาะเมื่อไม่จำเป็นต้องมีพื้นหลัง)

androidx.wear.compose.material3.CircularProgressIndicator

androidx.wear.compose.material.CircularProgressIndicator

androidx.wear.compose.material3.CompactButton

androidx.wear.compose.material.CompactChip

androidx.wear.compose.material3.ConfirmationDialog

androidx.wear.compose.material.dialog.Confirmation

androidx.wear.compose.material3.curvedText

androidx.wear.compose.material.curvedText

androidx.wear.compose.material3.DatePicker

ใหม่

androidx.wear.compose.material3.Dialog

androidx.wear.compose.material.dialog.Dialog

androidx.wear.compose.material3.EdgeButton

ใหม่

androidx.wear.compose.material3.FadingExpandingLabel

ใหม่

androidx.wear.compose.material3.FilledTonalButton

androidx.wear.compose.material.Chip เมื่อต้องใช้พื้นหลังปุ่มสีโทน

androidx.wear.compose.material3.HorizontalPageIndicator

androidx.wear.compose.material.HorizontalPageIndicator

androidx.wear.compose.material3.HorizontalPagerScaffold

ใหม่

androidx.wear.compose.material3.Icon

androidx.wear.compose.material.Icon

androidx.wear.compose.material3.IconButton

androidx.wear.compose.material.Button

androidx.wear.compose.material3.IconToggleButton

androidx.wear.compose.material.ToggleButton

androidx.wear.compose.material3.LevelIndicator

ใหม่

androidx.wear.compose.material3.LinearProgressIndicator

ใหม่

androidx.wear.compose.material3.ListHeader

androidx.wear.compose.material.ListHeader

androidx.wear.compose.material3.ListSubHeader

ใหม่

androidx.wear.compose.material3.MaterialTheme

androidx.wear.compose.material.MaterialTheme

androidx.wear.compose.material3.OpenOnPhoneDialog

ใหม่

androidx.wear.compose.material3.Picker

androidx.wear.compose.material.Picker

androidx.wear.compose.material3.PickerGroup

androidx.wear.compose.material.PickerGroup

androix.wear.compose.material3.RadioButton

androidx.wear.compose.material.ToggleChip ที่มีตัวควบคุมการสลับปุ่มตัวเลือก

androidx.wear.compose.material3.ScreenScaffold

android.wear.compose.material.Scaffold (พร้อม androidx.wear.compose material3.AppScaffold)

androidx.wear.compose.material3.ScrollIndicator

androidx.wear.compose.material.PositionIndicator

androidx.wear.compose.material3.scrollaway

androidx.wear.compose.material.scrollaway

androidx.wear.compose.material3.SegmentedCircularProgressIndicator

ใหม่

androidx.wear.compose.material3.Slider

androidx.wear.compose.material.InlineSlider

androidx.wear.compose.material3.SplitRadioButton

androidx.wear.compose.material.SplitToggleChip

androidx.wear.compose.material3.SplitCheckboxButton

androidx.wear.compose.material.SplitToggleChip

androidx.wear.compose.material3.SplitSwitchButton

androidx.wear.compose.material.SplitToggleChip

androidx.wear.compose.material3.Stepper

androidx.wear.compose.material.Stepper

androidx.wear.compose.material3.SwipeToDismissBox

androidx.wear.compose.material.SwipeToDismissBox

androidx.wear.compose.material3.SwipeToReveal

androidx.wear.compose.material.SwipeToRevealCard และ androidx.wear.compose.material.SwipeToRevealChip

androidx.wear.compose.material3.SwitchButton

androidx.wear.compose.material.ToggleChip พร้อมตัวควบคุมการสลับสวิตช์

androidx.wear.compose.material3.Text

androidx.wear.compose.material.Text

androidx.wear.compose.material3.TextButton

androidx.wear.compose.material.Button

androidx.wear.compose.material3.TextToggleButton

androidx.wear.compose.material.ToggleButton

androidx.wear.compose.material3.TimeText

androidx.wear.compose.material.TimeText

androidx.wear.compose.material3.VerticalPagerScaffold

ใหม่

และสุดท้ายคือรายการคอมโพเนนต์ที่เกี่ยวข้องบางส่วนจาก Wear Compose Foundation ไลบรารีเวอร์ชัน 1.5.0-beta01

Wear Compose Foundation 1.5.0-เบต้า

androidx.wear.compose.foundation.hierarchicalFocusGroup

ใช้เพื่อใส่คำอธิบายประกอบที่ Composable ในแอปพลิเคชัน เพื่อติดตามส่วนที่ใช้งานอยู่ของ Composition และประสานงานโฟกัส

androidx.compose.foundation.pager.HorizontalPager

เครื่องมือเปลี่ยนหน้าแบบเลื่อนแนวนอนที่สร้างขึ้นบนคอมโพเนนต์ Compose Foundation พร้อมการเพิ่มประสิทธิภาพเฉพาะ Wear เพื่อปรับปรุงประสิทธิภาพและการปฏิบัติตามหลักเกณฑ์ของ Wear OS

androidx.compose.foundation.pager.VerticalPager

เครื่องมือเปลี่ยนหน้าแบบเลื่อนแนวตั้งสร้างขึ้นจากคอมโพเนนต์ Compose Foundation พร้อมการเพิ่มประสิทธิภาพเฉพาะ Wear เพื่อปรับปรุงประสิทธิภาพและปฏิบัติตามหลักเกณฑ์ของ Wear OS

androidx.wear.foundation.lazy.TransformingLazyColumn

ใช้แทน ScalingLazyColumn เพื่อเพิ่มเอฟเฟกต์การเปลี่ยนภาพการเลื่อนไปยังแต่ละรายการได้

ปุ่ม

ปุ่มใน M3 แตกต่างจาก M2.5 ชิป M2.5 ถูกแทนที่ด้วย ปุ่ม การติดตั้งใช้งาน Button จะระบุค่าเริ่มต้นสำหรับ Text maxLines และ textAlign คุณจะลบล้างค่าเริ่มต้นเหล่านั้นได้ในองค์ประกอบ Text

M2.5

import androidx.wear.compose.material.Chip

//M2.5 Buttons
Chip(...)
CompactChip(...)
Button(...)

M3

import androidx.wear.compose.material3.Button

//M3 Buttons
Button(...)
CompactButton(...)
IconButton(...)
TextButton(...)

นอกจากนี้ M3 ยังมีปุ่มรูปแบบใหม่ด้วย ดูได้ที่ภาพรวมเอกสารอ้างอิง API ของ Compose Material 3

M3 ขอแนะนำปุ่มใหม่ EdgeButton EdgeButton มีให้เลือก 4 ขนาด ได้แก่ เล็กมาก เล็ก กลาง และใหญ่ EdgeButton การติดตั้งใช้งาน ระบุค่าเริ่มต้นสำหรับ maxLines ตามขนาดที่ปรับแต่งได้

หากคุณใช้ TransformingLazyColumn และ ScalingLazyColumn ให้ส่ง EdgeButton ไปยัง ScreenScaffold เพื่อให้ EdgeButton เปลี่ยนรูปร่างตามการเลื่อน ดูโค้ดด้านล่างเพื่อดูวิธีใช้ EdgeButton กับ ScreenScaffold และ TransformingLazyColumn

import androidx.wear.compose.material3.EdgeButton
import androidx.wear.compose.material3.ScreenScaffold

ScreenScaffold(
   scrollState = state,
   contentPadding = contentPadding,
   edgeButton = {
      EdgeButton(...)
   }
){ contentPadding ->
   TransformingLazyColumn(state = state, contentPadding = contentPadding,){
   // additional code here
   }
}

Scaffold

Scaffold ใน M3 แตกต่างจาก M2.5 ใน M3 AppScaffold และ ScreenScaffoldที่สามารถคอมโพสใหม่ได้จะแทนที่ Scaffold AppScaffold และ ScreenScaffold จะวางโครงสร้างของหน้าจอและประสานงานการเปลี่ยนของ คอมโพเนนต์ ScrollIndicator และ TimeText

AppScaffold ช่วยให้องค์ประกอบหน้าจอแบบคงที่ เช่น TimeText ยังคงมองเห็นได้ ในระหว่างการเปลี่ยนหน้าในแอป เช่น การปัดเพื่อปิด ​​โดยจะแสดงช่องสำหรับ เนื้อหาแอปพลิเคชันหลัก ซึ่งโดยปกติแล้วจะจัดเตรียมโดยคอมโพเนนต์การนำทาง เช่น SwipeDismissableNavHost

คุณประกาศ AppScaffold หนึ่งรายการสำหรับกิจกรรมและใช้ ScreenScaffold สำหรับแต่ละหน้าจอ

M2.5

import androidx.wear.compose.material.Scaffold

Scaffold {...}

M3

import androidx.wear.compose.material3.AppScaffold
import androidx.wear.compose.material3.ScreenScaffold

AppScaffold {
   // Define the navigation hierarchy within the AppScaffold,
   // such as using SwipeDismissableNavHost.
   SwipeDismissableNavHost(...) {
      composable("home") {
         HomeScreen()
      }
      //other screens
   }
}
fun HomeScreen() {
    val scrollState = rememberScrollState()
    ScreenScaffold(scrollState = scrollState) {
    //rest of the screen code
    }
}

หากคุณใช้ HorizontalPager กับ HorizontalPagerIndicator คุณสามารถย้ายข้อมูลไปยัง HorizontalPagerScaffold ได้ HorizontalPagerScaffold จะวางอยู่ ภายใน AppScaffold AppScaffold และ HorizontalPagerScaffold จะวางโครงสร้างของเพจเจอร์และประสานงานการเปลี่ยนของคอมโพเนนต์ HorizontalPageIndicator และ TimeText

HorizontalPagerScaffold จะแสดง HorizontalPageIndicator ที่ กึ่งกลางด้านท้ายของหน้าจอโดยค่าเริ่มต้น และประสานงานการแสดง/ซ่อน TimeText และ HorizontalPageIndicator ตามว่ามีการแบ่งหน้า Pager หรือไม่ ซึ่งกำหนดโดย PagerState

นอกจากนี้ยังมีAnimatedPageคอมโพเนนต์ใหม่ ซึ่งจะเคลื่อนไหวหน้าภายใน Pager ด้วยเอฟเฟกต์การปรับขนาดและเอฟเฟกต์ Scrim ตามตำแหน่งของหน้า

import androidx.wear.compose.material3.AppScaffold
import androidx.wear.compose.material3.HorizontalPagerScaffold
import androidx.wear.compose.material3.ScreenScaffold
import androidx.wear.compose.foundation.pager.HorizontalPager
import androidx.wear.compose.foundation.pager.rememberPagerState

AppScaffold {
    val pagerState = rememberPagerState(pageCount = { 10 })

    HorizontalPagerScaffold(pagerState = pagerState) {
       HorizontalPager(
          state = pagerState,
        ) { page ->
            AnimatedPage(pageIndex = page, pagerState = pagerState) {
                ScreenScaffold {

   }
}

สุดท้าย M3 ขอแนะนำ VerticalPagerScaffold ซึ่งมีรูปแบบเดียวกับ HorizontalPagerScaffold

import androidx.wear.compose.material3.AppScaffold
import androidx.wear.compose.material3.HorizontalPagerScaffold
import androidx.wear.compose.material3.ScreenScaffold
import androidx.wear.compose.foundation.pager.VerticalPager
import androidx.wear.compose.foundation.pager.rememberPagerState

AppScaffold {
   val pagerState = rememberPagerState(pageCount = { 10 })

   VerticalPagerScaffold(pagerState = pagerState) {
      VerticalPager(
         state = pagerState
      ) { page ->
             AnimatedPage(pageIndex = page, pagerState = pagerState){
                ScreenScaffold {
        
   }
}

ตัวยึดตำแหน่ง

API มีการเปลี่ยนแปลงบางอย่างระหว่าง M2.5 กับ M3 Placeholder.PlaceholderDefaults ตอนนี้มีตัวแก้ไข 2 รายการดังนี้

  • Modifier.placeholder ซึ่งวาดแทนเนื้อหาที่ยังไม่ได้โหลด
  • เอฟเฟกต์การสั่นไหวของตัวยึดตำแหน่ง Modifier.placeholderShimmer ซึ่งให้เอฟเฟกต์การสั่นไหวของตัวยึดตำแหน่งที่ทำงานในลูปภาพเคลื่อนไหว ขณะรอให้ระบบโหลดข้อมูล

ดูการเปลี่ยนแปลงเพิ่มเติมในคอมโพเนนต์ Placeholder ที่ด้านล่าง

M2.5

M3

PlaceholderState.startPlaceholderAnimation

ถูกนำออกแล้ว

PlaceholderState.placeholderProgression

ถูกนำออกแล้ว

PlaceholderState.isShowContent

เปลี่ยนชื่อเป็น !PlaceholderState.isVisible แล้ว

PlaceholderState.isWipeOff

ถูกนำออกแล้ว

PlaceholderDefaults.painterWithPlaceholderOverlayBackgroundBrush

ถูกนำออกแล้ว

PlaceholderDefaults.placeholderBackgroundBrush

ถูกนำออกแล้ว

PlaceholderDefaults.placeholderChipColors

ถูกนำออกแล้ว

SwipeDismissableNavHost

SwipeDismissableNavHost เป็นส่วนหนึ่งของ wear.compose.navigation เมื่อใช้คอมโพเนนต์นี้กับ M3, MaterialTheme ของ M3 จะอัปเดต LocalSwipeToDismissBackgroundScrimColor และ LocalSwipeToDismissContentScrimColor

TransformingLazyColumn

TransformingLazyColumn เป็นส่วนหนึ่งของ wear.compose.lazy.foundation และเพิ่ม การรองรับการปรับขนาดและภาพเคลื่อนไหวที่เปลี่ยนรูปร่างในรายการระหว่างการเลื่อน เพื่อปรับปรุงประสบการณ์ของผู้ใช้

เช่นเดียวกับ ScalingLazyColumn ซึ่งมี rememberTransformingLazyColumnState() สำหรับสร้าง TransformingLazyColumnState ที่ระบบจดจำได้ในทุกองค์ประกอบ

หากต้องการเพิ่มภาพเคลื่อนไหวการปรับขนาดและการมอร์ฟ ให้เพิ่มสิ่งต่อไปนี้ลงในรายการแต่ละรายการ

  • Modifier.transformedHeight ซึ่งช่วยให้คุณคำนวณความสูงที่แปลงแล้ว ของรายการโดยใช้ TransformationSpec ได้ คุณสามารถใช้ rememberTransformationSpec() ได้ เว้นแต่คุณต้องการปรับแต่งเพิ่มเติม
  • A SurfaceTransformation
import androidx.wear.compose.material3.AppScaffold
import androidx.wear.compose.material3.ScreenScaffold
import androidx.wear.compose.material3.SurfaceTransformation
import androidx.wear.compose.material3.lazy.rememberTransformationSpec
import androidx.wear.compose.material3.lazy.transformedHeight
import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn

val state = rememberTransformingLazyColumnState()
val transformationSpec = rememberTransformationSpec()
AppScaffold {
   ScreenScaffold(state) { contentPadding ->
      TransformingLazyColumn(state = state, contentPadding = contentPadding) {
         items(count = 50) {
            Button(
               onClick = {},
               modifier =
                        Modifier.fillMaxWidth().transformedHeight(this, transformationSpec),
                        transformation = SurfaceTransformation(transformationSpec),
                    ) {
                        Text("Item $it")
                    }
                }
            }
        }
    }

ลิงก์ที่มีประโยชน์

ดูข้อมูลเพิ่มเติมเกี่ยวกับการย้ายข้อมูลจาก M2.5 ไปยัง M3 ใน Compose ได้จาก แหล่งข้อมูลเพิ่มเติมต่อไปนี้

ตัวอย่าง

เอกสารอ้างอิง API และซอร์สโค้ด

การออกแบบ