[英]Making a Phone Dialer App. How to link a FloatingActionButton such that it opens a textfield where I can enter a Number
I have tried making a function that returns a TextField and then putting the function name in the onPressed argument.我尝试制作一个返回 TextField 的 function,然后将 function 名称放入 onPressed 参数中。 However when I do press the button nothing happens even in the terminal other than acknowledging that the button has been pressed.但是,当我按下按钮时,除了确认按钮已被按下之外,即使在终端中也没有任何反应。
I am only a week or so in flutter learning and entirely new to stackoverflow so please forgive if I am stupid.This is my code:-我只有一周左右的时间在 flutter 学习并且对 stackoverflow 是全新的所以如果我很愚蠢请原谅。这是我的代码:-
import 'package:flutter/material.dart';
void main() {
runApp(
dialer(),
);
}
class dialer extends StatefulWidget {
const dialer({super.key});
@override
State<dialer> createState() => _dialerState();
}
txtfld(onPressed) {
return Container(
child: TextField(
keyboardType: TextInputType.phone,
),
);
}
class _dialerState extends State<dialer> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.black,
actions: <Widget>[
Icon(
Icons.search,
size: 30,
),
Icon(
Icons.more_vert,
size: 30,
),
Padding(
padding: EdgeInsets.all(5),
),
],
),
body: Column(
children: <Widget>[
Flexible(
flex: 3,
fit: FlexFit.tight,
child: Container(
margin: EdgeInsets.only(top: 50),
child: Text(
'Phone',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
children: <Widget>[
Container(),
Container(
height: 80,
width: 314,
color: Color.fromARGB(255, 59, 59, 59),
padding: EdgeInsets.all(10),
child: Text(
'94693-68171',
style: TextStyle(
color: Colors.grey,
),
textScaleFactor: 1.25,
),
),
],
),
SizedBox(
height: 5,
),
Row(
children: <Widget>[
Container(),
Container(
height: 80,
width: 314,
color: Color.fromARGB(255, 59, 59, 59),
padding: EdgeInsets.all(10),
child: Text(
'696969696969',
style: TextStyle(color: Colors.grey),
textScaleFactor: 1.25,
),
),
],
),
SizedBox(
height: 5,
),
Row(
children: <Widget>[
Container(
height: 80,
width: 314,
color: Color.fromARGB(255, 59, 59, 59),
padding: EdgeInsets.all(10),
child: Text(
'4204204204',
style: TextStyle(color: Colors.grey),
textScaleFactor: 1.25,
),
),
],
),
SizedBox(
height: 5,
),
Row(
children: <Widget>[
Container(
height: 80,
width: 314,
color: Color.fromARGB(255, 59, 59, 59),
padding: EdgeInsets.all(10),
child: Text(
'6666666666',
style: TextStyle(color: Colors.grey),
textScaleFactor: 1.25,
),
),
],
),
SizedBox(
height: 5,
),
Row(
children: <Widget>[
Container(
height: 80,
width: 314,
color: Color.fromARGB(255, 59, 59, 59),
padding: EdgeInsets.all(10),
child: Text(
'1234567890',
style: TextStyle(color: Colors.grey),
textScaleFactor: 1.25,
),
),
],
),
SizedBox(
height: 5,
),
Row(
children: <Widget>[
Container(
height: 80,
width: 314,
color: Color.fromARGB(255, 59, 59, 59),
padding: EdgeInsets.all(10),
child: Text(
'Elon musk',
style: TextStyle(color: Colors.grey),
textScaleFactor: 1.25,
),
),
],
),
SizedBox(
height: 5,
),
],
),
],
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.call),
onPressed: (() => txtfld),
elevation: 12,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
),
);
}
}
Try this: Define a focus node for your textfield and remember to dispose it once triggered by the onTap as below试试这个:为您的文本字段定义一个焦点节点,并记住在 onTap 触发后将其处置,如下所示
late FocusNode commentFocus;
@override
void dispose() {
commentFocus.dispose();
}
@override
initState(){
commentFocus = FocusNode();
super.initState();
}
...
txtfld(onPressed) {
return Container(
child: TextField(
focusNode: commentFocus,
keyboardType: TextInputType.phone,
),
);
}
...
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.call),
onPressed: (){
commentFocus.requestFocus();///Trigger here.
},
elevation: 12,
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.