"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/dev/integration_tests/android_views/lib/wm_integrations.dart" (13 Nov 2020, 4523 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'dart:io';
    6 import 'dart:ui';
    7 
    8 import 'package:flutter/material.dart';
    9 import 'package:flutter/services.dart';
   10 import 'package:flutter/widgets.dart';
   11 
   12 import 'page.dart';
   13 
   14 class WindowManagerIntegrationsPage extends PageWidget {
   15   const WindowManagerIntegrationsPage()
   16       : super('Window Manager Integrations Tests', const ValueKey<String>('WmIntegrationsListTile'));
   17 
   18   @override
   19   Widget build(BuildContext context) => WindowManagerBody();
   20 }
   21 
   22 class WindowManagerBody extends StatefulWidget {
   23   @override
   24   State<WindowManagerBody> createState() => WindowManagerBodyState();
   25 }
   26 
   27 enum _LastTestStatus {
   28   pending,
   29   success,
   30   error
   31 }
   32 
   33 class WindowManagerBodyState extends State<WindowManagerBody> {
   34 
   35   MethodChannel viewChannel;
   36   _LastTestStatus lastTestStatus = _LastTestStatus.pending;
   37   String lastError;
   38   int id;
   39   int windowClickCount = 0;
   40 
   41   @override
   42   Widget build(BuildContext context) {
   43     return Scaffold(
   44       appBar: AppBar(
   45         title: const Text('Window Manager Integrations'),
   46       ),
   47       body: Column(
   48         children: <Widget>[
   49           SizedBox(
   50             height: 300,
   51             child: AndroidView(
   52               viewType: 'simple_view',
   53               onPlatformViewCreated: onPlatformViewCreated,
   54             ),
   55           ),
   56           if (lastTestStatus != _LastTestStatus.pending) _statusWidget(),
   57           if (viewChannel != null) ... <Widget>[
   58             ElevatedButton(
   59               key: const ValueKey<String>('ShowAlertDialog'),
   60               child: const Text('SHOW ALERT DIALOG'),
   61               onPressed: onShowAlertDialogPressed,
   62             ),
   63             Row(
   64               children: <Widget>[
   65                 ElevatedButton(
   66                   key: const ValueKey<String>('AddWindow'),
   67                   child: const Text('ADD WINDOW'),
   68                   onPressed: onAddWindowPressed,
   69                 ),
   70                 ElevatedButton(
   71                   key: const ValueKey<String>('TapWindow'),
   72                   child: const Text('TAP WINDOW'),
   73                   onPressed: onTapWindowPressed,
   74                 ),
   75                 if (windowClickCount > 0)
   76                   Text(
   77                       'Click count: $windowClickCount',
   78                       key: const ValueKey<String>('WindowClickCount'),
   79                   ),
   80               ],
   81             ),
   82           ],
   83         ],
   84       ),
   85     );
   86   }
   87 
   88   Widget _statusWidget() {
   89     assert(lastTestStatus != _LastTestStatus.pending);
   90     final String message = lastTestStatus == _LastTestStatus.success ? 'Success' : lastError;
   91     return Container(
   92       color: lastTestStatus == _LastTestStatus.success ? Colors.green : Colors.red,
   93       child: Text(
   94         message,
   95         key: const ValueKey<String>('Status'),
   96         style: TextStyle(
   97           color: lastTestStatus == _LastTestStatus.error ? Colors.yellow : null,
   98         ),
   99       ),
  100     );
  101   }
  102 
  103   Future<void> onShowAlertDialogPressed() async {
  104     if (lastTestStatus != _LastTestStatus.pending) {
  105       setState(() {
  106         lastTestStatus = _LastTestStatus.pending;
  107       });
  108     }
  109     try {
  110       await viewChannel.invokeMethod<void>('showAndHideAlertDialog');
  111       setState(() {
  112         lastTestStatus = _LastTestStatus.success;
  113       });
  114     } catch(e) {
  115       setState(() {
  116         lastTestStatus = _LastTestStatus.error;
  117         lastError = '$e';
  118       });
  119     }
  120   }
  121 
  122   Future<void> onAddWindowPressed() async {
  123     try {
  124       await viewChannel.invokeMethod<void>('addWindowAndWaitForClick');
  125       setState(() {
  126         windowClickCount++;
  127       });
  128     } catch(e) {
  129       setState(() {
  130         lastTestStatus = _LastTestStatus.error;
  131         lastError = '$e';
  132       });
  133     }
  134   }
  135 
  136   Future<void> onTapWindowPressed() async {
  137     await Future<void>.delayed(const Duration(seconds: 1));
  138 
  139     // Dispatch a tap event on the child view inside the platform view.
  140     //
  141     // Android mutates `MotionEvent` instances, so in this case *do not* dispatch
  142     // new instances as it won't cover the `MotionEventTracker` class in the embedding
  143     // which tracks events.
  144     //
  145     // See the issue this prevents: https://github.com/flutter/flutter/issues/61169
  146     await Process.run('input', const <String>['tap', '250', '550']);
  147   }
  148 
  149   void onPlatformViewCreated(int id) {
  150     this.id = id;
  151     setState(() {
  152       viewChannel = MethodChannel('simple_view/$id');
  153     });
  154   }
  155 
  156 }