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

时间:2019-09-13 09:26:22

标签: flutter dart

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

!(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
            )
           ),
          ],
          ),
         )

1 个答案:

答案 0 :(得分:0)

如果您有字符串键,则在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)),
        ],
      ),
    ));
  }
}