Flutter富文本Text.rich

2022年7月10日 763点热度 1人点赞 0条评论

例子一:
先上图:
Flutter富文本Text.rich插图

<code>import &#039;package:flutter/material.dart&#039;;

main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(&quot;1111&quot;),
      ),
      body: const Text(&quot;《定风波》 苏轼 莫听穿林打叶声,何妨吟啸且徐行。竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。&quot;,
          style: TextStyle(fontSize: 25,color: Colors.purple),
          maxLines: 3,
          textAlign: TextAlign.center,
         // overflow: TextOverflow.ellipsis,
      ),
    );
  }
}</code>

例子二
效果图:
Flutter富文本Text.rich插图1

<code>import &#039;package:flutter/material.dart&#039;;

main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(&quot;富文本例子二&quot;),
      ),
      body: const Text.rich(
        TextSpan(
          //TextSpan(text: &quot;《定风波》 苏轼 莫听穿林打叶声,何妨吟啸且徐行。竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。&quot;)
          children: [
            TextSpan(text: &quot;《定风波》&quot;, style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold, color: Colors.black)),
            TextSpan(text: &quot;苏轼&quot;, style: TextStyle(fontSize: 18, color: Colors.redAccent)),
            TextSpan(text: &quot;\n莫听穿林打叶声,何妨吟啸且徐行。\n竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。&quot;)
          ]
        ),
        style: TextStyle(fontSize: 20, color: Colors.purple),
        textAlign: TextAlign.center,
      ),
    );
  }
}</code>

小小调酒师

此刻打盹,你将做梦; 此刻学习,你将圆梦。 个人邮箱:shellways@foxmail.com

文章评论