本文最后更新于 2021年4月4日 晚上
看到一篇国外的文章实现了一个 Visibility 效果的 Widget, 故记录下来.
Flutter 的一个视图可见性控件实现
参考这篇文章.
可以实现四种类型的可见性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import 'package:flutter/widgets.dart'; import 'package:meta/meta.dart';
enum VisibilityFlag { visible, invisible, offscreen, gone, }
class Visibility extends StatelessWidget { final VisibilityFlag visibility; final Widget child; final Widget removedChild;
Visibility({ @required this.child, @required this.visibility, }) : this.removedChild = Container();
@override Widget build(BuildContext context) { if (visibility == VisibilityFlag.visible) { return child; } else if (visibility == VisibilityFlag.invisible) { return new IgnorePointer( ignoring: true, child: new Opacity( opacity: 0.0, child: child, ), ); } else if (visibility == VisibilityFlag.offscreen) { return new Offstage( offstage: true, child: child, ); } else { return removedChild; } } }
|
果然想象力很重要.