Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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
Flutter 如何在颤振中设置材料按钮的焦点_Flutter_Dart - Fatal编程技术网

Flutter 如何在颤振中设置材料按钮的焦点

Flutter 如何在颤振中设置材料按钮的焦点,flutter,dart,Flutter,Dart,我希望焦点在“材质”按钮上,这样我就可以按enter键或单击按钮来创建项目 final FocusNode _createButtonFocusNode = new FocusNode(); @override void initState() { FocusScope.of(context).requestFocus(_createButtonFocusNode); super.initState(); } RawKeyboardListener(

我希望焦点在“材质”按钮上,这样我就可以按enter键或单击按钮来创建项目

final FocusNode _createButtonFocusNode = new FocusNode();

@override
void initState() {
   FocusScope.of(context).requestFocus(_createButtonFocusNode);
   super.initState();
}
 RawKeyboardListener(
                focusNode: _createButtonFocusNode,
                onKey: (RawKeyEvent event) {
                  if (event.logicalKey == LogicalKeyboardKey.enter) {
                    _createItem();
                  }
                },
child:RaisedButton(focusNode: _createButtonFocusNode,
                 onPressed: () {
                      _createItem();
                    },
                    child: Text("Create"))))

还假设存在一个带有_cancelItem事件的取消物料按钮,该按钮应该能够接受焦点上的enter键

您可以复制下面的粘贴运行完整代码
您可以使用
\u node.requestFocus()
请求焦点,并使用
FocusAttachment
attach

在演示代码中,当接收
时,输入
将更改按钮颜色,请参见下面的工作演示
代码片段

_node.requestFocus();
...
FocusAttachment _nodeAttachment;
 _nodeAttachment = _node.attach(context, onKey: _handleKeyPress);
...
 bool _handleKeyPress(FocusNode node, RawKeyEvent event) {
    if (event is RawKeyDownEvent) {
      print('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
      if (event.logicalKey == LogicalKeyboardKey.enter) {
        print('clicked enter');
        setState(() {
          _color = Colors.deepPurple;
        });
        return true;
      }
    }
    return false;
  }
工作演示

完整代码

// Flutter code sample for FocusNode

// This example shows how a FocusNode should be managed if not using the
// [Focus] or [FocusScope] widgets. See the [Focus] widget for a similar
// example using [Focus] and [FocusScope] widgets.

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: MyStatelessWidget(),
      ),
    );
  }
}

class CustomButton extends StatefulWidget {
  FocusNode focusNode;
  CustomButton({Key key, this.focusNode}) : super(key: key);

  @override
  _CustomButtonState createState() => _CustomButtonState();
}

class _CustomButtonState extends State<CustomButton> {
  bool _focused = false;
  FocusAttachment _nodeAttachment;
  Color _color = Colors.white;

  @override
  void initState() {
    super.initState();
    //widget.focusNode = FocusNode(debugLabel: 'Button');
    widget.focusNode.addListener(_handleFocusChange);
    _nodeAttachment = widget.focusNode.attach(context, onKey: _handleKeyPress);
  }

  void _handleFocusChange() {
    print(widget.focusNode.hasFocus);
    if (widget.focusNode.hasFocus != _focused) {
      setState(() {
        _focused = widget.focusNode.hasFocus;
        _color = Colors.white;
      });
    }
  }

  bool _handleKeyPress(FocusNode node, RawKeyEvent event) {
    if (event is RawKeyDownEvent) {
      print('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
      if (event.logicalKey == LogicalKeyboardKey.enter) {
        print('clicked enter');
        setState(() {
          _color = Colors.deepPurple;
        });
        return true;
      }
    }
    return false;
  }

  @override
  void dispose() {
    widget.focusNode.removeListener(_handleFocusChange);
    // The attachment will automatically be detached in dispose().
    widget.focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _nodeAttachment.reparent();
    return Center(
      child: RaisedButton(
        focusNode: widget.focusNode,
        color: _focused ? _color : Colors.white,
        child: Text(_focused ? "focused" : 'Not focus'),
        onPressed: () {
          print("create item");
        },
      ),
    );
  }
}

class MyStatelessWidget extends StatefulWidget {
  MyStatelessWidget({Key key}) : super(key: key);

  @override
  _MyStatelessWidgetState createState() => _MyStatelessWidgetState();
}

class _MyStatelessWidgetState extends State<MyStatelessWidget> {
  FocusNode _node1 = FocusNode();
  FocusNode _node2 = FocusNode();
  @override
  Widget build(BuildContext context) {
    final TextTheme textTheme = Theme.of(context).textTheme;
    return DefaultTextStyle(
      style: textTheme.headline4,
      child: Column(
        children: [
          CustomButton(
            focusNode: _node1,
          ),
          CustomButton(
            focusNode: _node2,
          ),
          RaisedButton(
              onPressed: () {
                _node1.requestFocus();
                setState(() {});
              },
              child: Text("request focus button 1")),
          RaisedButton(
              onPressed: () {
                _node2.requestFocus();
                setState(() {});
              },
              child: Text("request focus button 2")),
        ],
      ),
    );
  }
}
//FocusNode的颤振代码示例
//此示例显示了如果不使用
//[Focus]或[FocusScope]小部件。请参阅[Focus]小部件以了解类似的信息
//使用[Focus]和[FocusScope]小部件的示例。
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
void main()=>runApp(MyApp());
///此小部件是主应用程序小部件。
类MyApp扩展了无状态小部件{
静态常量字符串_title='颤振代码示例';
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:_标题,
家:脚手架(
appBar:appBar(标题:常量文本(_title)),
正文:MyStatelessWidget(),
),
);
}
}
类CustomButton扩展StatefulWidget{
焦点节点;
CustomButton({Key-Key,this.focusNode}):super(Key:Key);
@凌驾
_CustomButtonState createState()=>\u CustomButtonState();
}
类_CustomButtonState扩展状态{
bool_focused=false;
聚焦附件;
颜色_Color=Colors.white;
@凌驾
void initState(){
super.initState();
//widget.focusNode=focusNode(debugLabel:'Button');
widget.focusNode.addListener(_handleFocusChange);
_nodeAttachment=widget.focusNode.attach(上下文,onKey:_handleKeyPress);
}
void_handleFocusChange(){
打印(widget.focusNode.hasFocus);
if(widget.focusNode.hasFocus!=\u聚焦){
设置状态(){
_聚焦=widget.focusNode.hasFocus;
_颜色=颜色。白色;
});
}
}
bool\u handleKeyPress(焦点节点,RawKeyEvent事件){
if(事件为RawKeyDownEvent){
print('Focus node${node.debugLabel}已获取密钥事件:${event.logicalKey}');
if(event.logicalKey==LogicalKeyboardKey.enter){
打印(“点击输入”);
设置状态(){
_颜色=颜色。深紫色;
});
返回true;
}
}
返回false;
}
@凌驾
无效处置(){
widget.focusNode.removeListener(_handleFocusChange);
//附件将在dispose()中自动分离。
widget.focusNode.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
_nodeAttachment.reparent();
返回中心(
孩子:升起按钮(
focusNode:widget.focusNode,
颜色:_聚焦?_颜色:颜色。白色,
子项:文本(_focused?“focused”:“Not focus”),
已按下:(){
打印(“创建项目”);
},
),
);
}
}
类MyStatelessWidget扩展了StatefulWidget{
MyStatelessWidget({Key}):super(Key:Key);
@凌驾
_MyStatelessWidgetState createState()=>MyStatelessWidgetState();
}
类_MyStatelessWidgetState扩展状态{
FocusNode_node1=FocusNode();
FocusNode_node2=FocusNode();
@凌驾
小部件构建(构建上下文){
final TextTheme TextTheme=Theme.of(context).TextTheme;
返回DefaultTextStyle(
风格:textTheme.headline4,
子:列(
儿童:[
自定义按钮(
焦点节点:_节点1,
),
自定义按钮(
焦点节点:_节点2,
),
升起的按钮(
已按下:(){
_node1.requestFocus();
setState((){});
},
子项:文本(“请求焦点按钮1”),
升起的按钮(
已按下:(){
_node2.requestFocus();
setState((){});
},
子项:文本(“请求焦点按钮2”),
],
),
);
}
}

您可以复制粘贴运行下面的完整代码
您可以使用
\u node.requestFocus()
请求焦点,并使用
FocusAttachment
attach

在演示代码中,当接收
时,输入
将更改按钮颜色,请参见下面的工作演示
代码片段

_node.requestFocus();
...
FocusAttachment _nodeAttachment;
 _nodeAttachment = _node.attach(context, onKey: _handleKeyPress);
...
 bool _handleKeyPress(FocusNode node, RawKeyEvent event) {
    if (event is RawKeyDownEvent) {
      print('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
      if (event.logicalKey == LogicalKeyboardKey.enter) {
        print('clicked enter');
        setState(() {
          _color = Colors.deepPurple;
        });
        return true;
      }
    }
    return false;
  }
工作演示

完整代码

// Flutter code sample for FocusNode

// This example shows how a FocusNode should be managed if not using the
// [Focus] or [FocusScope] widgets. See the [Focus] widget for a similar
// example using [Focus] and [FocusScope] widgets.

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: MyStatelessWidget(),
      ),
    );
  }
}

class CustomButton extends StatefulWidget {
  FocusNode focusNode;
  CustomButton({Key key, this.focusNode}) : super(key: key);

  @override
  _CustomButtonState createState() => _CustomButtonState();
}

class _CustomButtonState extends State<CustomButton> {
  bool _focused = false;
  FocusAttachment _nodeAttachment;
  Color _color = Colors.white;

  @override
  void initState() {
    super.initState();
    //widget.focusNode = FocusNode(debugLabel: 'Button');
    widget.focusNode.addListener(_handleFocusChange);
    _nodeAttachment = widget.focusNode.attach(context, onKey: _handleKeyPress);
  }

  void _handleFocusChange() {
    print(widget.focusNode.hasFocus);
    if (widget.focusNode.hasFocus != _focused) {
      setState(() {
        _focused = widget.focusNode.hasFocus;
        _color = Colors.white;
      });
    }
  }

  bool _handleKeyPress(FocusNode node, RawKeyEvent event) {
    if (event is RawKeyDownEvent) {
      print('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
      if (event.logicalKey == LogicalKeyboardKey.enter) {
        print('clicked enter');
        setState(() {
          _color = Colors.deepPurple;
        });
        return true;
      }
    }
    return false;
  }

  @override
  void dispose() {
    widget.focusNode.removeListener(_handleFocusChange);
    // The attachment will automatically be detached in dispose().
    widget.focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _nodeAttachment.reparent();
    return Center(
      child: RaisedButton(
        focusNode: widget.focusNode,
        color: _focused ? _color : Colors.white,
        child: Text(_focused ? "focused" : 'Not focus'),
        onPressed: () {
          print("create item");
        },
      ),
    );
  }
}

class MyStatelessWidget extends StatefulWidget {
  MyStatelessWidget({Key key}) : super(key: key);

  @override
  _MyStatelessWidgetState createState() => _MyStatelessWidgetState();
}

class _MyStatelessWidgetState extends State<MyStatelessWidget> {
  FocusNode _node1 = FocusNode();
  FocusNode _node2 = FocusNode();
  @override
  Widget build(BuildContext context) {
    final TextTheme textTheme = Theme.of(context).textTheme;
    return DefaultTextStyle(
      style: textTheme.headline4,
      child: Column(
        children: [
          CustomButton(
            focusNode: _node1,
          ),
          CustomButton(
            focusNode: _node2,
          ),
          RaisedButton(
              onPressed: () {
                _node1.requestFocus();
                setState(() {});
              },
              child: Text("request focus button 1")),
          RaisedButton(
              onPressed: () {
                _node2.requestFocus();
                setState(() {});
              },
              child: Text("request focus button 2")),
        ],
      ),
    );
  }
}
//FocusNode的颤振代码示例
//此示例显示了如果不使用
//[Focus]或[FocusScope]小部件。请参阅[Focus]小部件以了解类似的信息
//使用[Focus]和[FocusScope]小部件的示例。
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
void main()=>runApp(MyApp());
///此小部件是主应用程序小部件。
类MyApp扩展了无状态小部件{
静态常量字符串_title='颤振代码示例';
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:_标题,
家:脚手架(
appBar:appBar(标题:常量文本(_title)),
正文:MyStatelessWidget(),
),
);
}
}
类CustomButton扩展StatefulWidget{
焦点节点;
CustomButton({Key-Key,this.focusNode}):super(Key:Key);
@凌驾
_CustomButtonState createState()=>\u CustomButtonState();
}
类_CustomButtonState扩展状态{
bool_focused=false;
聚焦附件;
颜色_Color=Colors.white;
@凌驾
void initState(){
super.initState();
//widget.focusNode=focusNode(debugLabel:'Button');
widget.focusNode.addListener(_handleFocusChange);
_nodeAttachment=widget.focusNode.attach(上下文,onKey:_handleKeyPress);
}
void_handleFocusChange(){
打印(widget.focusNode.hasFocus);
如果(widget.focusNode)。