final _scaffoldMessengerKey = getIt<GlobalKey<ScaffoldMessengerState>>();
void showSnackBar({required String text}) {
_scaffoldMessengerKey.currentState
?..clearSnackBars()
..showSnackBar(
SnackBar(
content: Text(text),
action: SnackBarAction(
label: 'Закрыть',
textColor: Colors.yellow,
onPressed: () {},
),
),
);
}
class _AppState extends State<App> {
final rootScaffoldMessengerKey = getIt<GlobalKey<ScaffoldMessengerState>>();
@override
Widget build(BuildContext context) {
return MaterialApp.router(
scaffoldMessengerKey: rootScaffoldMessengerKey,
builder: (context, child) {
return child;
},
);
}
}
class RuNumberTextInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue,
TextEditingValue newValue,
) {
final int newTextLength = newValue.text.length;
int selectionIndex = newValue.selection.end;
int usedSubstringIndex = 0;
final StringBuffer newText = StringBuffer();
if (newTextLength >= 1) {
newText.write('(');
if (newValue.selection.end >= 1) selectionIndex++;
}
if (newTextLength >= 4) {
newText.write('${newValue.text.substring(0, usedSubstringIndex = 3)}) ');
if (newValue.selection.end >= 3) selectionIndex += 2;
}
if (newTextLength >= 7) {
newText.write('${newValue.text.substring(3, usedSubstringIndex = 6)}-');
if (newValue.selection.end >= 6) selectionIndex++;
}
if (newTextLength >= 9) {
newText.write('${newValue.text.substring(6, usedSubstringIndex = 8)}-');
if (newValue.selection.end >= 8) selectionIndex++;
}
if (newTextLength >= usedSubstringIndex) {
newText.write(newValue.text.substring(usedSubstringIndex));
}
if (newTextLength > 10) {
return TextEditingValue(
text: oldValue.text,
selection: TextSelection.collapsed(offset: oldValue.text.length),
);
}
return TextEditingValue(
text: newText.toString(),
selection: TextSelection.collapsed(offset: selectionIndex),
);
}
}