Wrap text in container without using a fixed width in Flutter

前端 未结 1 826
一向
一向 2020-12-03 17:55

I\'m trying to create a basic chat application in Flutter and I want to display the conversation in simple containers which will adapt its length to the text inside. Everyth

相关标签:
1条回答
  • 2020-12-03 18:07

    I tried to edit your code and here is what I've made:

    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),
                      ),
                    ),
                  ],
                )),
    
               )
    
            ]
            );
    

    Here is the full version of my dummy code:

    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', 'ffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffdffffffffd', '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),
                          ),
                        ),
                      ],
                    )),
    
                  )
    
              ]
              );
    
          }
          ) 
          )
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题