Flutter:FocusScope 在发布模式下不工作

时间:2021-07-04 07:30:16

标签: flutter flutter-desktop

我正在创建一个 Flutter 桌面应用程序,并且在 Release 模式下 FocusScope 没有检测到键盘事件。在调试模式下一切正常。知道我怎样才能让它正常工作吗?

也许我可以给我的客户调试文件夹,因为它工作正常,而不是发布。除了大小之外还有其他显着差异吗?由于两个版本的加载速度都很快,我似乎无法识别出任何值得注意的东西。

我的代码如下:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(

      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  int counter = 0;
  late FocusNode _focusNode;

  @override
  dispose() {
    _focusNode.dispose();
  }

  @override
  void initState() {
    _focusNode = FocusNode();
  }

    @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("Test App"),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(

          children: <Widget>[
            Text(
              counter.toString(),
            ),
            FocusScope(
                child: TextField(),
                onKey: (_focusNode, event) {
                  if (event.toString().contains('RawKeyDownEvent') && event.toString().contains('Arrow Up')) {
                    print(event.toString());
                    setState(() {
                      counter--;
                    }
                    );
                    return KeyEventResult.handled;
                  }
                  if (event.toString().contains('RawKeyDownEvent') && event.toString().contains('Arrow Down')) {
                    print(event.toString());
                    setState(() {
                      counter++;
                    }
                    );
                    return KeyEventResult.handled;
                  }
                  if (event.toString().contains('RawKeyDownEvent') && event.toString().contains('Arrow Up')) {
                    print(event.toString());
                    setState(() {
                      counter--;
                    }
                    );
                    return KeyEventResult.handled;
                  }
                  return KeyEventResult.ignored;
                }

            ),

          ],
        ),
      ),
    );
  }
}

0 个答案:

没有答案