© Khmer Angkor Academy - sophearithput168

មូលដ្ឋានគ្រឹះ Dart

ភាសា Dart - មូលដ្ឋាននៃ Flutter

Dart គឺជាភាសាកម្មវិធីដែល Flutter ប្រើ។ វាត្រូវបានបង្កើតឡើងដោយ Google នៅឆ្នាំ 2011 ជា modern, object-oriented language ដែលមានលក្ខណៈប្រហាក់ប្រហែល Java, JavaScript, និង C#។

📚 ហេតុអ្វីជ្រើសរើស Dart?

  • Optimized for UI: ត្រូវបានរចនាសម្រាប់បង្កើត user interfaces
  • Fast on All Platforms: Compiles ទៅជា native code (AOT) និង supports JIT
  • Productive Development: Hot reload, modern syntax, async support
  • Type Safe: Null safety ការពារ null pointer errors
  • Easy to Learn: Syntax ស្រដៀងនឹង JavaScript និង Java

🔧 Compilation Modes

1. JIT (Just-In-Time) - Development

  • Compile code នៅពេល runtime
  • អនុញ្ញាត Hot Reload និង debugging
  • Faster development cycle

2. AOT (Ahead-Of-Time) - Production

  • Compile code មុនពេលចែកចាយ
  • Native machine code មាន performance ល្អបំផុត
  • Smaller app size, faster startup time

🎯 មុខងារសំខាន់ Dart

  • Type Safe: ការពារ error ពេល compile
  • Object-Oriented: គ្រប់យ៉ាងជា object
  • Null Safety: ការពារ null pointer errors
  • Hot Reload: មើលលទ្ធផលភ្លាមៗ
  • Async/Await: សម្រាប់ asynchronous operations

📝 Hello World

void main() {
  print('សួស្តី Flutter!');
}

🔢 Data Types

1. Numbers (int, double)

void main() {
  // Integer
  int age = 20;
  int year = 2024;
  
  // Double
  double price = 15.99;
  double pi = 3.14159;
  
  // Arithmetic
  int sum = 10 + 5;        // 15
  int diff = 10 - 5;       // 5
  int product = 10 * 5;    // 50
  double quotient = 10 / 3;// 3.333...
  int remainder = 10 % 3;  // 1
  
  print('អាយុ: $age');
  print('តម្លៃ: \$' + price.toString());
}

2. Strings

void main() {
  // String declaration
  String name = 'សុខា';
  String greeting = "សួស្តី";
  
  // Multi-line string
  String address = '''
    ផ្លូវ៖ ១២៣
    សង្កាត់៖ បឹងកេងកង
    ខ័ណ្ឌ៖ ចំការមន
  ''';
  
  // String interpolation
  String message = 'ឈ្មោះ៖ $name';
  String full = 'អាយុ 25 ឆ្នាំ';
  
  // String concatenation
  String fullName = 'Sok' + ' ' + 'Dara';
  
  // String methods
  print(name.length);           // 4
  print(name.toUpperCase());    // សុខា
  print(name.contains('ខា'));   // true
  print(name.replaceAll('ខា', 'ភា')); // សុភា
}

3. Booleans

void main() {
  bool isStudent = true;
  bool isPassed = false;
  
  // Comparison
  bool isAdult = age >= 18;
  bool isEqual = name == 'សុខា';
  
  // Logical operators
  bool canVote = isAdult && isStudent;      // AND
  bool needsHelp = isYoung || isOld;        // OR
  bool isNotReady = !isPassed;              // NOT
  
  print('សិស្ស៖ $isStudent');
}

4. Lists (Arrays)

void main() {
  // List declaration
  List<String> fruits = ['ផ្លែប៉ោម', 'ផ្លែចេក', 'ស្វាយ'];
  List<int> numbers = [1, 2, 3, 4, 5];
  
  // Access elements
  print(fruits[0]);        // ផ្លែប៉ោម
  print(fruits.first);     // ផ្លែប៉ោម
  print(fruits.last);      // ស្វាយ
  
  // Add elements
  fruits.add('ទ្រាប់');
  fruits.addAll(['ម្នាស់', 'ល្ហុង']);
  
  // Remove elements
  fruits.remove('ផ្លែចេក');
  fruits.removeAt(0);
  
  // List properties
  print(fruits.length);    // 5
  print(fruits.isEmpty);   // false
  
  // Loop through list
  for (var fruit in fruits) {
    print(fruit);
  }
}

5. Maps (Dictionary)

void main() {
  // Map declaration
  Map<String, dynamic> student = {
    'name': 'សុខា',
    'age': 20,
    'grade': 'A',
    'isPassed': true
  };
  
  // Access values
  print(student['name']);     // សុខា
  print(student['age']);      // 20
  
  // Add/Update
  student['phone'] = '012345678';
  student['age'] = 21;
  
  // Remove
  student.remove('grade');
  
  // Map properties
  print(student.length);      // 4
  print(student.keys);        // (name, age, isPassed, phone)
  print(student.values);      // (សុខា, 21, true, 012345678)
  
  // Loop through map
  student.forEach((key, value) {
    print('$key: $value');
  });
}

📦 Variables

var, final, const

void main() {
  // var - type inferred, can reassign
  var name = 'សុខា';  // String
  name = 'ដារា';      // OK
  
  // final - runtime constant, cannot reassign
  final city = 'ភ្នំពេញ';
  // city = 'សៀមរាប';  // ERROR!
  
  // const - compile-time constant
  const pi = 3.14159;
  // pi = 3.14;         // ERROR!
  
  // Type annotation
  String country = 'កម្ពុជា';
  int population = 16000000;
}

🔄 Control Flow

If-Else

void main() {
  int score = 85;
  
  if (score >= 90) {
    print('A');
  } else if (score >= 80) {
    print('B');
  } else if (score >= 70) {
    print('C');
  } else {
    print('F');
  }
  
  // Ternary operator
  String result = score >= 50 ? 'ជាប់' : 'ធ្លាក់';
}

Switch-Case

void main() {
  String day = 'Monday';
  
  switch (day) {
    case 'Monday':
      print('ចន្ទ');
      break;
    case 'Tuesday':
      print('អង្គារ');
      break;
    case 'Wednesday':
      print('ពុធ');
      break;
    default:
      print('ថ្ងៃផ្សេងទៀត');
  }
}

🔁 Loops

For Loop

void main() {
  // Traditional for
  for (int i = 1; i <= 5; i++) {
    print('លេខ $i');
  }
  
  // For-in loop
  List<String> names = ['សុខា', 'ដារា', 'សុភា'];
  for (var name in names) {
    print(name);
  }
  
  // forEach method
  names.forEach((name) {
    print(name);
  });
}

While Loop

void main() {
  int count = 0;
  
  while (count < 5) {
    print('Count: $count');
    count++;
  }
  
  // Do-while
  int num = 0;
  do {
    print('Number: $num');
    num++;
  } while (num < 3);
}

⚡ Functions

// Basic function
void greet(String name) {
  print('សួស្តី $name');
}

// Function with return
int add(int a, int b) {
  return a + b;
}

// Arrow function
int multiply(int a, int b) => a * b;

// Optional parameters
void introduce(String name, [int? age]) {
  print('ឈ្មោះ៖ ' + name + ', អាយុ៖ ' + (age != null ? age.toString() : 'មិនដឹង'));
}

// Named parameters
void createUser({required String name, int age = 18}) {
  print('User: $name, Age: $age');
}

void main() {
  greet('សុខា');
  print(add(5, 3));
  print(multiply(4, 2));
  
  introduce('ដារា');
  introduce('សុភា', 25);
  
  createUser(name: 'វណ្ណា', age: 22);
  createUser(name: 'រដ្ឋា');
}

💡 ជំនួយ: Dart គឺជាភាសាងាយស្រួល។ បើអ្នកធ្លាប់សរសេរ JavaScript ឬ Java អ្នកនឹងជួបភាពស្រដៀងគ្នាច្រើន។