'How to using TextEditingController
I make a page to verify a phone number and then verify the OTP on a second page. Now my problem is I can send the phone number and can't go to the OTP verify page.
phone: +60176713856 otp: 134679
But I also need to send data of TextEditingController (patientnumberController) values from this page:
import 'package:flutter/material.dart';
import 'package:nodejs_jwt/api_service.dart';
import 'package:snippet_coder_utils/FormHelper.dart';
import 'package:snippet_coder_utils/ProgressHUD.dart';
import 'package:snippet_coder_utils/hex_color.dart';
import 'otp_verify_page.dart';
class LoginOTPPage extends StatefulWidget {
const LoginOTPPage({Key? key}) : super(key: key);
_LoginOTPageState createState() => _LoginOTPageState();
}
class _LoginOTPageState extends State<LoginOTPPage> {
String mobileNo = '+60176713856';
bool isAPICallProcess = false;
@override
Widget build(BuildContext context) {
final patientPhoneNumberController = TextEditingController();
return SafeArea(
child: Scaffold(
body: ProgressHUD(
child: loginUI(),
inAsyncCall: isAPICallProcess,
opacity: .3,
key: UniqueKey(),
),
),
);
}
loginUI() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.network(
"https://i.imgur.com/bOCEVJg.png",
height: 180,
fit: BoxFit.contain,
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: Center(
child: Text(
"Login Mobile Number",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
),
const SizedBox(
height: 10,
),
const Center(
child: Text(
"Enter Patient Phone Number",
style: TextStyle(
fontSize: 14,
),
),
),
const SizedBox(
height: 10,
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
child: Container(
height: 47,
width: 50,
margin: const EdgeInsets.fromLTRB(0, 10, 3, 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
border: Border.all(
color: Colors.grey,
),
),
child: const Center(
child: Text(
"",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
),
),
Flexible(
flex: 5,
child: TextFormField(
maxLines: 1,
maxLength: 10,
decoration: const InputDecoration(
contentPadding: EdgeInsets.all(6),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.green, width: 1),
),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
width: 1,
),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.blue,
width: 1,
),
),
),
initialValue: mobileNo,
keyboardType: TextInputType.number,
onChanged: (String value) {
if (value.length >= 9) {
mobileNo = "+60" + value;
}
},
),
)
],
),
),
Center(
child: FormHelper.submitButton(
"Continue",
() async {
if (mobileNo.length >= 9) {
setState(() {
isAPICallProcess = true;
});
APIService.signin(mobileNo).then((respone) async {
setState(() {
isAPICallProcess = false;
});
if (respone) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => OtpVerifyPage(
mobileNo: mobileNo,
),
),
(route) => false,
);
}
});
}
},
borderColor: HexColor("#78D0B1"),
btnColor: HexColor("#78D0B1"),
txtColor: HexColor("#000000"),
borderRadius: 20,
),
)
],
);
}
}
and send data of TextEditingController (otpverfyController) values from this page:
import 'package:flutter/material.dart';
import 'package:sms_autofill/sms_autofill.dart';
import 'package:snippet_coder_utils/FormHelper.dart';
import 'package:snippet_coder_utils/ProgressHUD.dart';
import 'package:snippet_coder_utils/hex_color.dart';
import 'api_service.dart';
import 'appointments_list.dart';
class OtpVerifyPage extends StatefulWidget {
final String? mobileNo;
const OtpVerifyPage({
Key? key,
this.mobileNo,
}) : super(key: key);
@override
_OtpVerifyPageState createState() => _OtpVerifyPageState();
}
class _OtpVerifyPageState extends State<OtpVerifyPage> {
String _otpCode = "134679";
final int _otpCodeLength = 6;
bool isAPICallProcess = false;
late FocusNode myFocusNode;
@override
void initState() {
super.initState();
myFocusNode = FocusNode();
myFocusNode.requestFocus();
SmsAutoFill().listenForCode.call();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: ProgressHUD(
child: verifyOtpUI(),
inAsyncCall: isAPICallProcess,
opacity: .3,
key: UniqueKey(),
),
),
);
}
verifyOtpUI() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image.network(
"https://i.imgur.com/6aiRpKT.png",
height: 180,
fit: BoxFit.contain,
),
const Padding(
padding: EdgeInsets.only(top: 20),
child: Text(
"OTP Verification",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(
height: 10,
),
Center(
child: Text(
"Enter OTP code \n+60-${widget.mobileNo}",
maxLines: 2,
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 14,
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(25, 0, 25, 0),
child: PinFieldAutoFill(
decoration: UnderlineDecoration(
textStyle: const TextStyle(
fontSize: 20,
color: Colors.black,
),
colorBuilder: FixedColorBuilder(Colors.black.withOpacity(.3)),
),
currentCode: _otpCode,
codeLength: _otpCodeLength,
onCodeChanged: (code) {
if (code!.length == _otpCodeLength) {
_otpCode = code;
FocusScope.of(context).requestFocus(FocusNode());
}
},
),
),
const SizedBox(
height: 20,
),
Center(
child: FormHelper.submitButton(
"Verify",
() async {
if (_otpCode.isNotEmpty) {
setState(() {
isAPICallProcess = true;
});
APIService.verifyOTP(_otpCode).then((respone) async {
setState(() {
isAPICallProcess = false;
});
if (respone!.isNotEmpty) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => AppointmentList(
token: respone,
),
),
(route) => false,
);
}
});
}
},
borderColor: HexColor("#78D0B1"),
btnColor: HexColor("#78D0B1"),
txtColor: HexColor("#000000"),
borderRadius: 20,
),
)
],
);
}
@override
void dispose() {
SmsAutoFill().unregisterListener();
myFocusNode.dispose();
super.dispose();
}
}
Solution 1:[1]
let me know if i am getting you correct .............
to send the the data of textediting controller to the next screen first assign the TexteditingControler to the controller property of the text field from where you want to get the user input after while navigating make a new variable at the receiver side same as you have done for mobile number and assigin texteditingcontrolller.text property to that variable like
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => OtpVerifyPage(
controlllervalue : texteditingController.text,
),
),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vishal_VE |
