Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振换行文本而不是溢出_Flutter_Flutter Layout - Fatal编程技术网

Flutter 颤振换行文本而不是溢出

Flutter 颤振换行文本而不是溢出,flutter,flutter-layout,Flutter,Flutter Layout,在flatter中创建带有长字符串的文本小部件时,当直接放入列中时,它会包装文本。但是,当它位于列-行-列内部时,文本会溢出屏幕的一侧 如何在列中换行文本? 这种差异的原因是什么?在我看来,上一列的任何子项都具有相同的宽度似乎是合乎逻辑的?为什么宽度是无限的 根据其他答案,我试着将文本放在扩展的、灵活的、容器和FittedBox中,但这会导致我不理解的新错误 例如: MaterialApp( title: 'Text overflow', home: Scaffold( appB

在flatter中创建带有长字符串的文本小部件时,当直接放入列中时,它会包装文本。但是,当它位于列-行-列内部时,文本会溢出屏幕的一侧

如何在列中换行文本? 这种差异的原因是什么?在我看来,上一列的任何子项都具有相同的宽度似乎是合乎逻辑的?为什么宽度是无限的

根据其他答案,我试着将文本放在扩展的、灵活的、容器和FittedBox中,但这会导致我不理解的新错误

例如:

MaterialApp(
  title: 'Text overflow',
  home: Scaffold(
    appBar: AppBar(title: Text("MyApp"),),
    body: Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            // The long text inside this column overflows. Remove the row and column above this comment and the text wraps.
            Column(
              children: <Widget>[
                Text("Short text"),
                Text("Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. ")
              ],
            ),
          ],
        ),
      ],
    ),
  ), 
)
MaterialApp(
标题:“文本溢出”,
家:脚手架(
appBar:appBar(标题:文本(“MyApp”),),
正文:专栏(
儿童:[
划船(
儿童:[
//此列中的长文本溢出。请删除此注释上方的行和列,并将文本换行。
纵队(
儿童:[
文本(“短文本”),
文本(“非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。”)
],
),
],
),
],
),
), 
)

您需要使用-
扩展的
灵活的
小部件包装最后一列

这样,
就可以占用文本所需的可用空间

body: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              // The long text inside this column overflows. Remove the row and column above this comment and the text wraps.
              Expanded(
                child: Column(
                  children: <Widget>[
                    Text("Short text"),
                    Text(
                        "Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. ")
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
body:Column(
儿童:[
划船(
儿童:[
//此列中的长文本溢出。请删除此注释上方的行和列,并将文本换行。
扩大(
子:列(
儿童:[
文本(“短文本”),
正文(
“非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。”)
],
),
),
],
),
],
),

和列是
Flex
小部件,如果没有足够的空间,则不会滚动。颤振会引发溢出错误

如果出现这种情况,可以使用
扩展的
灵活的
小部件包装长文本

在中未明确说明,但除了扩展以填充主轴中的可用空间外,
扩展
灵活
沿横轴方向缠绕

长话短说 循序渐进的方法可能有助于理解问题

首先,考虑这种情况:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Text overflow',
      home: Scaffold(
        appBar: AppBar(
          title: Text("MyApp"),
        ),
        body: Column(
          children: List.generate(100, (idx) => Text("Short text"))
        ),
      ),
    );
  }
}
这是一个在垂直方向溢出的列小部件,如颤振所明确报告的:

I/flutter ( 8390): The following message was thrown during layout:
I/flutter ( 8390): A RenderFlex overflowed by 997 pixels on the bottom.
I/flutter ( 8390): 
I/flutter ( 8390): The overflowing RenderFlex has an orientation of Axis.vertical.

现在,列中的一行:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Text overflow',
      home: Scaffold(
        appBar: AppBar(title: Text("MyApp"),),
        body: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Text(String.fromCharCodes(List.generate(100, (i) => 65)))
              ],
            ),
          ],
        ),
      ),
    );
  }
}

现在,扩展第一项
以填充所有可用空间:

Column(
  children: <Widget>[
    Row(
      children: <Widget>[
        Expanded(child: Text('AAAA')),
        Text('ZZZZ')],
    ),
  ],
),
列(
儿童:[
划船(
儿童:[
扩展(子项:文本('AAAA')),
文本('ZZZZ')],
),
],
),

最后,当您展开一个很长的字符串时,您会注意到文本沿横轴方向换行:

Column(
  children: <Widget>[
    Row(
      children: <Widget>[
        Expanded(child: Text(String.fromCharCodes(List.generate(100, (i) => 65)))),
        Text('ZZZZ')],
    ),
  ],
),
列(
儿童:[
划船(
儿童:[
扩展(子:文本(String.fromCharCodes(List.generate(100,(i)=>65))),
文本('ZZZZ')],
),
],
),

以避免行或列中的文本溢出。确保将文本包装在灵活或扩展的小部件下。在将文本包装到上面的一个小部件下之后,它将把大文本包装成多行。分享一个例子:

在展开小部件之前:

return  Center(child:
        Container(
        padding: EdgeInsets.only(left: 50, right: 50),
          child: Row(
           mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                height: 200,
                 child: Text('It is a multiline text It does not  auto warp the text.', textAlign: TextAlign.center,style: TextStyle(fontSize: 20.0, color: Colors.white)),

          )
          ],
        ))
      );
返回中心(子:
容器(
填充:仅限边缘设置(左:50,右:50),
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
身高:200,
child:Text('这是一个多行文本,它不会自动扭曲文本',textAlign:textAlign.center,样式:TextStyle(fontSize:20.0,颜色:Colors.white)),
)
],
))
);
截图:

在内部展开后:

  return  Center(
        child: Padding(padding:  EdgeInsets.only(left: 50, right: 50),
          child:  Row(
            mainAxisAlignment: MainAxisAlignment.center,

            children: <Widget>[
              Expanded(

                  child: Text('It is a multiline text It does not  auto warp the text.', textAlign: TextAlign.center,style: TextStyle(fontSize: 20.0, color: Colors.white))),
            ],
          ),
        )
      );
返回中心(
子项:填充(填充:仅限边集)(左:50,右:50),
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
扩大(
子:文本('它是多行文本,不会自动扭曲文本',textAlign:textAlign.center,样式:TextStyle(fontSize:20.0,颜色:Colors.white)),
],
),
)
);

屏幕截图:

颤振提供了一个非常有用的小部件,名为,可以轻松地水平和垂直包装其子对象

Wrap(
  direction: Axis.horizontal, //default
  alignment: WrapAlignment.center,
  children: [
    Text(),   //you can use any other widget
    Text(),
    Text(),
    Text(),
    Text(),
  ],
),

使用
上的
灵活
小部件,因为它只占用所需的可用空间

Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            // The long text inside this column overflows. Remove the row and column above this comment and the text wraps.
            Flexible(
              child: Column(
                children: <Widget>[
                Text("Short text"),
                  Text(
                      "Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. ")
                ],
              ),
            ),
          ],
        ),
      ],
    ),
列(
儿童:[
划船(
儿童:[
//此列中的长文本溢出。请删除此注释上方的行和列,并将文本换行。
灵活的(
子:列(
儿童:[
文本(“短文本”),
正文(
“非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。非常长文本。”)
],
),
),
],
),
],
),
输出:

Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            // The long text inside this column overflows. Remove the row and column above this comment and the text wraps.
            Flexible(
              child: Column(
                children: <Widget>[
                Text("Short text"),
                  Text(
                      "Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. ")
                ],
              ),
            ),
          ],
        ),
      ],
    ),
Row(
                              children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: Icon(
                                    Icons.watch_later_outlined,
                                    size: 25,
                                    color: Colors.blueAccent,
                                  ),
                                ),
                                Expanded(
                                  flex: 5,
                                  child: Text("Write your long text here"
                                  ),
                                ), // name
                              ]),