Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C++;Windows桌面应用程序中的DLL 我有一些库与FTDI芯片进行交互,我把它封装到C++中的DLL中。 我想创建一个带有flatter的前端,并在windows桌面应用程序中使用该库。 这些功能在颤振中仍然是新的,并且文档非常浅薄,并且是特定于移动的_C++_Flutter_Dart_Ffi_Flutter Desktop - Fatal编程技术网

使用C++;Windows桌面应用程序中的DLL 我有一些库与FTDI芯片进行交互,我把它封装到C++中的DLL中。 我想创建一个带有flatter的前端,并在windows桌面应用程序中使用该库。 这些功能在颤振中仍然是新的,并且文档非常浅薄,并且是特定于移动的

使用C++;Windows桌面应用程序中的DLL 我有一些库与FTDI芯片进行交互,我把它封装到C++中的DLL中。 我想创建一个带有flatter的前端,并在windows桌面应用程序中使用该库。 这些功能在颤振中仍然是新的,并且文档非常浅薄,并且是特定于移动的,c++,flutter,dart,ffi,flutter-desktop,C++,Flutter,Dart,Ffi,Flutter Desktop,按照指南,我用FFI创建了一个插件: import 'dart:ffi'; import 'dart:io'; import 'dart:async'; import 'package:flutter/services.dart'; final DynamicLibrary FT232H = DynamicLibrary.open(""); final int Function() initializeLibrary = FT232H .lookup<Na

按照指南,我用FFI创建了一个插件:

import 'dart:ffi';
import 'dart:io';
import 'dart:async';

import 'package:flutter/services.dart';

final DynamicLibrary FT232H = DynamicLibrary.open("");

final int Function() initializeLibrary = FT232H
    .lookup<NativeFunction<Uint8 Function()>>("initialize_library")
    .asFunction();

final void Function() cleanupLibrary = FT232H
    .lookup<NativeFunction<Void Function()>>("cleanup_library")
    .asFunction();

final int Function() initializeI2C = FT232H
    .lookup<NativeFunction<Uint8 Function()>>("Initialize_I2C")
    .asFunction();

final int Function() closeI2C = FT232H
    .lookup<NativeFunction<Uint8 Function()>>("Close_I2C")
    .asFunction();

final int Function(
        Uint8 slaveAddress, Uint8 registerAddress, Uint32 data, Uint32 numBytes)
    i2cWriteBytes = FT232H
        .lookup<NativeFunction<Uint8 Function(Uint8, Uint8, Uint32, Uint32)>>(
            "I2C_write_bytes")
        .asFunction();

final int Function(Uint8 slaveAddress, Uint8 registerAddress,
        Uint8 bRegisterAddress, Pointer<Uint8> data, Uint32 numBytes)
    i2cReadBytes = FT232H
        .lookup<
            NativeFunction<
                Uint8 Function(Uint8, Uint8, Uint8, Pointer<Uint8>,
                    Uint32)>>("I2C_read_bytes")
        .asFunction();

class DllImport {
  static const MethodChannel _channel = const MethodChannel('dll_import');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }
}
这里我有一些Uint8指针的ISSE,似乎我从Dart代码中得到了这个错误:

The type 'Uint8 Function(Uint8, Uint8, Uint8, Pointer<Uint8>, Uint32)' must be a subtype of 'int 
Function(Uint8, Uint8, Uint8, Pointer<Uint8>, Uint32)' for 'asFunction'.
Try changing one or both of the type arguments.dart(must_be_a_subtype)
类型“Uint8函数(Uint8,Uint8,Uint8,指针,Uint32)”必须是“int”的子类型
函数(Uint8、Uint8、Uint8、指针、Uint32)表示“asFunction”。
尝试更改一个或两个类型参数。dart(必须是子类型)
任何关于如何在颤振中实现这一点的建议都将不胜感激


谢谢。

我确实有一个解决方案,它与颤振桌面嵌入项目中提供的裸骨代码一起工作,我假设您将其用于您的桌面应用程序。你在正确的轨道上,但只需要一些定稿

为了进行测试,我使用这个简单的c代码和几个函数来测试传递指针、返回指针、填充内存、分配和释放。 这是我在dll中使用的C代码

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
__declspec(dllexport) uint8_t* createarray(int32_t size) {
    uint8_t* arr = malloc(size);
    return arr;
}
__declspec(dllexport) void populatearray(uint8_t* arr,uint32_t size){
    for (uint32_t index = 0; index < size; ++index) {
        arr[index] = index & 0xff;
    }
}
__declspec(dllexport) void destroyarray(uint8_t* arr) {
    free(arr);
}
现在,对于我们的代码部分,我们必须为要在dll中调用的每个函数创建一个函数指针,我们需要正确的函数名和propper/正确的参数数

我们需要dart:io和dart:ffi包。dart:io已在代码中,只需导入dart:ffi即可
导入'dart:ffi';//对于外国金融机构

现在,要创建需要加载的库的句柄,需要使用dll的名称调用DynamicLibrary.open。(dll需要放置在dart应用程序的执行路径中,或者需要给定一个绝对路径。执行路径为build/windows/runner/Debug)

final DynamicLibrary nativePointerTestLib=DynamicLibrary.open(“dynamicloadtest.dll”)

我的句柄名为
nativePointerTestLib
,dll的名称为“dynamicloadtest.dll”(是的,我可能应该使用更好的命名约定)

接下来,需要创建每个函数指针。我想调用dll中的三个函数:createarray、populatearray和destroyarray

第一个参数的大小为int->returns pointer to array(指针) 第二个将指针与size->void return一起使用 第三个只接受指针->void return

final Pointer<Uint8> Function(int size) nativeCreateArray =
  nativePointerTestLib
    .lookup<NativeFunction<Pointer<Uint8> Function(Int32)>>("createarray")
    .asFunction();

final void Function(Pointer<Uint8> arr,int size) nativePopulateArray =
  nativePointerTestLib
    .lookup<NativeFunction<Void Function(Pointer<Uint8>, Int32)>>("populatearray")
    .asFunction();

final void Function(Pointer<Uint8> arr) nativeDestroyArray =
  nativePointerTestLib
    .lookup<NativeFunction<Void Function(Pointer<Uint8>)>>("destroyarray")
    .asFunction();
函数调用的新方法

  void _setCounter(int value) {
    setState(() {

      Pointer<Uint8> parray = nativeCreateArray(5);
      nativePopulateArray(parray,5);
      //Now lets print
      print(parray);
      String str= "";
      for(int i = 0 ; i < 5; ++i){
        int val = parray.elementAt(i).value;
        str+=val.toString() +" ";
      }
      print(str);
      nativeDestroyArray(parray);
      _counter = value;
    });
  }
void\u设置计数器(int值){
设置状态(){
指针阵列=nativeCreateArray(5);
当地居民区(帕雷,5);
//现在让我们打印
印刷品(鹦鹉学舌);
字符串str=“”;
对于(int i=0;i<5;++i){
int val=parray.elementAt(i).值;
str+=val.toString()+“”;
}
打印(str);
nativeDestroyArray(帕雷);
_计数器=值;
});
}
我打电话给5号的nativeCreate。dll将为数组分配5个字节

接下来我调用populate,它将在数组的每个元素中插入索引0到4

然后我在数组中循环,获取该数组索引处的每个元素,然后获取值。我将该值赋给一个字符串,最后打印并销毁该数组

最终代码将所有内容放在一起:

// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:io' show Platform;
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:menubar/menubar.dart';
import 'package:window_size/window_size.dart' as window_size;

import 'keyboard_test_page.dart';

import 'dart:ffi'; // For FFI

final DynamicLibrary nativePointerTestLib = DynamicLibrary.open("dynamicloadtest.dll");


final Pointer<Uint8> Function(int size) nativeCreateArray =
  nativePointerTestLib
    .lookup<NativeFunction<Pointer<Uint8> Function(Int32)>>("createarray")
    .asFunction();

final void Function(Pointer<Uint8> arr,int size) nativePopulateArray =
  nativePointerTestLib
    .lookup<NativeFunction<Void Function(Pointer<Uint8>, Int32)>>("populatearray")
    .asFunction();

final void Function(Pointer<Uint8> arr) nativeDestroyArray =
  nativePointerTestLib
    .lookup<NativeFunction<Void Function(Pointer<Uint8>)>>("destroyarray")
    .asFunction();


void main() {
  // Try to resize and reposition the window to be half the width and height
  // of its screen, centered horizontally and shifted up from center.
  WidgetsFlutterBinding.ensureInitialized();
  window_size.getWindowInfo().then((window) {
    final screen = window.screen;
    if (screen != null) {
      final screenFrame = screen.visibleFrame;
      final width = math.max((screenFrame.width / 2).roundToDouble(), 800.0);
      final height = math.max((screenFrame.height / 2).roundToDouble(), 600.0);
      final left = ((screenFrame.width - width) / 2).roundToDouble();
      final top = ((screenFrame.height - height) / 3).roundToDouble();
      final frame = Rect.fromLTWH(left, top, width, height);
      window_size.setWindowFrame(frame);
      window_size.setWindowMinSize(Size(0.8 * width, 0.8 * height));
      window_size.setWindowMaxSize(Size(1.5 * width, 1.5 * height));
      window_size
          .setWindowTitle('Flutter Testbed on ${Platform.operatingSystem}');
    }
  });

  runApp(new MyApp());
}

/// Top level widget for the application.
class MyApp extends StatefulWidget {
  /// Constructs a new app with the given [key].
  const MyApp({Key? key}) : super(key: key);

  @override
  _AppState createState() => new _AppState();
}

class _AppState extends State<MyApp> {
  Color _primaryColor = Colors.blue;
  int _counter = 0;

  static _AppState? of(BuildContext context) =>
      context.findAncestorStateOfType<_AppState>();

  /// Sets the primary color of the app.
  void setPrimaryColor(Color color) {
    setState(() {
      _primaryColor = color;
    });
  }

  void incrementCounter() {
    _setCounter(_counter + 1);
  }

  void _decrementCounter() {
    _setCounter(_counter - 1);
  }

  void _setCounter(int value) {
    setState(() {

      Pointer<Uint8> parray = nativeCreateArray(5);
      nativePopulateArray(parray,5);
      //Now lets print
      print(parray);
      String str= "";
      for(int i = 0 ; i < 5; ++i){
        int val = parray.elementAt(i).value;
        str+=val.toString() +" ";
      }
      print(str);
      nativeDestroyArray(parray);
      _counter = value;
    });
  }

  /// Rebuilds the native menu bar based on the current state.
  void updateMenubar() {
    setApplicationMenu([
      Submenu(label: 'Color', children: [
        MenuItem(
            label: 'Reset',
            enabled: _primaryColor != Colors.blue,
            shortcut: LogicalKeySet(
                LogicalKeyboardKey.meta, LogicalKeyboardKey.backspace),
            onClicked: () {
              setPrimaryColor(Colors.blue);
            }),
        MenuDivider(),
        Submenu(label: 'Presets', children: [
          MenuItem(
              label: 'Red',
              enabled: _primaryColor != Colors.red,
              shortcut: LogicalKeySet(LogicalKeyboardKey.meta,
                  LogicalKeyboardKey.shift, LogicalKeyboardKey.keyR),
              onClicked: () {
                setPrimaryColor(Colors.red);
              }),
          MenuItem(
              label: 'Green',
              enabled: _primaryColor != Colors.green,
              shortcut: LogicalKeySet(LogicalKeyboardKey.meta,
                  LogicalKeyboardKey.alt, LogicalKeyboardKey.keyG),
              onClicked: () {
                setPrimaryColor(Colors.green);
              }),
          MenuItem(
              label: 'Purple',
              enabled: _primaryColor != Colors.deepPurple,
              shortcut: LogicalKeySet(LogicalKeyboardKey.meta,
                  LogicalKeyboardKey.control, LogicalKeyboardKey.keyP),
              onClicked: () {
                setPrimaryColor(Colors.deepPurple);
              }),
        ])
      ]),
      Submenu(label: 'Counter', children: [
        MenuItem(
            label: 'Reset',
            enabled: _counter != 0,
            shortcut: LogicalKeySet(
                LogicalKeyboardKey.meta, LogicalKeyboardKey.digit0),
            onClicked: () {
              _setCounter(0);
            }),
        MenuDivider(),
        MenuItem(
            label: 'Increment',
            shortcut: LogicalKeySet(LogicalKeyboardKey.f2),
            onClicked: incrementCounter),
        MenuItem(
            label: 'Decrement',
            enabled: _counter > 0,
            shortcut: LogicalKeySet(LogicalKeyboardKey.f1),
            onClicked: _decrementCounter),
      ]),
    ]);
  }

  @override
  Widget build(BuildContext context) {
    // Any time the state changes, the menu needs to be rebuilt.
    updateMenubar();

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        primaryColor: _primaryColor,
        accentColor: _primaryColor,
      ),
      darkTheme: ThemeData.dark(),
      home: _MyHomePage(title: 'Flutter Demo Home Page', counter: _counter),
    );
  }
}

class _MyHomePage extends StatelessWidget {
  const _MyHomePage({required this.title, this.counter = 0});

  final String title;
  final int counter;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: LayoutBuilder(
        builder: (context, viewportConstraints) {
          return SingleChildScrollView(
            child: ConstrainedBox(
              constraints:
                  BoxConstraints(minHeight: viewportConstraints.maxHeight),
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    const Text(
                      'You have pushed the button this many times:',
                    ),
                    new Text(
                      '$counter',
                      style: Theme.of(context).textTheme.headline4,
                    ),
                    TextInputTestWidget(),
                    new ElevatedButton(
                      child: new Text('Test raw keyboard events'),
                      onPressed: () {
                        Navigator.of(context).push(new MaterialPageRoute(
                            builder: (context) => KeyboardTestPage()));
                      },
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        width: 380.0,
                        height: 100.0,
                        decoration: BoxDecoration(
                            border: Border.all(color: Colors.grey, width: 1.0)),
                        child: Scrollbar(
                          child: ListView.builder(
                            padding: EdgeInsets.all(8.0),
                            itemExtent: 20.0,
                            itemCount: 50,
                            itemBuilder: (context, index) {
                              return Text('entry $index');
                            },
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _AppState.of(context)!.incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

/// A widget containing controls to test text input.
class TextInputTestWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: const <Widget>[
        SampleTextField(),
        SampleTextField(),
      ],
    );
  }
}

/// A text field with styling suitable for including in a TextInputTestWidget.
class SampleTextField extends StatelessWidget {
  /// Creates a new sample text field.
  const SampleTextField();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200.0,
      padding: const EdgeInsets.all(10.0),
      child: TextField(
        decoration: InputDecoration(border: OutlineInputBorder()),
      ),
    );
  }
}
//版权所有2018谷歌有限责任公司
//
//根据Apache许可证2.0版(以下简称“许可证”)获得许可;
//除非遵守许可证,否则不得使用此文件。
//您可以通过以下方式获得许可证副本:
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//除非适用法律要求或书面同意,软件
//根据许可证进行的分发是按“原样”进行分发的,
//无任何明示或暗示的保证或条件。
//请参阅许可证以了解管理权限和权限的特定语言
//许可证下的限制。
导入“dart:io”展示平台;
导入'dart:math'作为数学;
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
导入“package:menubar/menubar.dart”;
导入“包:窗口大小/窗口大小.省道”作为窗口大小;
导入“键盘测试页面.dart”;
导入“dart:ffi”;//外国金融机构
final DynamicLibrary nativePointerTestLib=DynamicLibrary.open(“dynamicloadtest.dll”);
最终指针函数(int size)nativeCreateArray=
nativePointerTestLib
.lookup(“创建数组”)
.asFunction();
最终空函数(指针arr,int size)nativePopulateArray=
nativePointerTestLib
.lookup(“populatearray”)
.asFunction();
最终空洞函数(指针arr)nativeDestroyArray=
nativePointerTestLib
.lookup(“销毁数组”)
.asFunction();
void main(){
//尝试调整窗口大小并将其重新定位为宽度和高度的一半
//它的屏幕,水平居中,从中心向上移动。
WidgetsFlutterBinding.ensureInitialized();
窗口大小。getWindowInfo()。然后((窗口){
最终屏幕=window.screen;
如果(屏幕!=null){
最终屏幕框=screen.visibleFrame;
最终宽度=math.max((screenFrame.width/2).roundToDouble(),800.0);
最终高度=数学最大值((screenFrame.height/2).roundToDouble(),600.0);
最后左=((screenFrame.width-width)/2.roundToDouble();
最终顶部=((screenFrame.height-height)/3.roundToDouble();
最终帧=垂直于LTWH(左、上、宽、高);
窗口大小。设置窗口框架(框架);
窗口大小。设置窗口大小(大小(0.8*宽度,0.8*高度));
窗口大小。设置窗口最大大小(大小(1.5*宽,1.5*高));
窗口大小
.setWindowTitle(${Platform.operatingSystem}上的颤振试验台);
}
});
runApp(新的MyApp());
}
///应用程序的顶级小部件。
类MyApp扩展了StatefulWidget{
///使用给定的[key]构造一个新应用程序。
常量MyApp({Key?Key}):超级
void _setCounter(int value) {
    setState(() {
      _counter = value;
    });
  }
  void _setCounter(int value) {
    setState(() {

      Pointer<Uint8> parray = nativeCreateArray(5);
      nativePopulateArray(parray,5);
      //Now lets print
      print(parray);
      String str= "";
      for(int i = 0 ; i < 5; ++i){
        int val = parray.elementAt(i).value;
        str+=val.toString() +" ";
      }
      print(str);
      nativeDestroyArray(parray);
      _counter = value;
    });
  }
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:io' show Platform;
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:menubar/menubar.dart';
import 'package:window_size/window_size.dart' as window_size;

import 'keyboard_test_page.dart';

import 'dart:ffi'; // For FFI

final DynamicLibrary nativePointerTestLib = DynamicLibrary.open("dynamicloadtest.dll");


final Pointer<Uint8> Function(int size) nativeCreateArray =
  nativePointerTestLib
    .lookup<NativeFunction<Pointer<Uint8> Function(Int32)>>("createarray")
    .asFunction();

final void Function(Pointer<Uint8> arr,int size) nativePopulateArray =
  nativePointerTestLib
    .lookup<NativeFunction<Void Function(Pointer<Uint8>, Int32)>>("populatearray")
    .asFunction();

final void Function(Pointer<Uint8> arr) nativeDestroyArray =
  nativePointerTestLib
    .lookup<NativeFunction<Void Function(Pointer<Uint8>)>>("destroyarray")
    .asFunction();


void main() {
  // Try to resize and reposition the window to be half the width and height
  // of its screen, centered horizontally and shifted up from center.
  WidgetsFlutterBinding.ensureInitialized();
  window_size.getWindowInfo().then((window) {
    final screen = window.screen;
    if (screen != null) {
      final screenFrame = screen.visibleFrame;
      final width = math.max((screenFrame.width / 2).roundToDouble(), 800.0);
      final height = math.max((screenFrame.height / 2).roundToDouble(), 600.0);
      final left = ((screenFrame.width - width) / 2).roundToDouble();
      final top = ((screenFrame.height - height) / 3).roundToDouble();
      final frame = Rect.fromLTWH(left, top, width, height);
      window_size.setWindowFrame(frame);
      window_size.setWindowMinSize(Size(0.8 * width, 0.8 * height));
      window_size.setWindowMaxSize(Size(1.5 * width, 1.5 * height));
      window_size
          .setWindowTitle('Flutter Testbed on ${Platform.operatingSystem}');
    }
  });

  runApp(new MyApp());
}

/// Top level widget for the application.
class MyApp extends StatefulWidget {
  /// Constructs a new app with the given [key].
  const MyApp({Key? key}) : super(key: key);

  @override
  _AppState createState() => new _AppState();
}

class _AppState extends State<MyApp> {
  Color _primaryColor = Colors.blue;
  int _counter = 0;

  static _AppState? of(BuildContext context) =>
      context.findAncestorStateOfType<_AppState>();

  /// Sets the primary color of the app.
  void setPrimaryColor(Color color) {
    setState(() {
      _primaryColor = color;
    });
  }

  void incrementCounter() {
    _setCounter(_counter + 1);
  }

  void _decrementCounter() {
    _setCounter(_counter - 1);
  }

  void _setCounter(int value) {
    setState(() {

      Pointer<Uint8> parray = nativeCreateArray(5);
      nativePopulateArray(parray,5);
      //Now lets print
      print(parray);
      String str= "";
      for(int i = 0 ; i < 5; ++i){
        int val = parray.elementAt(i).value;
        str+=val.toString() +" ";
      }
      print(str);
      nativeDestroyArray(parray);
      _counter = value;
    });
  }

  /// Rebuilds the native menu bar based on the current state.
  void updateMenubar() {
    setApplicationMenu([
      Submenu(label: 'Color', children: [
        MenuItem(
            label: 'Reset',
            enabled: _primaryColor != Colors.blue,
            shortcut: LogicalKeySet(
                LogicalKeyboardKey.meta, LogicalKeyboardKey.backspace),
            onClicked: () {
              setPrimaryColor(Colors.blue);
            }),
        MenuDivider(),
        Submenu(label: 'Presets', children: [
          MenuItem(
              label: 'Red',
              enabled: _primaryColor != Colors.red,
              shortcut: LogicalKeySet(LogicalKeyboardKey.meta,
                  LogicalKeyboardKey.shift, LogicalKeyboardKey.keyR),
              onClicked: () {
                setPrimaryColor(Colors.red);
              }),
          MenuItem(
              label: 'Green',
              enabled: _primaryColor != Colors.green,
              shortcut: LogicalKeySet(LogicalKeyboardKey.meta,
                  LogicalKeyboardKey.alt, LogicalKeyboardKey.keyG),
              onClicked: () {
                setPrimaryColor(Colors.green);
              }),
          MenuItem(
              label: 'Purple',
              enabled: _primaryColor != Colors.deepPurple,
              shortcut: LogicalKeySet(LogicalKeyboardKey.meta,
                  LogicalKeyboardKey.control, LogicalKeyboardKey.keyP),
              onClicked: () {
                setPrimaryColor(Colors.deepPurple);
              }),
        ])
      ]),
      Submenu(label: 'Counter', children: [
        MenuItem(
            label: 'Reset',
            enabled: _counter != 0,
            shortcut: LogicalKeySet(
                LogicalKeyboardKey.meta, LogicalKeyboardKey.digit0),
            onClicked: () {
              _setCounter(0);
            }),
        MenuDivider(),
        MenuItem(
            label: 'Increment',
            shortcut: LogicalKeySet(LogicalKeyboardKey.f2),
            onClicked: incrementCounter),
        MenuItem(
            label: 'Decrement',
            enabled: _counter > 0,
            shortcut: LogicalKeySet(LogicalKeyboardKey.f1),
            onClicked: _decrementCounter),
      ]),
    ]);
  }

  @override
  Widget build(BuildContext context) {
    // Any time the state changes, the menu needs to be rebuilt.
    updateMenubar();

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        primaryColor: _primaryColor,
        accentColor: _primaryColor,
      ),
      darkTheme: ThemeData.dark(),
      home: _MyHomePage(title: 'Flutter Demo Home Page', counter: _counter),
    );
  }
}

class _MyHomePage extends StatelessWidget {
  const _MyHomePage({required this.title, this.counter = 0});

  final String title;
  final int counter;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: LayoutBuilder(
        builder: (context, viewportConstraints) {
          return SingleChildScrollView(
            child: ConstrainedBox(
              constraints:
                  BoxConstraints(minHeight: viewportConstraints.maxHeight),
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    const Text(
                      'You have pushed the button this many times:',
                    ),
                    new Text(
                      '$counter',
                      style: Theme.of(context).textTheme.headline4,
                    ),
                    TextInputTestWidget(),
                    new ElevatedButton(
                      child: new Text('Test raw keyboard events'),
                      onPressed: () {
                        Navigator.of(context).push(new MaterialPageRoute(
                            builder: (context) => KeyboardTestPage()));
                      },
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        width: 380.0,
                        height: 100.0,
                        decoration: BoxDecoration(
                            border: Border.all(color: Colors.grey, width: 1.0)),
                        child: Scrollbar(
                          child: ListView.builder(
                            padding: EdgeInsets.all(8.0),
                            itemExtent: 20.0,
                            itemCount: 50,
                            itemBuilder: (context, index) {
                              return Text('entry $index');
                            },
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _AppState.of(context)!.incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

/// A widget containing controls to test text input.
class TextInputTestWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: const <Widget>[
        SampleTextField(),
        SampleTextField(),
      ],
    );
  }
}

/// A text field with styling suitable for including in a TextInputTestWidget.
class SampleTextField extends StatelessWidget {
  /// Creates a new sample text field.
  const SampleTextField();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200.0,
      padding: const EdgeInsets.all(10.0),
      child: TextField(
        decoration: InputDecoration(border: OutlineInputBorder()),
      ),
    );
  }
}