native_locale 0.0.3 copy "native_locale: ^0.0.3" to clipboard
native_locale: ^0.0.3 copied to clipboard

PlatformAndroid

A flutter plugin for getting and changing the native platform locale.

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_locale/native_locale.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _nativeLocale = 'Unknown';
  final _nativeLocalePlugin = NativeLocale();

  @override
  void initState() {
    super.initState();
    initLocaleState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initLocaleState() async {
    String nativeLocale;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      nativeLocale = await NativeLocale().getNativeLocale() ?? 'Unknown locale';
      await NativeLocale().setNativeLocale(const Locale('en')) ??
          'Unknown locale';
    } on PlatformException {
      nativeLocale = 'Failed to get native locale.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _nativeLocale = nativeLocale;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('current native locale is: $_nativeLocale\n'),
        ),
      ),
    );
  }
}
4
likes
130
pub points
56%
popularity

Publisher

verified publisheramrahmd.com

A flutter plugin for getting and changing the native platform locale.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on native_locale