【发布时间】:2019-08-05 10:40:19
【问题描述】:
我正在尝试创建聊天气泡并让它们环绕文本而不扩展到屏幕的另一端。我已经看到了有关如何执行此操作的其他线程,并尝试实现它,但没有一个有效。以下是我看到并尝试实施建议修复的线程:
Wrap text in container without using a fixed width in Flutter
我的代码:
Widget build(BuildContext context) {
return new SizeTransition(
sizeFactor: new CurvedAnimation(
parent: animationController.view,
curve: Curves.easeOut),
axisAlignment: 0.0,
child: new Container(
decoration: BoxDecoration(
border: Border.all(
color: (text[0]=='r') ? lightVioletBlue :mintgreen,
width: 2.0,
style: BorderStyle.solid,
),
borderRadius: BorderRadius.all(Radius.circular(20)),
color: (text[0] == 'r') ? lightVioletBlue:mintgreen,
),
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: new Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Container(
margin: const EdgeInsets.only(right: 16.0),
child: (text[0] == 'r') //If first letter is an r, then it indicates it's a reply.
? new CircleAvatar(child: new Text(_otherName[0]))
: null
),
new Expanded(
child: new Column(
crossAxisAlignment: (text[0] == 'r') //If first letter is an r, then it indicates it's a reply.
? CrossAxisAlignment.start
: CrossAxisAlignment.end,
children: <Widget>[
new Text((text[0] == 'r') ? _otherName : _name, //If first letter is an r, then it indicates it's a reply.
style: (text[0] == 'r') ? TextStyle(color: Colors.white) : Theme.of(context).textTheme.subhead),
new Container(
margin: const EdgeInsets.only(top: 5.0),
child: (text[0] == 'r') //If first letter is an r, then it indicates it's a reply.
? new Text(text.substring(1), style: TextStyle(color: Colors.white))
: new Text(text),
),
],
),
),
new Container(
margin: const EdgeInsets.only(left: 16.0),
child: (text[0] == 'r') //If first letter is an r, then it indicates it's a reply.
? null
: new CircleAvatar(child: new Text(_name[0]))),
],
),
) //new
);
}
我尝试在父容器上添加约束,但这需要我创建一个固定大小(至少根据我自己对颤振的理解),并且容器仍然从左侧开始。因此,来自“Ken”的消息会出现在容器的右侧,但容器会出现在屏幕中间。
【问题讨论】:
-
应该是,简而言之,Row(Expanded(Text(''))), Container(Text('Ken')))。即在气泡容器之前使用 Expanded emty Text。
-
什么意思?我尝试将 Expanded 添加到 Expanded 内的文本部分,并删除了扩展。尽管这短暂地奏效了,但一旦我从父容器中删除了约束,一切都崩溃了。
-
不,您的 Expanded 在气泡容器内,我的意思是在气泡容器外有一个 Expanded EMPTY Text。
-
在气泡容器之外是指我的行小部件吗?
-
再添加一个行小部件
标签: flutter