Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import 'dart:math';
  2. import 'dart:ui' as ui;
  3.  
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/rendering.dart';
  7. import 'package:flutter/widgets.dart';
  8.  
  9. void main() {
  10. debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
  11. runApp(MyApp());
  12. }
  13.  
  14.  
  15. class MyApp extends StatelessWidget {
  16. @override
  17. Widget build(BuildContext context) {
  18. return MaterialApp(
  19. initialRoute: '/',
  20. onGenerateRoute: (settings) => _generateRoute(context, settings),
  21. );
  22. }
  23.  
  24. Route _generateRoute(BuildContext context, RouteSettings settings) {
  25. return PageRouteBuilder(
  26. settings: settings,
  27. pageBuilder: (ctx, a1, a2) {
  28. // print('pageBuilder $a1');
  29. return _buildPage(ctx, a2, settings);
  30. },
  31. transitionsBuilder: (ctx, a1, a2, child) {
  32. return Opacity(
  33. opacity: a1.value,
  34. child: FractionalTranslation(
  35. translation: Offset(0, (1 - a1.value)),
  36. child: child,
  37. ),
  38. );
  39. },
  40. transitionDuration: Duration(milliseconds: 750),
  41. );
  42. }
  43.  
  44. Widget _buildPage(BuildContext context, Animation<double> animation, RouteSettings settings) {
  45. if (settings.name == '/')
  46. return Curtain(animation: animation, child: MyPage());
  47. if (settings.name == 'level2')
  48. return Container(
  49. color: Colors.green,
  50. child: OutlineButton(
  51. onPressed: () => Navigator.of(context).pop(),
  52. child: Text('back', textScaleFactor: 3, ),
  53. ),
  54. );
  55. throw 'no route: ${settings.name}';
  56. }
  57. }
  58.  
  59. class MyPage extends StatelessWidget {
  60. @override
  61. Widget build(BuildContext context) {
  62. return Scaffold(
  63. body: Container(
  64. decoration: FlutterLogoDecoration(style: FlutterLogoStyle.horizontal),
  65. child: Center(
  66. child: Container(
  67. width: 200,
  68. height: 100,
  69. color: Colors.deepPurple,
  70. child: FlatButton(
  71. onPressed: () => Navigator.of(context).pushNamed('level2'),
  72. child: Text('press me', textScaleFactor: 3, textAlign: TextAlign.center,),
  73. ),
  74. ),
  75. ),
  76. ),
  77. );
  78. }
  79. }
  80.  
  81. ///------------------------------------------------------
  82. ///------------------------------------------------------
  83.  
  84. class Curtain extends SingleChildRenderObjectWidget {
  85. final Animation<double> animation;
  86. const Curtain({
  87. Key key,
  88. this.animation,
  89. Widget child,
  90. }) : super(key: key, child: child);
  91.  
  92. @override
  93. RenderObject createRenderObject(BuildContext context) => RenderCurtain(animation);
  94. }
  95.  
  96. class RenderCurtain extends RenderProxyBox {
  97. Animation<double> animation;
  98. ui.Image image;
  99. Paint p = Paint();
  100. List offsets;
  101.  
  102. RenderCurtain(this.animation) {
  103. animation.addListener(markNeedsPaint);
  104. animation.addStatusListener(_statusListener);
  105. var random = Random();
  106. offsets = List.generate(64, (_) => -random.nextDouble() / 4);
  107. }
  108.  
  109. @override
  110. bool get isRepaintBoundary => true;
  111.  
  112. _statusListener(AnimationStatus status) async {
  113. if (status == AnimationStatus.forward) {
  114. print('take off-screen image');
  115. OffsetLayer offsetLayer = layer;
  116. image = await offsetLayer.toImage(Offset.zero & size);
  117. }
  118. }
  119.  
  120. @override
  121. void paint(PaintingContext context, Offset offset) {
  122. if (animation.status == AnimationStatus.forward && image != null) {
  123. // print('paint: $animation');
  124. context.canvas.drawRect(offset & size, p..color = Colors.grey);
  125. var i = 0;
  126. offsets.forEach((offset) {
  127. var src = Rect.fromLTWH(size.width * i / offsets.length , 0, size.width / offsets.length, size.height);
  128. var dst = src;
  129. var effectifeOffset = animation.value + offset;
  130. if (effectifeOffset > 0)
  131. dst = src.translate(0, effectifeOffset * size.height);
  132. context.canvas.drawImageRect(image, src, dst, p);
  133. i++;
  134. });
  135. }
  136. else
  137. super.paint(context, offset);
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement