单独小部件中的 Flutter Textfield onChanged: (value) {if (value.length == 2) focus.requestFocus(nextFocusNode);} 不起作用
我尝试制作一个单独的 TextField 小部件以重复使用。因为有很多代码我会重复
,而且我想让光标在 onchanged 中有 2 个数字时跳转到下一个 TextField。所以我使用该值来查看是否已经输入了 2 个数字。我使用上下文来关注下一个焦点节点。
如果我将它放在同一个小部件中,但当我提取该小部件时,它会起作用。这是行不通的。
请你帮助我好吗?
是否可以?
import 'package:flutter/material.dart';
import 'package:careapp/widgets/layout/time_input.dart';
class newInput extends StatefulWidget {
const newInput({Key? key}) : super(key: key);
@override
_newInputState createState() => _newInputState();
}
class _newInputState extends State<newInput> {
final hoursController = TextEditingController();
final minutesController = TextEditingController();
final nextFocusNode = FocusNode();
@override
void dispose() {
hoursController.dispose();
minutesController.dispose();
nextFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final focus = FocusScope.of(context);
return Scaffold(
appBar: AppBar(
title: Text('Add input'),
actions: [
IconButton(
onPressed: _addEvent,
icon: Icon(Icons.save),
),
],
),
body: Card(
elevation: 6,
child: Container(
padding: EdgeInsets.only(top: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
//height: 100,
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 20,
),
child: Row(
children: [
Container(
child: Text('Time:'),
),
timeInput(
labelText: 'Hour',
onSubmitted: () {},
controller: hoursController,
onChanged: (value) {
if (value.length == 2)
focus.requestFocus(nextFocusNode);
},
//textInputAction: TextInputAction.next,
// onEditingComplete: () => focus.nextFocus(),
),
Container(
child: Text(
':',
style: TextStyle(
fontSize: 20,
//fontWeight: FontWeight.bold,
),
),
),
timeInput(
labelText: 'Min',
onSubmitted: () {},
controller: minutesController,
focusNode: nextFocusNode,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ElevatedButton(
onPressed: () {},
child: Text('Add Event'),
),
],
)
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
class timeInput extends StatelessWidget {
final String labelText;
final VoidCallback onSubmitted;
final Function? onChanged;
final VoidCallback? onEditingComplete;
FocusNode? focusNode;
final TextInputAction textInputAction;
final TextEditingController controller;
// final String lastname;
// final String birthday;
// final String id;
timeInput(
{required this.labelText,
required this.onSubmitted,
this.focusNode,
this.textInputAction = TextInputAction.done,
this.onChanged,
this.onEditingComplete = _onEditingComplete,
required this.controller});
static _onEditingComplete() {}
@override
Widget build(BuildContext context) {
return Flexible(
child: Container(
padding: EdgeInsets.all(10),
height: 50,
width: 70,
child: TextField(
// onChanged: (_) => onChanged!(),
onChanged: (_) => onChanged!(),
textInputAction: textInputAction,
// onEditingComplete: onEditingComplete,
maxLength: 2,
//style: ,
decoration: InputDecoration(
//labelText: 'Uur',
labelText: labelText,
border: OutlineInputBorder(),
counterText: '',
),
controller: controller,
keyboardType: TextInputType.numberWithOptions(decimal: false),
onSubmitted: (_) =>
onSubmitted(), //You must add an input but underscore indicates that you are not using it
// onChanged: (val) => amountInput = val,
),
),
);
}
}
I try to make a separate TextField widget to use repeatedly. Because there is a lot of code that I would otherwise duplicate
And I want to make the cursor jump to the next TextField when there are 2 numbers in onchanged. So I use the value to see whether there are already 2 numbers inputted. And I use context to focus on the next focus node.
It works if I put it in te same widget, but when I extract the widget. It won't work.
Could you please help me?
Is it possible?
import 'package:flutter/material.dart';
import 'package:careapp/widgets/layout/time_input.dart';
class newInput extends StatefulWidget {
const newInput({Key? key}) : super(key: key);
@override
_newInputState createState() => _newInputState();
}
class _newInputState extends State<newInput> {
final hoursController = TextEditingController();
final minutesController = TextEditingController();
final nextFocusNode = FocusNode();
@override
void dispose() {
hoursController.dispose();
minutesController.dispose();
nextFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final focus = FocusScope.of(context);
return Scaffold(
appBar: AppBar(
title: Text('Add input'),
actions: [
IconButton(
onPressed: _addEvent,
icon: Icon(Icons.save),
),
],
),
body: Card(
elevation: 6,
child: Container(
padding: EdgeInsets.only(top: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
//height: 100,
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 20,
),
child: Row(
children: [
Container(
child: Text('Time:'),
),
timeInput(
labelText: 'Hour',
onSubmitted: () {},
controller: hoursController,
onChanged: (value) {
if (value.length == 2)
focus.requestFocus(nextFocusNode);
},
//textInputAction: TextInputAction.next,
// onEditingComplete: () => focus.nextFocus(),
),
Container(
child: Text(
':',
style: TextStyle(
fontSize: 20,
//fontWeight: FontWeight.bold,
),
),
),
timeInput(
labelText: 'Min',
onSubmitted: () {},
controller: minutesController,
focusNode: nextFocusNode,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ElevatedButton(
onPressed: () {},
child: Text('Add Event'),
),
],
)
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
class timeInput extends StatelessWidget {
final String labelText;
final VoidCallback onSubmitted;
final Function? onChanged;
final VoidCallback? onEditingComplete;
FocusNode? focusNode;
final TextInputAction textInputAction;
final TextEditingController controller;
// final String lastname;
// final String birthday;
// final String id;
timeInput(
{required this.labelText,
required this.onSubmitted,
this.focusNode,
this.textInputAction = TextInputAction.done,
this.onChanged,
this.onEditingComplete = _onEditingComplete,
required this.controller});
static _onEditingComplete() {}
@override
Widget build(BuildContext context) {
return Flexible(
child: Container(
padding: EdgeInsets.all(10),
height: 50,
width: 70,
child: TextField(
// onChanged: (_) => onChanged!(),
onChanged: (_) => onChanged!(),
textInputAction: textInputAction,
// onEditingComplete: onEditingComplete,
maxLength: 2,
//style: ,
decoration: InputDecoration(
//labelText: 'Uur',
labelText: labelText,
border: OutlineInputBorder(),
counterText: '',
),
controller: controller,
keyboardType: TextInputType.numberWithOptions(decimal: false),
onSubmitted: (_) =>
onSubmitted(), //You must add an input but underscore indicates that you are not using it
// onChanged: (val) => amountInput = val,
),
),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试这个
please try this