flutter - 在 Flutter 中,如何在较大的屏幕尺寸上修复 PopupMenuItem Widget 中的截断文本?

标签 flutter dart

在下面的屏幕截图中,存在一个 UI 崩溃,其中 PopupMenuItem 文本在较大的屏幕尺寸下被截断,但在较窄的屏幕上完全可见。

解决此错误的最佳方法是什么?

运行下面的代码,至少我希望与屏幕截图显示的相反。

据我所知,我没有指定任何宽度限制。

代码设置了独特的颜色背景以指示哪个小部件占用的空间。

enter image description here

import 'package:flutter/material.dart';

main() {
  runApp(Main2DemoApp());
}

class Main2DemoApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Main2DemoScreen(),
    );
  }
}

class Main2DemoScreen extends StatefulWidget {
  @override
  _Main2DemoState createState() => _Main2DemoState();
}

class _Main2DemoState extends State<Main2DemoScreen> {
  static const int EMAIL_CONVERSATION = 11;

  AppScale _scale;

  @override
  Widget build(BuildContext context) {
    _scale = AppScale(context);

    Widget fullHeaderRow = Row(
      children: <Widget>[
        Container(
          color: Colors.purple[200],
          child: getPopupMenuButton(),
        ),
      ],
    );

    fullHeaderRow = Container(
      color: Colors.amber[200],
      child: fullHeaderRow,
    );

    return Scaffold(
      appBar: AppBar(title: Text('PopupMenuButton Demo')),
      body: fullHeaderRow,
    );
  }

  Widget getPopupMenuButton() {
    Widget emailPopUpItem = PopupMenuItem(
      textStyle: TextStyle(
        color: Colors.white,
        fontSize: _scale.popupMenuItem,
      ),
      child: Row(
        children: [
          Padding(
              padding: EdgeInsets.only(right: 10),
              child: Icon(
                Icons.email,
                color: Colors.white,
                size: _scale.popupMenuItem,
              )),
          Text('Send Email Message Now')
        ],
      ),
      value: EMAIL_CONVERSATION,
    );

    List<PopupMenuEntry<dynamic>> menuWidgets = [emailPopUpItem];
    double iconSize = _scale.popupMenuButton;

    PopupMenuButton popupMenuButton = PopupMenuButton(
      padding: EdgeInsets.all(0),
      color: Colors.lightGreen[700],
      offset: Offset(40, 20),
      shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(5)),
      icon: Icon(
        Icons.more_vert,
        color: Colors.white,
        size: iconSize,
      ),
      onSelected: (newValue) {
        print('newValue: $newValue');
      },
      itemBuilder: (context) => menuWidgets,
    );

    return popupMenuButton;
  }
}

class AppScale {
  BuildContext _ctxt;

  AppScale(this._ctxt);

  double get popupMenuButton => scaledWidth(.08);
  double get popupMenuItem => scaledHeight(.025);

  double scaledWidth(double widthScale) {
    return MediaQuery.of(_ctxt).size.width * widthScale;
  }

  double scaledHeight(double heightScale) {
    return MediaQuery.of(_ctxt).size.height * heightScale;
  }
}

最佳答案

弹出菜单的最大和最小宽度限制在这里定义:popup_menu.dart:554而且好像是不可配置的

const double _kMenuMaxWidth = 5.0 * _kMenuWidthStep;
const double _kMenuMinWidth = 2.0 * _kMenuWidthStep;
...
const double _kMenuWidthStep = 56.0;
...
class _PopupMenu<T> extends StatelessWidget {
...
    final Widget child = ConstrainedBox(
      constraints: const BoxConstraints(
        minWidth: _kMenuMinWidth,
        maxWidth: _kMenuMaxWidth,
      )
...

关于flutter - 在 Flutter 中,如何在较大的屏幕尺寸上修复 PopupMenuItem Widget 中的截断文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63570199/

相关文章:

flutter - 来自原生android的Channel Invokemethod不调用flutter方法

dart - 如何在我的 Flutter 应用程序中从 JWT 获取声明

ios - 未找到模块 'apple_sign_in'

json - Dartlang:如何从json获取键和值?

mongodb - 蒙大拿 flutter 的应用程序连接错误

android - 为什么我的 Flutter 应用程序在退出时会下拉到屏幕底部?

flutter - @freezed copyWith 缺少密封类

dart - 如何在flutter中记录http请求

windows - 是否有任何解决方案可以使用带有 Flutter 桌面的笔记本电脑相机拍照?

stream - Dart中的StreamTransformer被跳过了吗?