Flutter提取文本小部件并使用它

问题描述 投票:0回答:1

我有一个data,正在不同的Text中使用。下面是代码片段。

var data = "Hello world";

Text(
  data,
  style: TextStyle(fontSize: 10),
);

Text(
  data,
  style: TextStyle(fontSize: 15),
);

Text(
  data,
  style: TextStyle(fontSize: 20),
);

如您所见,Text(data)在所有情况中都很常见,所以我认为我可以做出类似的事情:

var text = Text("Hello World");

text.copyWith(style: TextStyle(fontSize: 10));

text.copyWith(style: TextStyle(fontSize: 15));

text.copyWith(style: TextStyle(fontSize: 20));

但这是不可能的,任何人都可以告诉我如何实现这样的目标?

flutter dart
1个回答
0
投票

也许您可以通过使其成为函数来实现?

类似这样的东西:

Widget textFunction(int fontsize){
    return Text(
             data,
             style: TextStyle(fontSize: fontsize),
           );
}

您这样称呼它:

...
textFunction(10),
textFunction(15),
textFunction(20),
...
© www.soinside.com 2019 - 2024. All rights reserved.