Utils

1. Translations

Bản dịch được lưu trữ dưới dạng Map. Custom bản dịch bằng cách tạo một class kế thừa Translations.

import 'package:get/get.dart';

class Messages extends Translations {
  @override
  Map<String, Map<String, String>> get keys => {
        'en_US': {
          'hello': 'Hello World',
        },
        'de_DE': {
          'hello': 'Hallo Welt',
        }
      };
}

Sử dụng bản dịch dễ dàng bằng cách thêm .tr vào sau chuỗi.

Text('title'.tr);

2. Locales

Khai báo translations và locale ở GetMaterialApp

return GetMaterialApp(
    translations: Messages(), // Custom class
    locale: Locale('en', 'US'), // locale hiện tại
    fallbackLocale: Locale('en', 'UK'), // locate default

Gọi lệnh Get.updateLocale(locale) để cập nhật locale. Bản dịch sẽ tự động thay đổi theo locale mới.

var locale = Locale('en', 'US');
Get.updateLocale(locale);

Ngoài ra có thể lấy locale hiện tại của máy làm ngôn ngữ khởi tạo như vị dụ dưới:

import 'dart:ui' as ui;

return GetMaterialApp(
    locale: ui.window.locale,
);

3. Thay đổi Theme

Get.changeTheme(ThemeData.light());
Get.changeTheme(Get.isDarkMode? ThemeData.light(): ThemeData.dark());

4. Một số hàm khác

Xem Platform trong khi running

GetPlatform.isAndroid
GetPlatform.isIOS
GetPlatform.isMacOS
GetPlatform.isWindows
GetPlatform.isLinux
GetPlatform.isFuchsia

Xem loại thiết bị

GetPlatform.isMobile
GetPlatform.isDesktop
GetPlatform.isWeb

Lấy chiều dài/ rộng

Get.height
Get.width

Và nhiều hơn thế nữa

// Gives you the power to define half the screen, a third of it and so on.
// Useful for responsive applications.
// param dividedBy (double) optional - default: 1
// param reducedBy (double) optional - default: 0
context.heightTransformer()
context.widthTransformer()

/// Similar to MediaQuery.of(context).size
context.mediaQuerySize()

/// Similar to MediaQuery.of(context).padding
context.mediaQueryPadding()

/// Similar to MediaQuery.of(context).viewPadding
context.mediaQueryViewPadding()

/// Similar to MediaQuery.of(context).viewInsets;
context.mediaQueryViewInsets()

/// Similar to MediaQuery.of(context).orientation;
context.orientation()

/// Check if device is on landscape mode
context.isLandscape()

/// Check if device is on portrait mode
context.isPortrait()

/// Similar to MediaQuery.of(context).devicePixelRatio;
context.devicePixelRatio()

/// Similar to MediaQuery.of(context).textScaleFactor;
context.textScaleFactor()

/// Get the shortestSide from screen
context.mediaQueryShortestSide()

/// True if width be larger than 800
context.showNavbar()

/// True if the shortestSide is smaller than 600p
context.isPhone()

/// True if the shortestSide is largest than 600p
context.isSmallTablet()

/// True if the shortestSide is largest than 720p
context.isLargeTablet()

/// True if the current device is Tablet
context.isTablet()

/// Returns a value<T> according to the screen size
/// can give value for:
/// watch: if the shortestSide is smaller than 300
/// mobile: if the shortestSide is smaller than 600
/// tablet: if the shortestSide is smaller than 1200
/// desktop: if width is largest than 1200
context.responsiveValue<T>()

Last updated

Was this helpful?