specialText static method

Widget specialText(
  1. BuildContext context,
  2. String text, {
  3. TextAlign textAlign = TextAlign.start,
  4. FontWeight fontWeight = FontWeight.normal,
  5. Color? color,
  6. required dynamic profileFunction({
    1. required Media? avatar,
    2. required Media? banner,
    3. required String? displayname,
    4. required int userID,
    5. required String username,
    }),
})

Implementation

static Widget specialText(
  BuildContext context,
  String text, {
  TextAlign textAlign = TextAlign.start,
  FontWeight fontWeight = FontWeight.normal,
  Color? color,
  required Function({
    required int userID,
    required String username,
    required String? displayname,
    required Media? avatar,
    required Media? banner,
  }) profileFunction,
}) {
  final lines = text.split('\n');
  final textSpans = <TextSpan>[];

  // URL tespiti için bir düzenli ifade (regex)
  final urlRegex = RegExp(
      r"((https?:www\.)|(https?:\/\/)|(www\.))[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}(\/[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)?");

  for (String line in lines) {
    final words = line.split(' ');
    final lineSpans = <TextSpan>[];

    for (int i = 0; i < words.length; i++) {
      final word = words[i];

      if (word.isNotEmpty) {
        if (word.startsWith('#')) {
          final trimmedWord = word.trim();
          lineSpans.add(TextSpan(
            text: trimmedWord,
            style: const TextStyle(
              color: Colors.blue,
              fontWeight: FontWeight.bold,
            ),
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                // Burada # işaretine tıklandığında yapılacak işlemi ekleyin
                log('Tapped on hashtag: $trimmedWord');
              },
          ));
        } else if (word.startsWith('@')) {
          final username = word.trim(); // @ işaretini kaldır ve trim yap
          lineSpans.add(TextSpan(
            text: username,
            style: const TextStyle(
              color: Colors.amber,
              fontWeight: FontWeight.bold,
            ),
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                // Burada @ işaretine tıklandığında yapılacak işlemi ekleyin
                log('Tapped on username: $username');
                profileFunction(
                  userID: 0,
                  username: username.substring(1),
                  displayname: null,
                  avatar: null,
                  banner: null,
                );
              },
          ));
        } else if (urlRegex.hasMatch(word)) {
          final match = urlRegex.firstMatch(word);
          final url = match?.group(0) ?? word;

          lineSpans.add(TextSpan(
            text: url,
            style: const TextStyle(
              color: Colors.blueAccent,
              decoration: TextDecoration.underline,
            ),
            recognizer: TapGestureRecognizer()
              ..onTap = () async {
                ARMOYUWidget.showConfirmationDialog(context,
                    question:
                        "Bu bağlantı uygulama dışına yönlendiriyor. Güvenliğiniz için, tıklamadan önce bağlantının güvenilir olduğundan emin olun.",
                    accept: () async {
                  if (!url.startsWith('http')) {
                    await launchUrl(Uri.parse('https://$url'));
                  } else {
                    await launchUrl(Uri.parse(url));
                  }
                });
              },
          ));
        } else {
          lineSpans.add(
            TextSpan(
              text: word,
              style: TextStyle(color: color),
            ),
          );
        }

        if (i < words.length - 1) {
          lineSpans.add(const TextSpan(text: ' '));
        }
      }
    }

    textSpans.add(TextSpan(children: lineSpans));
    textSpans.add(const TextSpan(text: '\n'));
  }

  // return Text("data");
  return RichText(
    text: TextSpan(
      children: textSpans,
      style: TextStyle(fontWeight: fontWeight, color: Get.theme.primaryColor),
    ),
    textAlign: textAlign,
  );
}