简体   繁体   English

Flutter:如何在给定的宽度内换行?

[英]Flutter: How to wrap text inside a given width?

What I am trying to achieve is like how we see text messages like the image below.我想要实现的就像我们如何看到如下图所示的文本消息。 But when I put a long text I am getting the screen overflow error.但是当我输入长文本时,我收到屏幕溢出错误。 I tried using softwrap: true with all the overflow: TextOverflow.xxxx options, but doesn't seem to do what I am looking for.我尝试使用softwrap: true和所有overflow: TextOverflow.xxxx选项,但似乎没有做我想要的。 For reference I put my code here.作为参考,我把我的代码放在这里。

Container(
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10)),
                    child: Container(
                      padding:
                          EdgeInsets.all(5),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Text(
                            'Add a very long text the goes a few lines',
                            style: TextStyle(fontSize: 16, color: Colors.black),
                            softWrap: true,
                            overflow: TextOverflow.visible,
                          ),
                          Text(
                            '3:08',
                            style:
                                TextStyle(fontSize: 12, color: Colors.black38),
                          ),
                        ],
                      ),
                    ),
                  ),

在此处输入图像描述

You can wrap Text with Flexible widget to fix the overflow.您可以使用Flexible的小部件包装Text以修复溢出。

Container(
  decoration: BoxDecoration(
      color: Colors.white, borderRadius: BorderRadius.circular(10)),
  child: Container(
    padding: EdgeInsets.all(5),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Flexible(
          child: Text(
            'Add a very long text the goes a few lines',
            style: TextStyle(fontSize: 16, color: Colors.black),
            softWrap: true,
            overflow: TextOverflow.visible,
          ),
        ),
        Text(
          '3:08',
          style: TextStyle(fontSize: 12, color: Colors.black38),
        ),
      ],
    ),
  ),
),

Just wrap the text around an Expanded widget.只需将文本包裹在Expanded小部件周围。 The text is going to wrap inside the available width.文本将在可用宽度内换行。

Take a look at the screenshots below and the live demo on DartPad .看看下面的截图和DartPad 上的现场演示

Thinner稀释剂 Regular常规的 Wider更宽的
在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

Here's the code:这是代码:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(10)),
        child: Container(
          padding: const EdgeInsets.all(5),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: const [
              Expanded(
                child: Text(
                  'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus egestas dui elit, et porttitor lorem sodales ut. Maecenas quis lacinia arcu, a ultricies enim.',
                  style: TextStyle(fontSize: 16, color: Colors.black),
                  softWrap: true,
                  overflow: TextOverflow.visible,
                ),
              ),
              Text(
                '3:08',
                style: TextStyle(fontSize: 12, color: Colors.black38),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM