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 extends State { 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!; } } }