Amfani da RichText a cikin Flutter: Jagora da Misalai

A Flutter, RichText shine widget din da ke ba ka damar ƙirƙirar rubutu tare da salo daban-daban da tsarawa a cikin widget ɗin rubutu guda ɗaya. Kuna iya amfani da TextSpan widgets da yawa don ayyana sassa daban-daban na rubutu tare da salo daban-daban.

Ga misalin yadda ake amfani da shi RichText:

import 'package:flutter/material.dart';  
  
void main() {  
  runApp(MyApp());  
}  
  
class MyApp extends StatelessWidget {  
  @override  
  Widget build(BuildContext context) {  
    return MaterialApp(  
      home: MyHomePage(),  
   );  
  }  
}  
  
class MyHomePage extends StatelessWidget {  
  @override  
  Widget build(BuildContext context) {  
    return Scaffold(  
      appBar: AppBar(  
        title: Text('RichText Example'),  
     ),  
      body: Center(  
        child: RichText(  
          text: TextSpan(  
            text: 'Hello ',  
            style: DefaultTextStyle.of(context).style,  
            children: <TextSpan>[  
              TextSpan(  
                text: 'Flutter',  
                style: TextStyle(  
                  fontWeight: FontWeight.bold,  
                  color: Colors.blue,  
               ),  
             ),  
              TextSpan(text: ' is amazing!'),  
            ],  
         ),  
       ),  
     ),  
   );  
  }  
}  

A cikin wannan misali, RichText ana amfani da widget din don ƙirƙirar rubutu mai salo daban-daban. Ana amfani da widget TextSpan din azaman yara don ayyana sassa daban-daban na rubutu tare da salo daban-daban.

  • An tsara na farko TextSpan ta hanyar amfani da tsohowar salon rubutu na mahallin(a wannan yanayin, ya gaji tsarin tsoho na AppBar).
  • Na biyu TextSpan yana amfani da madaidaicin nauyin rubutu da launin shuɗi ga kalmar " Flutter."
  • Na uku TextSpan kawai yana ƙara rubutun "abin mamaki ne!" har zuwa karshe.

Kuna iya keɓance tsarawa, fonts, launuka, da sauran salo a cikin kowane TextSpan gwargwadon buƙata.

Widget din RichText yana da amfani musamman lokacin da kake buƙatar amfani da salo daban-daban zuwa sassa daban-daban na rubutunka, kamar lokacin nuna abubuwan da aka tsara ko jaddada takamaiman kalmomi ko jimloli.

Jin kyauta don gwaji tare da salo daban-daban da TextSpan widgets na gida don cimma tasirin gani da ake so a cikin app ɗin ku.