flutter - Flutter 对话框 RTL
问题描述
我做了以下
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
Future<void> main() async {
runApp(
MaterialApp(
locale: Locale('he'),
localizationsDelegates: [
// ... app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('he', ''), // Heberew
const Locale('en', ''), // English
],
theme: ThemeData.light(),
home: new Main(
// Pass the appropriate camera to the TakePictureScreen widget.
),
),
);
}
class Main extends StatefulWidget {
@override
_MainState createState() => _MainState();
}
class _MainState extends State<Main> {
Widget _buildDialog(BuildContext context) {
print("_buildDialog");
return MaterialApp(
// locale: Locale('he'),
// localizationsDelegates: [
// // ... app-specific localization delegate[s] here
// GlobalMaterialLocalizations.delegate,
// GlobalWidgetsLocalizations.delegate,
// GlobalCupertinoLocalizations.delegate,
// ],
// supportedLocales: [
// const Locale('he', ''), // Heberew
// const Locale('en', ''), // English
// ],
title: "Test",
home: Scaffold(
body: AlertDialog(
content: Row(children: <Widget>[
Text('1'),
Text('2'),
]),
actions: <Widget>[
FlatButton(
child: const Text('CLOSE'),
onPressed: () {
Navigator.pop(context, false);
},
),
FlatButton(
child: const Text('SHOW'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
),
),
);
}
void _showPushDialog() {
print("DIALOG");
showDialog<bool>(
context: context,
builder: (_) => _buildDialog(context),
).then((bool shouldNavigate) {
if (shouldNavigate == true) {
_navigateToPushDetail();
}
});
}
void _navigateToPushDetail() {
print("TODO: Goto...");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: Locale('he'),
localizationsDelegates: [
// ... app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('he', ''), // Heberew
const Locale('en', ''), // English
],
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
body: new Material(
child: Row(children: <Widget>[
Text('1'),
Text('2'),
RaisedButton(
onPressed: () {
print("pushed?");
_showPushDialog();
},
child: Text("press me"),
)
]),
),
),
);
}
}
运行应用程序时,它按预期在 RTL 中显示 21。但是当点击按钮时它显示 12(而不是像父页面中的 21)
如果我取消注释 _buildDialog (37-47) 中的行,对话框将显示在 RTL 中,所以这将解决我的问题。
我的问题是:
1.有没有更好的方法来做到这一点?
2.注意我还需要用MaterialApp和Scaffold包围AlertDialog,不清楚为什么需要?
3. 为什么我们需要一个新的背景?
4. 为什么我们需要新的本地化定义?
5. 我错过了什么吗?
解决方案
MaterialApp
小部件提供了Navigator
, Theme
,Directionality
等功能。它主要用作根小部件。您不必在每个页面中都使用它。
Scaffold
小部件对于创建新页面很有用。它提供了AppBar
, Drawer
,BottomNavigationBar
等功能。由于它试图填满设备屏幕,所以不要环绕它AlertDialog
。
我做了一些改变。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
Future<void> main() async {
runApp(
MaterialApp(
locale: Locale('he'),
localizationsDelegates: [
// ... app-specific localization delegate[s] here
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('he', ''), // Heberew
const Locale('en', ''), // English
],
theme: ThemeData.light(),
home: new Main(
// Pass the appropriate camera to the TakePictureScreen widget.
),
),
);
}
class Main extends StatefulWidget {
@override
_MainState createState() => _MainState();
}
class _MainState extends State<Main> {
Widget _buildDialog(BuildContext context) {
return AlertDialog(
content: Row(children: <Widget>[
Text('1'),
Text('2'),
]),
actions: <Widget>[
FlatButton(
child: const Text('CLOSE'),
onPressed: () {
Navigator.pop(context, false);
},
),
FlatButton(
child: const Text('SHOW'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
}
void _showPushDialog() {
print("DIALOG");
showDialog<bool>(
context: context,
builder: (_) => _buildDialog(context),
).then((bool shouldNavigate) {
if (shouldNavigate == true) {
_navigateToPushDetail();
}
});
}
void _navigateToPushDetail() {
print("TODO: Goto...");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
body: new Material(
child: Row(children: <Widget>[
Text('1'),
Text('2'),
RaisedButton(
onPressed: () {
print("pushed?");
_showPushDialog();
},
child: Text("press me"),
)
]),
),
);
}
}
推荐阅读
- python - 如何更改导入包的代码
- objective-c - 通过代码为应用程序分配辅助功能访问权限
- ios - 长按手势多次调用
- reactjs - 在动态生成的情况下,React 不会完全挂载节点
- caffe - 如何在 Windows 中使用 matcaffe?
- javascript - 停止复选框改变状态直到函数执行
- laravel - 将 Vue.js 包含到 Laravel 中
- java - 数组中负整数元素的最小总和,可以选择跳过元素但没有两个连续的
- c++ - 如何更有效地使用模板在 C++ 中实现具有绑定检查的二维数组?
- django - Django:即使我的表单是有效的,打印cleaned_data时我的一些字段也是空的