2024-04-09 16:32:18 +08:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
|
|
|
|
abstract class SkBaseFieldWidget extends StatelessWidget {
|
2024-04-10 11:04:27 +08:00
|
|
|
final FocusNode focusNode = FocusNode();
|
|
|
|
late final SkBaseFieldController baseFieldController;
|
2024-04-09 16:32:18 +08:00
|
|
|
final bool customLabel;
|
|
|
|
final String? labelText;
|
2024-04-10 11:04:27 +08:00
|
|
|
final Color? fillColor;
|
2024-04-09 16:32:18 +08:00
|
|
|
SkBaseFieldWidget({
|
|
|
|
super.key,
|
|
|
|
this.customLabel = false,
|
|
|
|
this.labelText,
|
2024-04-10 11:04:27 +08:00
|
|
|
this.fillColor,
|
2024-04-09 16:32:18 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|