专栏名称: NeXT
Android
目录
相关文章推荐
程序员小灰  ·  今天,小灰39岁了! ·  昨天  
程序员的那些事  ·  工资12000,下家给15000,我说考虑下 ... ·  3 天前  
程序员小灰  ·  部署DeepSeek ... ·  3 天前  
51好读  ›  专栏  ›  NeXT

字节跳动开源的 Flutter 富文本展示效果

NeXT  · 掘金  ·  · 2018-11-28 08:02

正文

A Tricky Solution for Implementing Inline-Image-In-Text Feature in Flutter.

Getting Started

According to the related Flutter Issues( #2022 ) , Inline-Image-In-Text is a long-time(2 years) missing feature since RichText(or the underlying Paragraph) does only support pure text. But we can solve this problem in a simple/tricky way:

  1. Regard the images as a particular blank TextSpan, convert image's width and height to textspan's letterSpacing and fontSize. the origin paragraph will do the layout operation and leave the desired image space for us.
  2. Override the paint function,calculate the right offset via the getOffsetForCaret() api to draw the image over the space.

For more details, please refer to the source code.

Usage

The only thing you have to do is converting your origin text to a TextSpan/ImageSpan List first.

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:real_rich_text/real_rich_text.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: RealRichText([
            TextSpan(
              text: "A Text Link",
              style: TextStyle(color: Colors.red, fontSize: 14),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  debugPrint("Link Clicked.");
                },
            ),
            ImageSpan(
              AssetImage("packages/real_rich_text/images/emoji_9.png"),
              width: 24,
              height: 24,
            ),
            ImageSpan(
              AssetImage("packages/real_rich_text/images/emoji_10.png"),
              width: 24,
              height: 24,
            ),
            TextSpan(
              text: "哈哈哈",
              style: TextStyle(color: Colors.yellow, fontSize: 14),
            ),
            TextSpan(
              text: "@Somebody",
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 14,
                  fontWeight: FontWeight.bold),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  debugPrint("Link Clicked");
                },
            ),
            TextSpan(
              text: " #RealRichText# ",
              style: TextStyle(color: Colors.blue, fontSize: 14),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  debugPrint("Link Clicked");
                },
            ),
            TextSpan(
              text: "showing a bigger image",
              style: TextStyle(color: Colors.black, fontSize: 14),
            ),
            ImageSpan(
              AssetImage("packages/real_rich_text/images/emoji_10.png"),
              width: 40,
              height: 40,
            ),
            TextSpan(
              text: "and seems working perfect……",
              style: TextStyle(color: Colors.black, fontSize: 14),
            ),
          ]),
        ),
      ),
    );
  }
}

Note

ImageSpan must set the width & height properties.







请到「今天看啥」查看全文