es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Problema de tamaño de fuente en Flutter al utilizar `Text.rich()` o `RichText`.

Usé Text.rich() o RichText con un tamaño de fuente de 25, pero el tamaño de RichText se ve más grande que Text.rich(). Por favor, dime por qué hay una diferencia de tamaño, he especificado un tamaño de fuente de 25 para ambos widgets.

Este código muestra un tamaño de fuente pequeño:

runApp(MaterialApp(
    theme: ThemeData(fontFamily: "nunito"),
    home: Scaffold(
      appBar: AppBar(title: Text("PUROGRAMMER")),
      body: Center(child: Container(
        child: Text.rich(
         TextSpan(
           style: TextStyle(fontSize: 20, color: Colors.grey),
           children: [
                  TextSpan(text: "Click"),
                  TextSpan(text: " + ", style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
                  TextSpan(text: "to add")
           ]
         )
         )),)
    ),
  ));

Y esto muestra un tamaño de fuente grande:

runApp(MaterialApp(
    theme: ThemeData(fontFamily: "nunito"),
    home: Scaffold(
      appBar: AppBar(title: Text("PUROGRAMMER")),
      body: Center(child: Container(
        child: TRichText(
         text: TextSpan(
           style: TextStyle(fontSize: 20, color: Colors.grey),
           children: [
                  TextSpan(text: "Click"),
                  TextSpan(text: " + ", style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
                  TextSpan(text: "to add")
           ]
         )
         )),)
    ),
  ));
Tags:  , , ,

Answer

  1. Avatar for davy.ai

    El tamaño de RichText se ve más grande que Text.rich() porque RichText utiliza un estilo de texto predeterminado que anula el tamaño de fuente especificado en TextSpan. Para solucionar esto, puedes establecer explícitamente el estilo de texto en RichText con un tamaño de fuente de 25, de la siguiente manera:

    RichText(
      text: TextSpan(
        style: TextStyle(fontSize: 25),
        children: [
          TextSpan(text: "Clic"),
          TextSpan(text: " + ", style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
          TextSpan(text: "para añadir")
        ]
      ),
    )
    

    Esto asegurará que tanto Text.rich() como RichText tengan el mismo tamaño de fuente de 25.

Comments are closed.