Thuta Learning
React Native
BasicMobile Developmentအလယ်အလတ်

Layout with Flexbox

React Nativeသင်ခန်းစာ 620 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Layout with Flexbox ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
  • Accessibility, failure နဲ့ lifecycle cases စမ်းနိုင်ရန်
  • Maintainable, testable နဲ့ performant React Native code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Layout with Flexbox ကို ရိုးရိုးလေးနားလည်မယ်

React Native မှာ View တိုင်းရဲ့ default flexDirection က 'column' ပါ (web CSS ရဲ့ 'row' default နဲ့ ဆန့်ကျင်ဘက်) — child component တွေက default အနေနဲ့ အပေါ်ကနေအောက် အစီစီ arrange ဖြစ်ပါတယ်။ flexDirection: 'row' ပြောင်းရင် ဘေးချင်းကပ် arrange ဖြစ်ပါတယ်။ justifyContent က main axis ပေါ်မှာ spacing ချုပ်ကိုင်ပြီး (flex-start, center, space-between), alignItems က cross axis ပေါ်မှာ alignment ချုပ်ကိုင်ပါတယ် (flex-start, center, stretch)။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Web CSS ရဲ့ default flexDirection: row ကို ယူဆပြီး React Native (column default) မှာ layout မှားခြင်း

ဖြစ်လေ့ရှိတဲ့အမှား

Fixed width/height ကိုပဲ သုံးပြီး flex: 1 ကို လျစ်လျူရှုခြင်း — screen size မတူတဲ့ device မှာ layout ပျက်နိုင်

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Layout with Flexbox ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။

Mental Model
  1. JavaScript state
  2. Native component + layout
  3. Android / iOS UI

Layout with Flexbox ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

javascript
const styles = StyleSheet.create({
  header: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    padding: 16,
  },
  content: {
    flex: 1,           // remaining space ကို ယူ
    justifyContent: 'center',
    alignItems: 'center',
  },
});

Layout with Flexbox ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. JavaScript stateconst styles = StyleSheet.create({

    React Native မှာ View တိုင်းရဲ့ default flexDirection က 'column' ပါ (web CSS ရဲ့ 'row' default နဲ့ ဆန့်ကျင်ဘက်) — child component တွေက default အနေနဲ့ အပေါ်ကနေအောက် အစီစီ arrange ဖြစ်ပါတယ်။ flexDirection: 'row' ပြောင်းရင် ဘေးချင်းကပ် arrange ဖြစ်ပါတယ်။ justifyContent က main axis ပေါ်မှာ spacing ချုပ်ကိုင်ပြီး (flex-start, center, space-between), alignItems က cross axis ပေါ်မှာ alignment ချုပ်ကိုင်ပါတယ် (flex-start, center, stretch)။

  2. Native component + layout},

    Layout with Flexbox ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။

  3. Android / iOS UI});

    Layout with Flexbox example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Execution Flow
  1. JavaScript state
  2. Native component + layout
  3. Android / iOS UI

Success

Layout with Flexbox ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

Failure

Web CSS ရဲ့ default flexDirection: row ကို ယူဆပြီး React Native (column default) မှာ layout မှားခြင်း

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

React Native မှာ View တိုင်းရဲ့ default flexDirection က 'column' ပါ (web CSS ရဲ့ 'row' default နဲ့ ဆန့်ကျင်ဘက်) — child component တွေက default အနေနဲ့ အပေါ်ကနေအောက် အစီစီ arrange ဖြစ်ပါတယ်။ flexDirection: 'row' ပြောင်းရင် ဘေးချင်းကပ် arrange ဖြစ်ပါတယ်။ justifyContent က main axis ပေါ်မှာ spacing ချုပ်ကိုင်ပြီး (flex-start, center, space-between), alignItems က cross axis ပေါ်မှာ alignment ချုပ်ကိုင်ပါတယ် (flex-start, center, stretch)။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Header bar (logo ဘယ်ဘက်, menu icon ညာဘက်) ကို ဆောက်ချင်ရင် flexDirection: 'row', justifyContent: 'space-between' ကို View style ထဲ ထည့်ရုံပါ — child ၂ ခုက automatic ကြားခြားထွက်သွားပါလိမ့်မယ်။ flex: 1 ကို child component ရဲ့ style ထဲ ထည့်ရင် available space ကို ယူပါတယ် — screen size အမျိုးမျိုးမှာ responsive ဖြစ်စေချင်ရင် fixed width/height အစား flex ကို သုံးတာက ပိုကောင်းပါတယ်။

Code နမူနာ

javascript
const styles = StyleSheet.create({
  header: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    padding: 16,
  },
  content: {
    flex: 1,           // remaining space ကို ယူ
    justifyContent: 'center',
    alignItems: 'center',
  },
});
You should see
Header bar တစ်ခုကို flexDirection: row နဲ့ ဆောက်ပြီး logo/icon ကို ဘေးချင်းကပ် arrange လုပ်နိုင်မည်။

၅ မိနစ် စမ်းကြည့်

flexDirection: 'row' ပါတဲ့ View တစ်ခုထဲမှာ box ၃ ခု ထည့်ပြီး justifyContent ကို 'flex-start', 'center', 'space-between' သုံးမျိုးနဲ့ စမ်းကြည့်ပါ။

သတိလေးတစ်ချက်

flex: 1 ကို parent container မှာ height သတ်မှတ်မထားရင် (root View က flex: 1 ရှိမှ) အလုပ်မဖြစ်နိုင်ပါဘူး — parent-child flex chain ကို သတိထားပါ။

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Layout with Flexbox example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Layout with Flexbox example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
javascript
const styles = StyleSheet.create({
  header: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    padding: 16,
  },
  content: {
    flex: 1,           // remaining space ကို ယူ
    justifyContent: 'center',
    alignItems: 'center',
  },
});

ရလဒ်: Layout with Flexbox ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Layout with Flexbox example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Quick Quiz 1 / 3

Layout with Flexbox ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • JavaScript state → Native component + layout → Android / iOS UI
  • Layout with Flexbox ကို screen တစ်ခုအတွက် style syntax အဖြစ်သာမမြင်ဘဲ Yoga constraints, pixel density, font scaling, safe areas, keyboard, touch target, accessibility tree နဲ့ iOS/Android behavior ကွာခြားမှုတို့ပါသော native UI contract အဖြစ်နားလည်ပါ။
  • Web CSS ရဲ့ default flexDirection: row ကို ယူဆပြီး React Native (column default) မှာ layout မှားခြင်း
  • Layout with Flexbox example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

React Native Documentation — React Native

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Web CSS ရဲ့ default flexDirection: row ကို ယူဆပြီး React Native (column default) မှာ layout မှားခြင်း
  • Fixed width/height ကိုပဲ သုံးပြီး flex: 1 ကို လျစ်လျူရှုခြင်း — screen size မတူတဲ့ device မှာ layout ပျက်နိုင်
  • Simulator တစ်ခု၏ happy path မှန်ရုံဖြင့် real devices, release builds, permissions, accessibility, offline နဲ့ lifecycle paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Layout with Flexbox example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Header bar တစ်ခုကို flexDirection: row နဲ့ ဆောက်ပြီး logo/icon ကို ဘေးချင်းကပ် arrange လုပ်နိုင်မည်။

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Layout with Flexbox | Thuta Learning