/
main.dart
120 lines (115 loc) · 3.78 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import 'package:flutter/material.dart';
import 'package:telegram/telegram.dart';
void main() {
runApp(const TelegramApp());
}
class TelegramApp extends StatefulWidget {
const TelegramApp({super.key});
@override
State<TelegramApp> createState() => _TelegramAppState();
}
class _TelegramAppState extends State<TelegramApp> {
final _key = GlobalKey<FormState>();
String? username;
String? message;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Telegram',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
resizeToAvoidBottomInset: true,
appBar: AppBar(
centerTitle: false,
title: Text(
username == null || username == '' ? 'Telegram' : '@$username'),
actions: [
// Copy to clipboard
IconButton(
tooltip: 'Copy to clipboard',
onPressed: () {
if (_key.currentState!.validate()) {
Telegram.copyLinkToClipboard(
username: username ?? '', message: message);
}
},
icon: const Icon(Icons.content_copy),
),
// Send via telegram
IconButton(
tooltip: 'Send',
onPressed: () {
if (_key.currentState!.validate()) {
if (message == null) {
Telegram.send(username: username!);
} else {
Telegram.send(
username: username!,
message: message,
);
}
}
},
icon: const Icon(Icons.send),
),
],
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Form(
key: _key,
child: Column(
children: [
TextFormField(
decoration: const InputDecoration(
labelText: 'Username',
),
onChanged: (String value) {
setState(() {
username = value;
});
},
validator: (value) {
if (value == null || value.isEmpty) {
return 'Username is required';
}
if (value.length < 5) {
return 'Username must be at least 5 characters long';
}
// Validating username with regex
if (!RegExp(r'^([a-zA-Z0-9_-]*)$').hasMatch(value)) {
return 'Invalid username';
}
return null;
},
),
TextFormField(
minLines: 1,
maxLines: 20,
textAlign: TextAlign.justify,
decoration: const InputDecoration(
labelText: 'Message (Optional)',
labelStyle:
TextStyle(overflow: TextOverflow.ellipsis),
),
onChanged: (String value) {
message = value;
},
),
],
),
),
],
),
),
),
),
);
}
}