Flutter在iOS上如何在键盘上方显示Toast提示信息

6
我正在使用fluttertoast 7.0.4包在我的应用程序中显示Toast,但在iOS上,当键盘打开时,Toast不会显示出来"它实际上会出现在键盘后面",这是我的代码。
  Fluttertoast.showToast(
        msg: message,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: AppColors.strongOrange,
        textColor: Colors.white,
        fontSize: 16.0
);

有没有办法在Flutter中更改z-index并将其设置为大于键盘的z-index?
2个回答

6

在iOS上,无法将Fluttertoast显示在键盘上方。

有两个选项:

1.将Toast的重力改为居中

   Fluttertoast.showToast(
          msg: message,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          backgroundColor: Colors.red,
          textColor: Colors.white,
          fontSize: 16.0
      );
  1. Hide keyboard when showing toast at the bottom.

           FocusManager.instance.primaryFocus.unfocus();
    

不是最好的选择,但是一个不错的技巧。非常感谢 :D - Mad World

2

另一种选择是在键盘之后显示Toast。

重要的是使用

MediaQuery.of(context).viewInsets.bottom

在使用"Toast with BuildContext (All Platforms)"选项显示Fluttertoast时(更多信息请参阅https://pub.dev/packages/fluttertoast
这将获取键盘的大小,然后您可以手动设置偏移量,以免被键盘遮挡。

伪代码如下:

import 'package:fluttertoast/fluttertoast.dart';

FToast()
  ..init(context)
  ..showToast(
    child: <YOUR WIDGET>,
    positionedToastBuilder: (context, child) {
      return Positioned(
        bottom: MediaQuery.of(context).viewInsets.bottom,
        top: 16.0,
        left: 16.0,
        child: child,
      );
    },
  );

谢谢,这是个好主意。 - Abdulmalek Dery

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接