【发布时间】:2018-12-10 13:21:18
【问题描述】:
我已经学习了如何通过 StatelessWidget 使用 i18n 进行颤振练习, 但仍然无法通过 StatefulWidget 工作。
我可以简单地替换下面的代码
title: new Text(S.of(context).title)
带有一个 const 字符串,例如:
title: const Text("A Test Title");
所以我认为其他一切都应该没问题。唯一的问题是 i18n 不起作用。
有人可以帮我,“如何通过 StatefulWidget 在 Flutter 上使用 i18n?”
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'generated/i18n.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key key, this.title}) : super(key: key);
final String title;
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
BuildContext c;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
var tiles = new List<Widget>();
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text(S.of(context).title), // Here is the problem
),
body: new Stack(
children: <Widget>[
new Container(),
new ListView(
children: tiles,
)
],
),
),
localizationsDelegates: [S.delegate],
supportedLocales: S.delegate.supportedLocales,
localeResolutionCallback: S.delegate.resolution(
fallback: new Locale("en", "")
),
);
}
}
【问题讨论】:
-
使用 widget.title
标签: internationalization dart flutter statefulwidget