import 'package:base_app/blocs/application_bloc.dart'; import 'package:base_app/blocs/bloc_provider.dart'; import 'package:base_app/common/common.dart'; import 'package:base_app/models/common/language_model.dart'; import 'package:base_app/res/index.dart'; import 'package:fluintl/fluintl.dart'; import 'package:flustars/flustars.dart'; import 'package:flutter/material.dart'; /// 选择语言页面 class LanguagePage extends StatefulWidget { @override State<StatefulWidget> createState() { return _LanguagePageState(); } } class _LanguagePageState extends State<LanguagePage> { // 多语言列表 List<LanguageModel> _list = new List(); // 当前语言 LanguageModel _currentLanguage; @override void initState() { super.initState(); // 语言列表 _list.add(LanguageModel(Ids.languageAuto, '', '')); _list.add(LanguageModel(Ids.languageZH, 'zh', 'CH')); _list.add(LanguageModel(Ids.languageTW, 'zh', 'TW')); _list.add(LanguageModel(Ids.languageHK, 'zh', 'HK')); _list.add(LanguageModel(Ids.languageEN, 'en', 'US')); // 当前语言,否则默认列表第一个(跟随系统) _currentLanguage = SpUtil.getObj(Constant.keyLanguage, (v) => LanguageModel.fromJson(v)); if (ObjectUtil.isEmpty(_currentLanguage)) { _currentLanguage = _list[0]; } _updateData(); } void _updateData() { LogUtil.e('currentLanguage: ' + _currentLanguage.toString()); String language = _currentLanguage.countryCode; for (int i = 0, length = _list.length; i < length; i++) { _list[i].isSelected = (_list[i].countryCode == language); } } @override Widget build(BuildContext context) { // application bloc final ApplicationBloc bloc = BlocProvider.of<ApplicationBloc>(context); return new Scaffold( appBar: new AppBar( title: new Text( IntlUtil.getString(context, Ids.titleLanguage), // style: new TextStyle(fontSize: 16.0), ), actions: [ // 保存按钮 new Padding( padding: EdgeInsets.all(12.0), child: new SizedBox( width: 64.0, child: new RaisedButton( textColor: Colors.white, color: Colours.app_main, child: Text( IntlUtil.getString(context, Ids.save), style: new TextStyle(fontSize: 14.0), ), onPressed: () { // 保存当前语言,并通知给整个app SpUtil.putObject( Constant.keyLanguage, ObjectUtil.isEmpty(_currentLanguage.languageCode) ? null : _currentLanguage); bloc.sendAppEvent(Constant.type_sys_update); Navigator.pop(context); }, ), ), ), ], ), // 语言列表 body: new ListView.builder( itemCount: _list.length, itemBuilder: (BuildContext context, int index) { LanguageModel model = _list[index]; return new ListTile( title: new Text( (model.titleId == Ids.languageAuto ? IntlUtil.getString(context, model.titleId) : IntlUtil.getString(context, model.titleId, languageCode: 'zh', countryCode: 'CH')), style: new TextStyle(fontSize: 13.0), ), trailing: new Radio( value: true, groupValue: model.isSelected == true, // activeColor: Colors.indigoAccent, onChanged: (value) { setState(() { _currentLanguage = model; _updateData(); }); }), onTap: () { setState(() { _currentLanguage = model; _updateData(); }); }, ); }), ); } }