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
// Copyright 2014 The Flutter 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:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'page.dart';
class WindowManagerIntegrationsPage extends PageWidget {
const WindowManagerIntegrationsPage()
: super('Window Manager Integrations Tests', const ValueKey<String>('WmIntegrationsListTile'));
@override
Widget build(BuildContext context) => WindowManagerBody();
}
class WindowManagerBody extends StatefulWidget {
@override
State<WindowManagerBody> createState() => WindowManagerBodyState();
}
enum _LastTestStatus {
pending,
success,
error
}
class WindowManagerBodyState extends State<WindowManagerBody> {
MethodChannel viewChannel;
_LastTestStatus lastTestStatus = _LastTestStatus.pending;
String lastError;
int id;
int windowClickCount = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Window Manager Integrations'),
),
body: Column(
children: <Widget>[
SizedBox(
height: 300,
child: AndroidView(
viewType: 'simple_view',
onPlatformViewCreated: onPlatformViewCreated,
),
),
if (lastTestStatus != _LastTestStatus.pending) _statusWidget(),
if (viewChannel != null) ... <Widget>[
ElevatedButton(
key: const ValueKey<String>('ShowAlertDialog'),
child: const Text('SHOW ALERT DIALOG'),
onPressed: onShowAlertDialogPressed,
),
Row(
children: <Widget>[
ElevatedButton(
key: const ValueKey<String>('AddWindow'),
child: const Text('ADD WINDOW'),
onPressed: onAddWindowPressed,
),
ElevatedButton(
key: const ValueKey<String>('TapWindow'),
child: const Text('TAP WINDOW'),
onPressed: onTapWindowPressed,
),
if (windowClickCount > 0)
Text(
'Click count: $windowClickCount',
key: const ValueKey<String>('WindowClickCount'),
),
],
),
],
],
),
);
}
Widget _statusWidget() {
assert(lastTestStatus != _LastTestStatus.pending);
final String message = lastTestStatus == _LastTestStatus.success ? 'Success' : lastError;
return Container(
color: lastTestStatus == _LastTestStatus.success ? Colors.green : Colors.red,
child: Text(
message,
key: const ValueKey<String>('Status'),
style: TextStyle(
color: lastTestStatus == _LastTestStatus.error ? Colors.yellow : null,
),
),
);
}
Future<void> onShowAlertDialogPressed() async {
if (lastTestStatus != _LastTestStatus.pending) {
setState(() {
lastTestStatus = _LastTestStatus.pending;
});
}
try {
await viewChannel.invokeMethod<void>('showAndHideAlertDialog');
setState(() {
lastTestStatus = _LastTestStatus.success;
});
} catch(e) {
setState(() {
lastTestStatus = _LastTestStatus.error;
lastError = '$e';
});
}
}
Future<void> onAddWindowPressed() async {
try {
await viewChannel.invokeMethod<void>('addWindowAndWaitForClick');
setState(() {
windowClickCount++;
});
} catch(e) {
setState(() {
lastTestStatus = _LastTestStatus.error;
lastError = '$e';
});
}
}
Future<void> onTapWindowPressed() async {
await Future<void>.delayed(const Duration(seconds: 1));
// Dispatch a tap event on the child view inside the platform view.
//
// Android mutates `MotionEvent` instances, so in this case *do not* dispatch
// new instances as it won't cover the `MotionEventTracker` class in the embedding
// which tracks events.
//
// See the issue this prevents: https://github.com/flutter/flutter/issues/61169
await Process.run('input', const <String>['tap', '250', '550']);
}
void onPlatformViewCreated(int id) {
this.id = id;
setState(() {
viewChannel = MethodChannel('simple_view/$id');
});
}
}