You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.1 KiB
Dart
55 lines
1.1 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
class ReactiveController extends GetxController {
|
|
Widget? _cachedView;
|
|
|
|
Widget render(Widget Function() build) {
|
|
if (_cachedView != null) {
|
|
return _cachedView!;
|
|
} else {
|
|
_cachedView = build();
|
|
return _cachedView!;
|
|
}
|
|
}
|
|
}
|
|
|
|
abstract class ReactiveState<T extends StatefulWidget> extends State<T> {
|
|
Widget? _cachedView;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
receiveProps();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(T oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
receiveProps();
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
// 此处必须重建组件,防止更新字体或者夜间模式切换时组件不刷新
|
|
_cachedView = null;
|
|
}
|
|
|
|
void receiveProps() {}
|
|
|
|
Widget render(BuildContext context) {
|
|
throw Exception("Render is not impleted");
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_cachedView != null) {
|
|
return _cachedView!;
|
|
} else {
|
|
_cachedView = render(context);
|
|
return _cachedView!;
|
|
}
|
|
}
|
|
}
|