How to read and write rtf file in flutter app

82 views Asked by At

I have a .rtf file and planned to view it on my Flutter app. Is there any recommeneded way to acheive this? So far I just found ways to create rich text using RichText widget but this is not what I am looking for.

1

There are 1 answers

7
SaWin0 On

To read rtf file, I have posted an example code. First, please add rtf file under your assets' folder. I have added example.rtf file.

class RftReaderScreen extends StatefulWidget {
  const RftReaderScreen({Key? key}) : super(key: key);

  @override
  State<RftReaderScreen> createState() => _RftReaderScreenState();
}

class _RftReaderScreenState extends State<RftReaderScreen> {
  String? _res;

  @override
  void initState() {
    super.initState();

    // Here we are accessing example.rtf file
    // loadString method fetches all the text from example.rtf file 
    // after loading all the text we are setting to our local variable
    rootBundle.loadString("assets/example.rtf").then((value) => setState(() {
          _res = value;
        }));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(_res ?? "Loading . . ."),
      ),
    );
  }
}

Don't forget to import services

import 'package:flutter/services.dart';

Here you can find entire code: https://gist.github.com/sawin0/8262f087524ce9b1a22d192422bddd37

I hope this helps. Happy learning...