forked from flutter/assets-for-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_dialog.dart
164 lines (144 loc) · 4.62 KB
/
simple_dialog.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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 Duration _pauseDuration = Duration(seconds: 1);
const Duration _openDuration = Duration(milliseconds: 300);
final Duration _totalDuration = _pauseDuration +
_pauseDuration +
_openDuration +
_pauseDuration +
_openDuration +
_pauseDuration;
final GlobalKey _openDialogKey = GlobalKey();
final GlobalKey _treasuryKey = GlobalKey();
final GlobalKey _stateKey = GlobalKey();
class SimpleDialogDiagram extends StatelessWidget implements DiagramMetadata {
const SimpleDialogDiagram(this.name);
@override
final String name;
@override
Widget build(BuildContext context) {
return ConstrainedBox(
key: UniqueKey(),
constraints: BoxConstraints.tight(const Size(350, 622)),
child: Navigator(
initialRoute: '/',
onGenerateRoute: (RouteSettings settings) {
return PageRouteBuilder<void>(
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return Scaffold(
appBar: AppBar(
title: const Text('SimpleDialog Demo'),
),
body: Center(
child: Builder(
builder: (BuildContext context) {
return OutlinedButton(
key: _openDialogKey,
child: const Text('Show Options'),
onPressed: () => _askedToLead(context),
);
},
),
),
);
},
);
},
),
);
}
Future<void> _askedToLead(BuildContext context) async {
final Department result = (await showDialog<Department>(
context: context,
useRootNavigator: false,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text('Select assignment'),
children: <Widget>[
SimpleDialogOption(
key: _treasuryKey,
onPressed: () {
Navigator.pop<Department>(context, Department.treasury);
},
child: const Text('Treasury department'),
),
SimpleDialogOption(
key: _stateKey,
onPressed: () {
Navigator.pop<Department>(context, Department.state);
},
child: const Text('State department'),
),
],
);
},
))!;
switch (result) {
case Department.treasury:
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Treasury')),
);
break;
case Department.state:
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('State')),
);
break;
}
}
}
enum Department {
treasury,
state,
}
class SimpleDialogDiagramStep extends DiagramStep<SimpleDialogDiagram> {
SimpleDialogDiagramStep(DiagramController controller) : super(controller);
@override
final String category = 'material';
@override
Future<List<SimpleDialogDiagram>> get diagrams async => <SimpleDialogDiagram>[
const SimpleDialogDiagram('simple_dialog'),
];
@override
Future<File> generateDiagram(SimpleDialogDiagram 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 _pause();
await _tap(_openDialogKey);
await _pause();
await _pause();
await _pause();
await _tap(_treasuryKey);
await _pause();
return result;
}
Future<void> _tap(GlobalKey key) async {
final RenderBox target = key.currentContext!.findRenderObject() as RenderBox;
final Offset targetOffset =
target.localToGlobal(target.size.center(Offset.zero));
final TestGesture gesture = await controller.startGesture(targetOffset);
await Future<void>.delayed(_pauseDuration);
await gesture.up();
await Future<void>.delayed(_openDuration);
}
Future<void> _pause() async {
await Future<void>.delayed(_pauseDuration);
}
}