简体   繁体   English

Flutter Android 模拟器中的文本溢出,在 IOS 模拟器中工作正常

[英]Flutter text overflow in Android emulator, works fine in IOS emulator

I have a Column widget that has 2 children: a SizedBox and a Container with a Text widget.我有一个 Column 小部件,它有 2 个孩子:一个 SizedBox 和一个带有 Text 小部件的容器。 I get an overflow in iOS emulator, but it renders without problems on Android.我在 iOS 模拟器中出现溢出,但它在 Android 上呈现没有问题。 Should I be using MediaQuery to customize the SizedBox height based on type of device?我应该使用 MediaQuery 根据设备类型自定义 SizedBox 高度吗? (NOTE: The Scaffold/Stack code is included merely to provide more context, and likely has no bearing on the overflow issue, unless I'm mistaken!) (注意:包含 Scaffold/Stack 代码只是为了提供更多上下文,并且可能与溢出问题无关,除非我弄错了!)

  Widget build(BuildContext context) {
  return new Scaffold(
    body: new Swiper(
    itemBuilder: (BuildContext context, int index) {
     return GestureDetector(
      onVerticalDragStart: (details) {
            /* do something */
            ));
      },
      child: Stack(
        children: [
          ShaderMask(
            shaderCallback: (Rect bounds) {
              return LinearGradient(
                      begin: Alignment.bottomCenter,
                      end: Alignment.center,
                      colors: [Colors.black12, Colors.white])
                  .createShader(bounds);
            },
            child: Container(
              padding:
                  new EdgeInsets.only(left: 16.0, bottom: 8.0, right: 16.0),
              decoration: new BoxDecoration(
                image: new DecorationImage(
                  image: new AssetImage(tipList[index].imagePath),
                  fit: BoxFit.cover,
                ),
              ),
            ),
          ),
          Column(
            children: [
              new SizedBox(height: 700),
              Container(
                padding: new EdgeInsets.only(left: 16.0, bottom: 8.0, right: 16.0),
                child: new Text(tipList[index].description,
                    style: Theme.of(context).textTheme.headline4),
              ),
            ],
          ),
        ],
      ),

iOS 与 Android 模拟器的屏幕截图

Use FittedBox with BoxFit.scaleDown and I update your code.将 FittedBox 与 BoxFit.scaleDown 一起使用,我会更新您的代码。

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Scaffold(
      body: Swiper(
          itemCount: tipList.length,
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onVerticalDragStart: (details) {},
              child: Stack(
                children: [
                  ShaderMask(
                    shaderCallback: (Rect bounds) {
                      return const LinearGradient(
                              begin: Alignment.bottomCenter,
                              end: Alignment.center,
                              colors: [Colors.black12, Colors.white])
                          .createShader(bounds);
                    },
                    child: Container(
                      padding: const EdgeInsets.only(
                          left: 16.0, bottom: 8.0, right: 16.0),
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage(tipList[index].imagePath),
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                  ),
                  Column(
                    children: [
                      const SizedBox(height: 700),
                      Padding(
                        padding: const EdgeInsets.only(
                            left: 16.0, bottom: 8.0, right: 16.0),
                        child: FittedBox(
                          fit: BoxFit.scaleDown,
                          child: Text(tipList[index].description,
                              style: Theme.of(context).textTheme.headline4),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            );
          }),
    );
  }

place your text with Positioned:使用定位放置文本:

  Widget build(BuildContext context) {
  return new Scaffold(
    body: new Swiper(
    itemBuilder: (BuildContext context, int index) {
     return GestureDetector(
      onVerticalDragStart: (details) {
            /* do something */
            ));
      },
      child: Stack(
        children: [
          ShaderMask(
            shaderCallback: (Rect bounds) {
              return LinearGradient(
                      begin: Alignment.bottomCenter,
                      end: Alignment.center,
                      colors: [Colors.black12, Colors.white])
                  .createShader(bounds);
            },
            child: Container(
              padding:
                  new EdgeInsets.only(left: 16.0, bottom: 8.0, right: 16.0),
              decoration: new BoxDecoration(
                image: new DecorationImage(
                  image: new AssetImage(tipList[index].imagePath),
                  fit: BoxFit.cover,
                ),
              ),
            ),
          ),
          Positioned(
            bottom: 0.0,
            child:
              Container(
                padding: new EdgeInsets.only(left: 16.0, bottom: 8.0, right: 16.0),
                child: new Text(tipList[index].description,
                    style: Theme.of(context).textTheme.headline4),
              ),
          ),
        ],
      ),

It is because height of these 2 screens are different.这是因为这两个屏幕的高度不同。 In order to make it UI responsive, you should avoid using SizedBox or Containers with exact height and use Expanded/Spacer widget.为了使其 UI 响应,您应该避免使用具有精确高度的 SizedBox 或容器,并使用 Expanded/Spacer 小部件。 Both of this widgets will take maximum available space on that specific device screen.这两个小部件都将占用该特定设备屏幕上的最大可用空间。 So I would try first with this:所以我会先试试这个:


   Column(
            children: [
              Spacer(), //or Expanded(child:Container()),
              Container(
                padding: new EdgeInsets.only(left: 16.0, bottom: 8.0, right: 16.0),
                child: new Text(tipList[index].description,
                    style: Theme.of(context).textTheme.headline4),
              ),
            ],
          ),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 Flutter 应用程序在 IOS 模拟器上看起来很好,但在 Android 上却溢出了? - Why does my Flutter app look fine on the IOS emulator but overflow on Android? 无法在 iOS 仿真器或物理设备上构建 Flutter 应用程序 - 虽然在 Android 上工作正常 - Cannot build Flutter app on iOS emulator or physical device - works fine on Android though Flutter 电话身份验证在模拟器上工作正常,但在真实设备上不工作(Android) - Flutter Phone authentication works fine on emulator but not working on real device (Android) Android 模拟器不处理 http get 请求,但 iOS 模拟器工作正常 - Android emulator not processing http get request, but iOS simulator works fine Android EditText字段可在模拟器上正常运行,但不能在设备上运行 - Android EditText field works fine on emulator, but not device Android应用程序可以在模拟器中正常运行,但不能在真实设备中运行? - Android application works fine in emulator but not in real device? Android应用程序可以在模拟器上正常运行,但不能在真实设备上运行 - Android Application works fine on emulator but not on a real device 在flutter中从image_picker包中打开相机会导致真实设备上的应用程序崩溃,但在模拟器(android)中运行良好 - opening camera from image_picker package in flutter crashes app on real device but works fine in emulator (android) 仅在 Android 上,React Native 天才聊天在模拟器和设备上崩溃,在 iOS 中工作正常 - React Native gifted chat crashing on both emulator and device only on Android, works fine in iOS Flutter 应用程序:IOS 模拟器未连接到 Android Studio - Flutter app : IOS emulator not connecting to Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM