From 2b308b700afcffacbebc9eb6e70b1d38375be2f0 Mon Sep 17 00:00:00 2001 From: "aditya.chaudhary1558@gmail.com" Date: Sat, 23 Nov 2024 00:22:16 +0530 Subject: [PATCH] feat(random_generate): Random username and mail generation for test. --- lib/widgets/editor.dart | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/lib/widgets/editor.dart b/lib/widgets/editor.dart index 18b391b9..f0e55c4e 100644 --- a/lib/widgets/editor.dart +++ b/lib/widgets/editor.dart @@ -45,6 +45,84 @@ class _TextFieldEditorState extends State { void initState() { super.initState(); editorFocusNode = FocusNode(debugLabel: "Editor Focus Node"); + controller.addListener(() { + _processPlaceholders(); + }); + } + + void _processPlaceholders() { + final placeholderRegex = RegExp(r'\{\{(.*?)\}\}'); + final matches = placeholderRegex.allMatches(controller.text); + + if (matches.isNotEmpty) { + String updatedText = controller.text; + + for (final match in matches) { + final placeholder = match.group(1); + String replacement = ""; + + switch (placeholder) { + case 'username': + replacement = _generateRandomUsername(); + break; + case 'email': + replacement = _generateRandomEmail(); + break; + default: + replacement = "{{unknown}}"; + } + + updatedText = updatedText.replaceFirst(match.group(0)!, replacement); + } + + controller.value = TextEditingValue( + text: updatedText, + selection: TextSelection.collapsed(offset: updatedText.length), + ); + + widget.onChanged?.call(updatedText); + } + } + + String _generateRandomUsername() { + const adjectives = [ + 'Brave', + 'Clever', + 'Swift', + 'Bright', + 'Happy', + 'Lively', + 'Strong', + 'Gentle', + 'Kind', + 'Bold' + ]; + const nouns = [ + 'Lion', + 'Eagle', + 'Fox', + 'Wolf', + 'Bear', + 'Tiger', + 'Hawk', + 'Dolphin', + 'Panther', + 'Dragon' + ]; + + final random = math.Random(); + String adjective = adjectives[random.nextInt(adjectives.length)]; + String noun = nouns[random.nextInt(nouns.length)]; + + return '$adjective$noun${random.nextInt(1000)}'; + } + + String _generateRandomEmail() { + const domains = ['example.com', 'test.com', 'mail.com']; + final random = math.Random(); + final name = _generateRandomUsername().toLowerCase(); + final domain = domains[random.nextInt(domains.length)]; + return '$name@$domain'; } @override