import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart' show SynchronousFuture; import 'package:flutter_localizations/flutter_localizations.dart';
void main() => runApp(new MyApp());
class ZDLocalizations { ZDLocalizations(this.locale);
final Locale locale;
static ZDLocalizations of(BuildContext context) { return Localizations.of<ZDLocalizations>(context, ZDLocalizations); }
static Map<String, Map<String, String>> _localizedValues = { 'en': { 'title': 'List View', }, 'zh': { 'title': '列表视图', }, };
String get title { return _localizedValues[locale.languageCode]['title']; } }
class DemoLocalizationsDelegate extends LocalizationsDelegate<ZDLocalizations> { const DemoLocalizationsDelegate();
@override bool isSupported(Locale locale) => ['en', 'zh'].contains(locale.languageCode);
@override Future<ZDLocalizations> load(Locale locale) { return SynchronousFuture<ZDLocalizations>(ZDLocalizations(locale)); }
@override bool shouldReload(DemoLocalizationsDelegate old) => false; }
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( debugShowCheckedModeBanner: false, home: new CreateHome(), theme: new ThemeData( primaryColor: Colors.orange, ), localizationsDelegates: [ const DemoLocalizationsDelegate(), GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ], supportedLocales: [const Locale('en', ''), const Locale('zh', '')], ); } }
class CreateHome extends StatefulWidget { @override CreateHomeState createState() => CreateHomeState(); }
class CreateHomeState extends State<CreateHome> { int _currentIndex = 0; final _bodyOptions = [ Text('主页'), Text('商城'), Text('消息'), ];
void backOnPressed() {}
void menuOnPressed() {}
void onTabBarItemTapped(int idx) { setState(() { _currentIndex = idx; }); }
@override Widget build(BuildContext context) { return Scaffold( appBar: _createAppBar(), body: Center(child: _bodyOptions.elementAt(_currentIndex)), bottomNavigationBar: _createTabBar(), ); }
Widget _createAppBar() { return new AppBar( brightness: Brightness.dark, elevation: 0.5, iconTheme: IconThemeData(color: Colors.white), title: Text( ZDLocalizations.of(context).title, style: TextStyle(color: Colors.white), ), actions: [ IconButton( icon: Icon(Icons.menu), onPressed: menuOnPressed, ), ], leading: IconButton( icon: Icon( Icons.arrow_back_ios, ), onPressed: backOnPressed, ), ); }
Widget _createTabBar() { return new BottomNavigationBar( fixedColor: Colors.blue, backgroundColor: Colors.orange, currentIndex: _currentIndex, onTap: onTabBarItemTapped, items: [ BottomNavigationBarItem(icon: Icon(Icons.home), label: '主页'), BottomNavigationBarItem(icon: Icon(Icons.shop), label: '商城'), BottomNavigationBarItem(icon: Icon(Icons.message), label: '消息') ], ); } }
|