skip to Main Content

In my app, users can write in English as well as Arabic. So for English, I want to use "Poppins" as font style, and for Arabic, I want to use "sfArabic". I did not find any proper solution for that.

Below is my expected output –

enter image description here

2

Answers


  1. Try below packages hope its help to you.

    1. rich_text_controller
    2. flutter_social_textfield
    Login or Signup to reply.
  2. You can customize style of any text in TextField by extending TextEditingController and override buildTextSpan method.

    Check the code below:-

    import 'package:flutter/material.dart';
    
    class MultiStyleTextEditingController extends TextEditingController {
      
      bool isEnglishWord(String input) =>
          RegExp(r'^[A-Za-z][A-Za-z0-9]*$').hasMatch(input);
    
      bool isArabicWord(String input) => RegExp(r"^[ء-ي]+$").hasMatch(input);
    
      @override
      TextSpan buildTextSpan(
          {required BuildContext context,
          TextStyle? style,
          required bool withComposing}) {
        final words = text.split('');
    
        var textStyle = const TextStyle();
    
        final textSpanChildren = <TextSpan>[];
        for (var word in words) {
          if (isEnglishWord(word)) {
            textStyle =
                textStyle.copyWith(color: Colors.black, fontFamily: 'Poppins');
          } else if (isArabicWord(word)) {
            textStyle =
                textStyle.copyWith(color: Colors.red, fontFamily: 'sfArabic');
          }
          final TextStyle newTextStyle = style?.merge(textStyle) ?? textStyle;
    
          final child = TextSpan(
            text: word,
            style: newTextStyle,
          );
          textSpanChildren.add(child);
        }
        return TextSpan(
          style: style,
          children: textSpanChildren,
        );
      }
    }
    

    The result

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search