Flutter对话框RTL

时间:2019-12-21 14:20:57

标签: flutter

我做了以下

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。 请注意,我还需要将AlertDialog与MaterialApp和Scaffold一起使用, 不清楚为什么有必要?

3。 为什么我们需要一个新的环境?

4。 为什么我们需要新的本地化定义?

5。 我想念什么吗?

1 个答案:

答案 0 :(得分:1)

MaterialApp小部件提供诸如NavigatorThemeDirectionality等功能。它主要用作根小部件。您不必在每个页面中都使用它。

Scaffold小部件对于创建新页面很有用。它具有AppBarDrawerBottomNavigationBar等功能。由于它试图填充设备屏幕,因此请勿将其包裹在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"),
          )
        ]),
      ),
    );
  }
}