stack_wallet/lib/widgets/textfields/frost_step_field.dart

183 lines
6.2 KiB
Dart
Raw Normal View History

2024-05-01 00:41:02 +00:00
import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/conditional_parent.dart';
import 'package:stackwallet/widgets/icon_widgets/clipboard_icon.dart';
import 'package:stackwallet/widgets/icon_widgets/qrcode_icon.dart';
import 'package:stackwallet/widgets/icon_widgets/x_icon.dart';
import 'package:stackwallet/widgets/textfield_icon_button.dart';
class FrostStepField extends StatefulWidget {
const FrostStepField({
super.key,
required this.controller,
required this.focusNode,
this.label,
this.hint,
2024-05-01 15:13:47 +00:00
required this.onChanged,
2024-05-01 00:41:02 +00:00
required this.showQrScanOption,
});
final TextEditingController controller;
final FocusNode focusNode;
final String? label;
final String? hint;
2024-05-01 15:13:47 +00:00
final void Function(String) onChanged;
2024-05-01 00:41:02 +00:00
final bool showQrScanOption;
@override
State<FrostStepField> createState() => _FrostStepFieldState();
}
class _FrostStepFieldState extends State<FrostStepField> {
final _xKey = UniqueKey();
final _pasteKey = UniqueKey();
late final Key? _qrKey;
bool _isEmpty = true;
final _inputBorder = OutlineInputBorder(
borderSide: const BorderSide(
width: 0,
color: Colors.transparent,
),
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
);
2024-05-01 15:13:47 +00:00
late final void Function(String) _changed;
2024-05-01 00:41:02 +00:00
@override
void initState() {
_qrKey = widget.showQrScanOption ? UniqueKey() : null;
_isEmpty = widget.controller.text.isEmpty;
2024-05-01 15:13:47 +00:00
_changed = (value) {
if (context.mounted) {
widget.onChanged.call(value);
setState(() {
_isEmpty = widget.controller.text.isEmpty;
});
}
};
2024-05-01 00:41:02 +00:00
super.initState();
}
@override
Widget build(BuildContext context) {
return ConditionalParent(
condition: widget.label != null,
builder: (child) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
widget.label!,
style: STextStyles.w500_14(context),
),
const SizedBox(
height: 4,
),
child,
],
),
child: TextField(
controller: widget.controller,
focusNode: widget.focusNode,
readOnly: false,
autocorrect: false,
enableSuggestions: false,
style: STextStyles.field(context),
2024-05-01 15:13:47 +00:00
onChanged: _changed,
2024-05-01 00:41:02 +00:00
decoration: InputDecoration(
hintText: widget.hint,
fillColor: widget.focusNode.hasFocus
? Theme.of(context).extension<StackColors>()!.textFieldActiveBG
: Theme.of(context).extension<StackColors>()!.textFieldDefaultBG,
hintStyle: Util.isDesktop
? STextStyles.desktopTextFieldLabel(context)
: STextStyles.fieldLabel(context),
enabledBorder: _inputBorder,
focusedBorder: _inputBorder,
errorBorder: _inputBorder,
disabledBorder: _inputBorder,
focusedErrorBorder: _inputBorder,
suffixIcon: Padding(
padding: _isEmpty
? const EdgeInsets.only(right: 8)
: const EdgeInsets.only(right: 0),
child: UnconstrainedBox(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
!_isEmpty
? TextFieldIconButton(
semanticsLabel:
"Clear Button. Clears The Frost Step Field Input.",
key: _xKey,
onTap: () {
widget.controller.text = "";
2024-05-01 15:13:47 +00:00
_changed(widget.controller.text);
2024-05-01 00:41:02 +00:00
},
child: const XIcon(),
)
: TextFieldIconButton(
semanticsLabel:
"Paste Button. Pastes From Clipboard To Frost Step Field Input.",
key: _pasteKey,
onTap: () async {
final ClipboardData? data =
await Clipboard.getData(Clipboard.kTextPlain);
if (data?.text != null && data!.text!.isNotEmpty) {
widget.controller.text = data.text!.trim();
}
2024-05-01 15:13:47 +00:00
_changed(widget.controller.text);
2024-05-01 00:41:02 +00:00
},
child:
_isEmpty ? const ClipboardIcon() : const XIcon(),
),
if (_isEmpty && widget.showQrScanOption)
TextFieldIconButton(
semanticsLabel:
"Scan QR Button. Opens Camera For Scanning QR Code.",
key: _qrKey,
onTap: () async {
try {
if (FocusScope.of(context).hasFocus) {
FocusScope.of(context).unfocus();
await Future<void>.delayed(
const Duration(milliseconds: 75));
}
final qrResult = await BarcodeScanner.scan();
widget.controller.text = qrResult.rawContent;
2024-05-01 15:13:47 +00:00
_changed(widget.controller.text);
2024-05-01 00:41:02 +00:00
} on PlatformException catch (e, s) {
Logging.instance.log(
"Failed to get camera permissions while trying to scan qr code: $e\n$s",
level: LogLevel.Warning,
);
}
},
child: const QrCodeIcon(),
),
],
),
),
),
),
),
);
}
}