Flutter: RichText does not work with theme

960

After some learnings, I could answer my question.

Future<List<TextSpan>> listOfEventsAtDay({String day, String monthNum, BuildContext context}) async {
  final result = await DatabaseHelper.instance
      .queryForEventsAtDay(mnth: monthNum, dy: day);
  List<TextSpan> textSpans = [];
  int count = result.length;
  for (int i = 0; i < count; i++) {
    final String string = "-${result[i]['year']}: ${result[i]['title']}\n";
    textSpans.add(TextSpan(
        text: string,
        style: DefaultTextStyle.of(context).style,
        ));
  }

This fixed it.

Share:
960
mig001
Author by

mig001

Just curious!

Updated on December 24, 2022

Comments

  • mig001
    mig001 over 1 year

    I am trying to display some text in different colors. I learned about the RichText widget and used it as follows;

    Future<List<TextSpan>> listOfEventsAtDay({String day, String monthNum}) async {
      final result = await DatabaseHelper.instance
          .queryForEventsAtDay(mnth: monthNum, dy: day);
      List<TextSpan> textSpans = [];
      int count = result.length;
      for (int i = 0; i < count; i++) {
        final String string = "-${result[i]['year']}: ${result[i]['title']}\n";
        textSpans.add(TextSpan(
            text: string,
            ));
      }
    
      return textSpans;
    }
    

    which is later called on;

    ListTile( title: RichText(text: TextSpan(children: snapshot.data),
    

    I don't want any complex stylings for now. In dark mode, the text should be in white, and in the light mode, it should be in black. That's all. But problem is, the text is always white. Why doesn't it adjust with brightness settings? Is there any way to accomplish this? Please help me...

  • mako
    mako about 3 years
    related question for those who DefaultTextStyle is failing to help (for me, I had to use Theme.of(context).textTheme.bodyText1) stackoverflow.com/questions/66416669/…