答案 0 :(得分:2)
够了
WidgetsBinding.instance.window.locale.countryCode
答案 1 :(得分:0)
您可以使用以下软件包devicelocale
在您的pupspec.yaml中添加依赖项
dependencies:
devicelocale: ^0.2.1
将其导入所需的班级
import 'package:devicelocale/devicelocale.dart';
然后,您可以使用以下命令将当前设备本地化
String locale = await Devicelocale.currentLocale;
答案 2 :(得分:0)
您只需要编写以下代码。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Locale myLocale = Localizations.localeOf(context);
print(myLocale.languageCode); //You can get locale here...
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
key: Key('counterKey'),
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
答案 3 :(得分:0)
只需使用内置的Locale
类dart即可获取国家/地区代码和语言。
Locale myLocale = Localizations.localeOf(context);
要获取国家/地区代码:
print(myLocale.countryCode);
获取语言代码:
print(myLocale.languageCode);
答案 4 :(得分:0)
您可以使用系统区域设置。
final List<Locale> systemLocales = WidgetsBinding.instance.window.locales;
String isoCountryCode = systemLocales.first.countryCode;
答案 5 :(得分:0)
您可以使用 country_codes 包:
安装:
dependencies:
country_codes: ^2.0.1
用法:
await CountryCodes.init(); // Optionally, you may provide a `Locale` to get countrie's localizadName
final Locale deviceLocale = CountryCodes.getDeviceLocale();
print(deviceLocale.languageCode); // Displays en
print(deviceLocale.countryCode); // Displays US
final CountryDetails details = CountryCodes.detailsForLocale();
print(details.alpha2Code); // Displays alpha2Code, for example US.
print(details.dialCode); // Displays the dial code, for example +1.
print(details.name); // Displays the extended name, for example United States.
print(details.localizedName); // Displays the extended name based on device's language (or other, if provided on init)