mobile_skt/lib/widgets/form_item/sk_base_field.dart

45 lines
1.1 KiB
Dart
Raw Normal View History

2024-10-16 09:48:17 +08:00
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
abstract class SkBaseFieldWidget extends StatelessWidget {
final FocusNode focusNode = FocusNode();
late final SkBaseFieldController baseFieldController;
final bool customLabel;
final String? labelText;
final Color? fillColor;
SkBaseFieldWidget({
super.key,
this.customLabel = false,
this.labelText,
this.fillColor,
autoFocus = false,
}) {
baseFieldController = Get.put(
SkBaseFieldController(autoFocus: autoFocus, focusNode: focusNode),
tag: '${labelText ?? Random().nextInt(1000)}',
);
}
}
class SkBaseFieldController extends GetxController {
RxBool isFocus = false.obs;
final bool autoFocus;
FocusNode? focusNode;
SkBaseFieldController({required this.autoFocus, this.focusNode});
@override
void onReady() {
if (autoFocus) {
WidgetsBinding.instance.addPostFrameCallback((_) {
focusNode?.requestFocus();
});
}
focusNode?.addListener(() {
isFocus.value = focusNode?.hasFocus ?? false;
});
super.onReady();
}
}