首页 > 解决方案 > 在容器中包装文本而不在 Flutter 中使用固定宽度

问题描述

我正在尝试在 Flutter 中创建一个基本的聊天应用程序,并且我想在简单的容器中显示对话,这将使其长度适应里面的文本。一切正常,直到文本不适合容器的长度,当我收到溢出错误时。

在此处输入图像描述

我正在使用的代码是这个

Widget _buildMessage(Message message) {
    return Row(children: <Widget>[
      message.author == username ? Expanded(child: Container()) : Container(),
      Container(
          padding: EdgeInsets.all(8.0),
          margin: EdgeInsets.all(4.0),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(8.0))),
          child: Row(
            children: <Widget>[
              Text(
                message.text,
              ),
              SizedBox(
                width: 8.0,
              ),
              Padding(
                padding: EdgeInsets.only(top: 6.0),
                child: Text(
                  message.time,
                  style: TextStyle(fontSize: 10.0, color: Colors.grey),
                ),
              ),
              SizedBox(
                width: 8.0,
              ),
              Padding(
                padding: EdgeInsets.only(top: 6.0),
                child: Text(
                  message.author,
                  style: TextStyle(fontSize: 10.0, color: Colors.grey),
                ),
              ),
            ],
          )),
      message.author != username ? Expanded(child: Container()) : Container(),
    ]);
  }

我在一行中使用一行,以便我可以根据作者将这种对齐方式向右或向左对齐。如果我在输入中键入带有多行的内容,则文本会正确呈现,容器会根据需要垂直扩展。问题是当我只是输入超出容器的宽度时。

我可以通过用容器和固定的包装文本来解决这个问题,但我不希望这样,因为我希望宽度是动态的并适应文本。

我见过人们建议使用灵活或扩展的其他问题,但我不知道该怎么做。

任何想法,将不胜感激。

标签: flutter

解决方案


我试图编辑您的代码,这是我所做的:

在此处输入图像描述

return Row(
  mainAxisAlignment: message.author == username ? MainAxisAlignment.end : MainAxisAlignment.start,
  //this will determine if the message should be displayed left or right
  children: [
    Flexible(
    //Wrapping the container with flexible widget
      child: Container(
        padding: EdgeInsets.all(8.0),
        margin: EdgeInsets.all(4.0),
        decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(8.0))),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Flexible(
            //We only want to wrap the text message with flexible widget
              child: Container(
                child: Text(
                  message.text,
                  )
                )
              ),    
              SizedBox(
                width: 8.0,
              ),
              Padding(
                padding: EdgeInsets.only(top: 6.0),
                child: Text(
                  message.time,
                  style: TextStyle(fontSize: 10.0, color: Colors.grey),
                  ),
                ),
              SizedBox(
                width: 8.0,
              ),
              Padding(
                padding: EdgeInsets.only(top: 6.0),
                child: Text(
                  message.author,
                  style: TextStyle(fontSize: 10.0, color: Colors.grey),
                  ),
                ),
              ],
            )),

           )

        ]
        );

这是我的虚拟代码的完整版本:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

List<String> username = ['foo', 'bar', 'foo', 'bar', 'foo'];
List<String> messages = ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bb', 'cccccccccccccccccccccccccccccc', 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'ee'];
List<String> time = ['131', '6454', '54564', '54546', '88888'];
List<String> author = ['Jesus', 'Joseph', 'Mary', 'John', 'Cardo'];

@override
Widget build(BuildContext context){

return Scaffold(
  body: Container(
    color: Colors.blue[200],
    child: ListView.builder(
      itemCount: 5, 
      itemBuilder: (_, int index){

        return  Row(
          mainAxisAlignment: username[index] == "foo" ? MainAxisAlignment.end : MainAxisAlignment.start,
          children: [
            Flexible(
              child: Container(
                padding: EdgeInsets.all(8.0),
                margin: EdgeInsets.all(4.0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(8.0))),
                child: Row(

                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[

                    Flexible(
                      child: Container(
                        child: Text(
                          messages[index],
                        ),
                      )
                    ),

                    SizedBox(
                      width: 8.0,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 6.0),
                      child: Text(
                        time[index],
                        style: TextStyle(fontSize: 10.0, color: Colors.grey),
                      ),
                    ),
                    SizedBox(
                      width: 8.0,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 6.0),
                      child: Text(
                        author[index],
                        style: TextStyle(fontSize: 10.0, color: Colors.grey),
                      ),
                    ),
                  ],
                )),

              )

          ]
          );

      }
      ) 
      )
    );
  }
}

推荐阅读