单独小部件 onChanged 中的 Flutter Textfield:(值){if(value.length == 2)focus.requestFocus(nextFocusNode);} 不起作用
Flutter Textfield in separate widget onChanged: (value) {if (value.length == 2) focus.requestFocus(nextFocusNode);} does not work
我尝试制作一个单独的 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,
),
),
);
}
}
请试试这个
import 'package:flutter/material.dart';
import 'package:careapp/widgets/layout/time_input.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
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: (){
},
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: (v) {
if (v.length == 2){
print("ok");
FocusScope.of(context).nextFocus();
}
//focus.requestFocus(nextFocusNode);
},
//textInputAction: TextInputAction.next,
),
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'),
),
],
)
],
),
),
]),
)
)
);
}
}
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: (v)=>onChanged!(v),
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,
),
),
);
}
}
我尝试制作一个单独的 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,
),
),
);
}
}
请试试这个
import 'package:flutter/material.dart';
import 'package:careapp/widgets/layout/time_input.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
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: (){
},
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: (v) {
if (v.length == 2){
print("ok");
FocusScope.of(context).nextFocus();
}
//focus.requestFocus(nextFocusNode);
},
//textInputAction: TextInputAction.next,
),
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'),
),
],
)
],
),
),
]),
)
)
);
}
}
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: (v)=>onChanged!(v),
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,
),
),
);
}
}