-
-
Save chunhtai/abb5fea62500a3ced1074cc241d66825 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:ui' as ui; | |
| import 'package:flutter/rendering.dart'; | |
| import 'package:flutter/widgets.dart'; | |
| class OffscreenCachedWidget extends StatefulWidget { | |
| const OffscreenCachedWidget({super.key, required this.child}); | |
| final Widget child; | |
| @override | |
| State<OffscreenCachedWidget> createState() => _OffscreenCachedWidgetState(); | |
| } | |
| class _OffscreenCachedWidgetState extends State<OffscreenCachedWidget> { | |
| late final PipelineOwner _offscreenPipelineOwner; | |
| late final BuildOwner _offscreenBuildOwner; | |
| RenderObjectToWidgetElement<RenderBox>? _rootElement; | |
| RenderRepaintBoundary? _offscreenRoot; | |
| ui.Image? _cachedImage; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _offscreenBuildOwner = BuildOwner(focusManager: FocusManager()); | |
| _offscreenPipelineOwner = PipelineOwner( | |
| onNeedVisualUpdate: () { | |
| // Trigger a rebuild or rasterization when the offscreen subtree dirties. | |
| _rasterizeOffscreen(); | |
| }, | |
| ); | |
| } | |
| @override | |
| void didChangeDependencies() { | |
| super.didChangeDependencies(); | |
| // Adopt the offscreen pipeline owner as a child of the ambient view's owner. | |
| final parentOwner = View.pipelineOwnerOf(context); | |
| parentOwner.adoptChild(_offscreenPipelineOwner); | |
| _setupOffscreenTree(); | |
| } | |
| void _setupOffscreenTree() { | |
| _offscreenRoot = RenderRepaintBoundary( | |
| child: RenderConstrainedBox( | |
| additionalConstraints: const BoxConstraints.tightFor(width: 200, height: 200), | |
| ), | |
| ); | |
| _offscreenPipelineOwner.rootNode = _offscreenRoot; | |
| _rootElement = RenderObjectToWidgetAdapter<RenderBox>( | |
| container: _offscreenRoot!, | |
| child: widget.child, | |
| ).attachToRenderTree(_offscreenBuildOwner, _rootElement); | |
| _rasterizeOffscreen(); | |
| } | |
| void _rasterizeOffscreen() { | |
| _offscreenBuildOwner.buildScope(_rootElement!); | |
| _offscreenPipelineOwner.flushLayout(); | |
| _offscreenPipelineOwner.flushCompositingBits(); | |
| _offscreenPipelineOwner.flushPaint(); | |
| _offscreenBuildOwner.finalizeTree(); | |
| final image = _offscreenRoot?.toImageSync(pixelRatio: 1.0); | |
| if (mounted && image != null) { | |
| setState(() { | |
| _cachedImage = image; | |
| }); | |
| } | |
| } | |
| @override | |
| void dispose() { | |
| final parentOwner = View.pipelineOwnerOf(context); | |
| parentOwner.dropChild(_offscreenPipelineOwner); | |
| _offscreenPipelineOwner.rootNode = null; | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| if (_cachedImage == null) { | |
| return const SizedBox(width: 200, height: 200); | |
| } | |
| return RawImage(image: _cachedImage); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment