forked from flutter/assets-for-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgesture_detector.dart
107 lines (88 loc) · 3.14 KB
/
gesture_detector.dart
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:io';
import 'package:diagram_capture/diagram_capture.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'diagram_step.dart';
const String _gestureDetector = 'gesture_detector';
const Duration _pauseDuration = Duration(seconds: 1);
final Duration _totalDuration = _pauseDuration + _pauseDuration;
final GlobalKey _gestureDetectorKey = GlobalKey();
class GestureDetectorDiagram extends StatefulWidget implements DiagramMetadata {
const GestureDetectorDiagram(this.name);
@override
final String name;
@override
_GestureDetectorDiagramState createState() => _GestureDetectorDiagramState();
}
class _GestureDetectorDiagramState extends State<GestureDetectorDiagram> {
bool _lights = false;
@override
Widget build(BuildContext context) {
return ConstrainedBox(
key: UniqueKey(),
constraints: BoxConstraints.tight(const Size(200, 150)),
child: Container(
alignment: FractionalOffset.center,
color: Colors.white,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(
Icons.lightbulb_outline,
color: _lights ? Colors.yellow.shade600 : Colors.black,
size: 60,
),
),
GestureDetector(
key: _gestureDetectorKey,
onTap: () {
setState(() {
_lights = true;
});
},
child: Container(
color: Colors.yellow.shade600,
padding: const EdgeInsets.all(8),
child: const Text('TURN LIGHTS ON'),
),
),
],
),
),
);
}
}
class GestureDetectorDiagramStep extends DiagramStep<GestureDetectorDiagram> {
GestureDetectorDiagramStep(DiagramController controller) : super(controller);
@override
final String category = 'widgets';
@override
Future<List<GestureDetectorDiagram>> get diagrams async =>
<GestureDetectorDiagram>[
const GestureDetectorDiagram(_gestureDetector),
];
@override
Future<File> generateDiagram(GestureDetectorDiagram diagram) async {
controller.builder = (BuildContext context) => diagram;
controller.advanceTime(Duration.zero);
final Future<File> result = controller.drawAnimatedDiagramToFiles(
end: _totalDuration,
frameRate: 60,
name: diagram.name,
category: category,
);
await Future<void>.delayed(_pauseDuration);
final RenderBox target = _gestureDetectorKey.currentContext!.findRenderObject() as RenderBox;
final Offset targetOffset = target.localToGlobal(target.size.center(Offset.zero));
final TestGesture gesture = await controller.startGesture(targetOffset);
await gesture.up();
await Future<void>.delayed(_pauseDuration);
return result;
}
}