【问题标题】:Wrapping long text into a container, causes overflow Flutter将长文本包装到容器中,导致溢出 Flutter
【发布时间】:2020-03-31 03:32:24
【问题描述】:

我正在为我的应用程序创建一个通知中心,它包含一些用于标题和正文的长字符串,它总是会导致溢出,我尝试使用扩展小部件,以及以下这些堆栈溢出问题:Question 1Question 2 但我无法将它应用到我的代码中,这就是它现在的样子:

我从我的 firebase cloud firestore 获取文本,并使用列表视图输出文本。

标题文本是这样的:“Allen Indefenzo 已撤销同意您访问他/她的数据”
正文是这样的:“他/她已删除您的所有访问权限,您将无法再查看/更改他的数据,再次访问要求用户给您他/她再次特殊代码。”

这是我的代码:

Container(
        color: Colors.white,
        child: StreamBuilder(
          stream: db
              .collection('users')
              .document(userData.userID)
              .collection('notifications')
              .snapshots(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            if (snapshot.data.documents.length != 0) {
              return ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (BuildContext context, int index) {
                  return Container(
                    child: Row(
                      children: <Widget>[
                        Container(
                          height: 150,
                          width: 100,
                          child: Image(
                            image: NetworkImage(
                                snapshot.data.documents[index]['dpURL']), fit: BoxFit.contain,
                          ),
                        ),
                        Column(
                          children: <Widget>[
                            Container(
                              height: 50,
                              width: MediaQuery.of(context).size.width * 0.8,
                              child:
                                  Text(snapshot.data.documents[index]['title']),
                            ),
                            Container(
                              height: 50,
                              width: MediaQuery.of(context).size.width * 0.8,
                              child:
                                  Text(snapshot.data.documents[index]['body']),
                            ),
                          ],
                        ),
                      ],
                    ),
                  );
                },
              );
            }
          },
        ),
      ),

感谢您提供任何帮助,如果您能解释一下,那就太棒了!非常感谢!

【问题讨论】:

    标签: flutter dart overflow


    【解决方案1】:

    尝试将您的Column 包裹在Expanded 中,

     Container(
                  child: Row(
                    children: <Widget>[
                      Container(
                        height: 150,
                        width: 100,
                        child: Image(
                          image: NetworkImage(
                              'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQLBtBU3nUOK7osT41ZsQhP4VBV7fd9euqXvXVKQH0Q7bl4txeD'),
                          fit: BoxFit.contain,
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: <Widget>[
                            Text(
                                'Reallly looooooooooooooooooooooooooooooong textttttttttttttttttttttttttttttttttttttttttttttttt'),
                            Text(
                                'Reallly looooooooooooooooooooooooooooooong textttttttttttttttttttttttttttttttttttttttttttttttt'),
                          ],
                        ),
                      ),
                    ],
                  ),
                );
    

    输出:


    另外如果你想限制Text使用的行数,

      Text(
    'Reallly looooooooooooooooooooooooooooooongtextttttttttttttttttttttttttttttttttttttttttttttttt',
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
                            ),
    

    输出:

    【讨论】:

      【解决方案2】:

      只需将您的列包装在展开的小部件中

      Expanded(child: 
                     Column(
                                children: <Widget>[
                                  Container(
                                    height: 50,
                                    width: MediaQuery.of(context).size.width * 0.8,
                                    child:
                                        Text(snapshot.data.documents[index]['title']),
                                  ),
                                  Container(
                                    height: 50,
                                    width: MediaQuery.of(context).size.width * 0.8,
                                    child:
                                        Text(snapshot.data.documents[index]['body']),
                                  ),
                                ],
            ),
      ),
      

      当列或行小部件具有 2 个或更多子小部件时,当您想要填充所有剩余空间时,扩展小部件非常有用。因此,将子组件包裹在展开的小部件中会相应地填满剩余空间。

      【讨论】:

        【解决方案3】:
        return Container(child: Row(
                                    children: <Widget>[
                                      Container(
                                        height: 150,
                                        width: 100,
                                        child: Image(
                                          image: NetworkImage(snapshot.data.documents[index]['dpURL']), fit: BoxFit.contain,
                                        ),
                                      ),
                                     new Expanded(
                                        child: Column(
                                          children: <Widget>[
                                            Padding(
                                              padding: const EdgeInsets.all(8.0),
                                              child: TitleText(
                                                text:snapshot.data.documents[index]['title'] ,
                                                maxLine: 5,
                                              ),
                                            ),
                                            Container(
                                              height: 50,
                                              width: MediaQuery.of(context).size.width * 0.8,
                                              child: TitleText(
                                                  text:snapshot.data.documents[index]['body']),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ],
                                  ),
                                );
        

        【讨论】:

          猜你喜欢
          • 2017-05-08
          • 2022-01-18
          • 1970-01-01
          • 1970-01-01
          • 2019-03-18
          • 1970-01-01
          • 1970-01-01
          • 2021-11-05
          • 1970-01-01
          相关资源
          最近更新 更多