gpt4 book ai didi

flutter - 为什么硬编码时TextSpan中的文本会自动换行,而通过对象传递时为什么不自动换行

转载 作者:行者123 更新时间:2023-12-03 03:05:01 43 4
gpt4 key购买 nike

因此,正如标题所述,当文本经过硬编码时,它将自动换行而不是拆分单词……但是当通过对象传递相同的字符串时,它不会自动换行……我会丢失什么?

!(https://imgur.com/FuBnzDN)

!(https://imgur.com/8CnY5fq)

RichText(
softWrap: true,
text: TextSpan(
text: "Synonyms:",
style: TextStyle(
color: Colors.white
),
children: <TextSpan>[
TextSpan(
text:
"\n${wordOfTheDay.synonyms}",
// text: '\nwall, rampart, fortification, parapet, stockade, palisade, barricade, embankment, earthwork',
style: TextStyle(
fontSize: 20.0,
color: Colors.white70
)
),
],
),
)

最佳答案

如果您有字符串键,则在dart中只能这样访问它们:object['stringKey'] ..

enter image description here

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
backgroundColor: Colors.black38,
body: SafeArea(
child: MyHomePage(),
),
),
);
}
}

class MyHomePage extends StatelessWidget {
var wordOfTheDay = {
'synonyms': '\nwall, rampart, fortification, parapet, stockade, palisade, barricade, embankment, earthwork'
};

@override
Widget build(BuildContext context) {
print(wordOfTheDay);
return Container(
child: RichText(
softWrap: true,
text: TextSpan(
text: "Synonyms:",
style: TextStyle(color: Colors.black),
children: <TextSpan>[
TextSpan(
text: '\n${wordOfTheDay["synonyms"]}',
// text: '\nwall, rampart, fortification, parapet, stockade, palisade, barricade, embankment, earthwork',
style: TextStyle(fontSize: 20.0, color: Colors.white70)),
],
),
));
}
}

关于flutter - 为什么硬编码时TextSpan中的文本会自动换行,而通过对象传递时为什么不自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57920858/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com