skip to Main Content

i want to make richtext that has "read more" button like this image
richtext() made bold text, and then there is a "baca selengkapnya" means "read more"

my code bellow:

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:readmore/readmore.dart';

class BoldAsteris extends StatelessWidget {
  final String text;
  bool isExpanded = false;

  BoldAsteris({required this.text});

  @override
  Widget build(BuildContext context) {

    String ubahAsteris = text.replaceAll(RegExp(r'**'), "*");
    List<String> words = ubahAsteris.split('*');
    List<TextSpan> spans = List<TextSpan>.generate(words.length, (index) {
      if (index.isOdd) {
        return TextSpan(
          text: words[index],
          style: TextStyle(fontWeight: FontWeight.bold),
        );
      } else {
        return TextSpan(text: words[index]);
      }
    });
    return RichText(
      softWrap: true,
      overflow: TextOverflow.clip,
      text: TextSpan(
          children: spans, style: TextStyle(color: Colors.black, fontSize: 17)),
    );
  }
}

please help guys

2

Answers


  1. Chosen as BEST ANSWER

    i did it by myself,

    1. divide the text by two (bagian1, bagian2)
    2. use bagian1 as primary text and then bold
    3. create myReadmore widget, return Container or "readmore"
    4. return Column contains RichText and myReadmore

    my code below:

    import 'package:flutter/material.dart';
    
    class BoldAsteris extends StatefulWidget {
      final String text;
    
      BoldAsteris({required this.text});
    
      @override
      _BoldAsterisState createState() => _BoldAsterisState();
    }
    
    class _BoldAsterisState extends State<BoldAsteris> {
      // variable
      late bool isExpanded = false;
      late String bagian1;
      late String bagian2;
    
      @override
      void initState() {
        super.initState();
    
        // init for divide the text by two
        if (widget.text.length > 550 && isExpanded == false) {
          bagian1 = widget.text.substring(0, 550);
          bagian2 = widget.text.substring(551, widget.text.length);
        } else {
          bagian1 = widget.text;
          bagian2 = "";
        }
      }
    
      @override
      Widget build(BuildContext context) {
    
        // bold text between "*"
        String ubahAsteris = bagian1.replaceAll(RegExp(r'**'), "*");
        List<String> words = ubahAsteris.split('*');
        List<TextSpan> spans = List<TextSpan>.generate(words.length, (index) {
          if (index.isOdd) {
            return TextSpan(
              text: words[index],
              style: TextStyle(fontWeight: FontWeight.bold),
            );
          } else {
            return TextSpan(text: words[index]);
          }
        });
    
        // my Readmore widget
        Widget myReadmore() {
          return bagian2 == ""
              ? Container(
                  height: 0,
                )
              : InkWell(
                  child: Text('Baca selengkapnya', style: TextStyle(color: Colors.blue, fontSize: 17),),
                  onTap: () {
                    print('object');
                    setState(() {
                      isExpanded = true;
                      bagian1 = widget.text;
                      bagian2 = "";
                    });
                  },
                );
        }
    
        // return Column -> RichText & readmore widget
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            RichText(
              softWrap: true,
              overflow: TextOverflow.clip,
              text: TextSpan(
                  children: spans,
                  style: TextStyle(color: Colors.black, fontSize: 17)),
            ),
            myReadmore() // my Readmore here !
          ],
        );
      }
    }
    
    

  2. Change your code this way,

    import 'package:flutter/material.dart';
    import 'package:flutter/rendering.dart';
    import 'package:readmore/readmore.dart';
    
    class BoldAsteris extends StatelessWidget {
      final String text;
      final bool isExpanded;
    
      BoldAsteris({required this.text, this.isExpanded = false});
    
      @override
      Widget build(BuildContext context) {
        String ubahAsteris = text.replaceAll(RegExp(r'**'), "*");
        List<String> words = ubahAsteris.split('*');
        List<TextSpan> spans = List<TextSpan>.generate(words.length, (index) {
          if (index.isOdd) {
            return TextSpan(
              text: words[index],
              style: TextStyle(fontWeight: FontWeight.bold),
            );
          } else {
            return TextSpan(text: words[index]);
          }
        });
    
        return ReadMoreText(
          text: ubahAsteris,
          trimLines: 2,
          colorClickableText: Colors.blue,
          trimMode: TrimMode.Line,
          trimCollapsedText: 'Baca Selengkapnya',
          trimExpandedText: 'Sembunyikan',
          moreStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
          lessStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
          style: TextStyle(color: Colors.black, fontSize: 17),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search