英文:
How to place a Spacer between two TextSpan widgets?
问题
在两个TextSpan小部件之间放置Spacer的方法是什么?
在下一个示例中,我有两个文本,XXX和YYY,我想要一个Spacer,它将把YYY推到右边。
我想要对XXX语句进行对齐,然后将YYY右对齐并在XXX的末尾和YYY的开头之间插入一个空格。
我尝试了以下示例,但它不起作用:
RichText(
text: TextSpan(
children: [
TextSpan(text: "xxxxx xxxx xx x xxx xxxxx xxx xxxx", style: titleStyle),
const WidgetSpan(child: Spacer()),
TextSpan(text: "yyyy", style: titleStyle),
],
)
);
英文:
How to place a Spacer between two TextSpan widgets?
In the next example I have two texts XXX and YYY, and I would like to have a spacer that will push YYY to the right end.
I want to justify statement XXX and then YYYY right aligned and insert a space between the end of XXX and the beginning of YYY.
I tried the following sample, but it does not work:
RichText(
text: TextSpan(
children: [
TextSpan(text: "xxxxx xxxx xx x xxx xxxxx xxx xxxx", style: titleStyle),
const WidgetSpan(child: Spacer()),
TextSpan(text: "yyyy", style: titleStyle),
],
)
);
答案1
得分: 1
目前,没有直接的方法可以做到,但可以通过使用一些技巧来实现。我能想到的一种方法是将 TextSpan 小部件与文本 'yyyy' 结合使用两次,再加上 Stack 小部件,以达到你的需求。之所以使用两次是因为第一个 YYYY 小部件将始终是不可见的,但将占用与第二个 YYYY 相同的宽度,并且始终在 RichText 的末尾保留一个空间。用文字解释会更困难,代码会更有帮助。
英文:
Currently, there is no direct way of doing it, but it can be achieved by using some tricks. One such approach I could think of is using your TextSpan widget with text 'yyyy' twice along with Stack widget, to achieve what you need. The reason for using it twice is that the first YYYY widget will always be invisible but will occupy the same width as the second YYYY and it will always keep a space at the end of the RichText. It's more difficult for me to explain in words, instead the code will be more helpful.<br>
You can also find the following code at dart pad
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text("Flutter App"),
),
body: Stack(
children: [
Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: RichText(
overflow: TextOverflow.visible,
text: const TextSpan(
children: [
TextSpan(
text: "xxxxx xxxx xx x xxx xxxxx xxx xxxx xxxx xxx",
style: TextStyle(
color: Colors.black,
fontSize: 24,
)),
TextSpan(
text: "yyyy",
style: TextStyle(
color: Colors.transparent,
fontSize: 24,
),
),
],
)),
),
],
),
Positioned(
bottom: 0,
right: 0,
child: RichText(text: const TextSpan(
text: "yyyy",
style: TextStyle(
color: Colors.black,
fontSize: 24,
),
),))
],
),
);
}
}
Here is the screenshot<br>
答案2
得分: 0
这可能是不可能的,因为 RichText
只是一个文本样式小部件,而不是一个布局小部件(与 Row
不同,例如)。因此,尝试使用更适合的小部件为您的特定问题找到另一个解决方案,不要浪费时间与框架抗争。这一切都关乎常识。
英文:
It's probably impossible, simply because RichText
is a text styling widget, not a layout one (as opposed to Row
, for example). So just try to find another solution for your specific problem using more appropriate widgets and don't waste your time fighting the framework. It's all about common sense.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论