Display a few words in different colors in Flutter

前端 未结 3 1868
感情败类
感情败类 2021-01-04 04:41

I am writing an application which shows a few words in different colors in flutter.

I tried to load HTML files using the plugin flutter_html_view but that one doesn

3条回答
  •  爱一瞬间的悲伤
    2021-01-04 04:50

    Use RichText, TextSpan and TextStyle as i explained in below picture.

    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          home: Center(
            child: RichText(
              text: TextSpan(
                text: 'Default',
                style: TextStyle(color: Colors.red), /*defining default style is optional */
                children: [
                  TextSpan(
                      text: ' bold', style: TextStyle(fontWeight: FontWeight.bold)),
                  TextSpan(
                      text: ' colorful',
                      style: TextStyle(color: Colors.lightGreenAccent)),
                  TextSpan(
                      text: ' large',
                      style: TextStyle(color: Colors.cyanAccent, fontSize: 40)),
                ],
              ),
            ),
          ),
        );
      }
    }
    

提交回复
热议问题