How to add cursor For PinFieldAutoFill flutter - flutter

There is no cursor while entering OTP, How can add Cursor in pinFieldAutoFill. I am using the sms_autofill: ^1.2.5 package.
PinFieldAutoFill(
autofocus: true,
keyboardType: TextInputType.number,
decoration: UnderlineDecoration(
textStyle: TextStyle(
fontSize: 44.sp,
fontWeight: FontWeight.bold,
color: kDarkBlue),
colorBuilder: FixedColorBuilder(
Colors.grey),
),
currentCode: authService
.loginMobileOTP, // prefill with a code
onCodeSubmitted: (_) async {
authService.login(
context, _scaffoldKey);
},
onCodeChanged: (value) {
authService.loginMobileOTP =
value;
},
codeLength:
6 //code length, default 6
),

Please enable cursor option in "sms_autofill" package, class constructer "PinInputTextField".
Sharing reference code
return PinInputTextField(
pinLength: widget.codeLength,
decoration: widget.decoration,
cursor: Cursor(
width: 2,
height: 40,
color: Colors.red,
radius: Radius.circular(1),
enabled: true,
),

Related

How to make the text field accept other language flutter

The Problem
When I try to type Arabic it just don't show. I tried to look for answer but most what I found how to make the whole app in different language.
Code
Form(
key: _formKey,
child: Container(
width: width-80,
child: TextFormField(
//for disable for the done button
controller: titleControl,
onChanged: (val) {
/*change the val of title*/
setState(() {
title = val;
});
},
/*validation*/
validator: (val) {
if (val!.isEmpty) {
return "Title should not be empty";
}
if (val.length >= 35) {
return "Create a shorter title under 36 characters.";
}
if ((val.contains("*") ||
val.contains("\\")||
val.contains("%") ||
val.contains("~") ||
val.contains("^") ||
val.contains("+") ||
val.contains("=") ||
val.contains("{") ||
val.contains("[") ||
val.contains("}") ||
val.contains("]") ||
val.contains(":") ||
val.contains(";") ||
val.contains("\\")||
val.contains("<") ||
val.contains(">") )) {
return "Title should not contain symbol. Only ',?!_-&##.";
}
return null;
},
decoration: const InputDecoration(
/*background color*/
fillColor: Palette.lightgrey,
filled: true,
contentPadding: const EdgeInsets.symmetric(
vertical: 1.0, horizontal: 10),
/*hint*/
border: OutlineInputBorder(),
hintText: "Title",
hintStyle: TextStyle(fontSize: 18.0,
color: Palette.grey,
height: 2.0,),
/*Border*/
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Palette.midgrey,
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Palette.midgrey,
width: 2.0,
),
),
),
),
),
),
/*end of title*/
What I tried
I follow video to make the whole app in another language.
In pubspec.yaml:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.17.0
#...
flutter:
generate: true
In the main:
supportedLocales: L10n.all,
Created L10n that look like this:
import 'package:flutter/material.dart';
class L10n {
static final all = [
const Locale('en'),
const Locale('ar'),
const Locale('hi'),
const Locale('es'),
const Locale('de'),
];
}
Then I realize this was not my issues I don't want to make the whole app in another language I just want to make the text filed accept Arabic.
I use translator pachege
final translator = GoogleTranslator();
final input = "Здравствуйте. Ты в порядке?";
translator.translate(input, from: 'ru', to: 'en').then(print);
// prints Hello. Are you okay?
var translation = await translator.translate("Dart is very cool!", to:
'pl');
print(translation);
// prints Dart jest bardzo fajny!
print(await "example".translate(to: 'pt'));

When tap on textfield, app crashes returns homepage

Hi I get an error when user focus textfield, app crashes and returns to homepage (see clip here https://www.screencast.com/t/yiJkCBsibcoY)
I've had this error for a while now and cannot seem to fix it, sometimes it happens on other textfields. I cannot replicate the issue only sent from users. Anyone experience this with flutter?
Widget searchBox() {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
border: Border.all(color: Color(0xff0F004E), width: 1.0),
),
child: SimpleAutoCompleteTextField(
key: keyAuto,
controller: textController,
suggestions: suggestions,
textChanged: (text) => searchProduct = text,
textSubmitted: (text) {
loadingBarActive = true;
_sendAnalyticsEvent(text, 'serach_food_action');
searchProduct = text.replaceAll(new RegExp(r'[^\w\s]+'), '');
print('searchProduct RegX $searchProduct');
newSearch = true;
_filterCategories(searchProduct);
_filterRecipes(searchProduct);
// reset search values to intial
usdaItems.clear();
usda!.clear();
perPage = perPageIntial;
present.value = 0;
loadingBarActive = false;
selectApi = <int, Widget>{
0: allProductTab(),
1: allProductTab(),
2: allProductTab(),
3: allProductTab(),
};
setState(() {
_loadUSDAlist = usdaFoodProductList();
_loadOpenList = openFoodProductList();
});
},
style: TextStyle(
fontFamily: 'Nunito', fontSize: 20.0, color: Color(0xff0F004E)),
decoration: InputDecoration(
border: InputBorder.none,
// contentPadding: EdgeInsets.only(top: 14.0),
hintText: 'Search',
hintStyle: TextStyle(
fontFamily: 'Nunito', fontSize: 16.0, color: Color(0xff0F004E)),
prefixIcon: Icon(Icons.search, color: Color(0xff0F004E)),
suffixIcon: IconButton(
icon: Icon(Icons.close, color: Color(0xff0F004E)),
onPressed: () {
textController.clear();
})),
),
);
}
I had exactly the same issue and was literally going crazy trying to find the solution. For me the error originated in my usage of the GetX state management library. For me the issue was solved by replacing this:
Get.to(ReservationDetails(res: Reservation.empty()))
?.then(((value) => setState(() {
//Reload the reservations for the new date from the server
_taskesFuture =
FetchReservation(setStatePublic: _setStatePublic)
.fetchReservation(_selectedDate);
})));
with:
Navigator.push(context,
MaterialPageRoute(
builder: ((context) =>
ReservationDetails(res: Reservation.empty()))))
.then((value) => setState(() {
//Reload the reservations for the new date from the server
_taskesFuture =
FetchReservation(setStatePublic: _setStatePublic)
.fetchReservation(_selectedDate);
}));
I suggest you have a look at how you are opening the page you are experiencing this issue.

Obscure text bool value is not updating in flutter

I am using pin_code_text_field package to create pin code textfields. But when I was updating the bool value used to obscure text. Bool value is not updating in pin code fields until I click on textfields.
Code was added below:
bool pinWasObscured = true;
Row(
children: [
PinCodeTextField(
maxLength: 4,
hideCharacter: pinWasObscured,
highlight: true,
highlightAnimation: true,
highlightAnimationBeginColor: Colors.black,
highlightAnimationEndColor: Colors.white,
highlightAnimationDuration: Duration(seconds: 5),
highlightColor: Color(0xFFF37021),
pinBoxDecoration: ProvidedPinBoxDecoration.underlinedPinBoxDecoration,
maskCharacter: "*",
pinTextStyle: TextStyle(
fontSize: 15.sp,
fontWeight: FontWeight.bold,
),
pinBoxWidth: SizeConfig.blockSizeHorizontal! * 12,
pinBoxHeight: SizeConfig.blockSizeHorizontal! * 10,
autofocus: false,
controller: pinController,
defaultBorderColor: Colors.black26,
),
SizedBox(width: 10),
IconButton(
icon: pinWasObscured
? Icon(Icons.visibility_off_outlined)
: Icon(Icons.visibility_outlined),
onPressed: () {
setState(() {
pinWasObscured = !pinWasObscured;
});
},
),
],
),
the issue is that you are also updating the value of the "hideCharacter" on the click of item. you dont have to update the hideCharacter value everytime.
instead you have to set the value to true if you want to hide the character and false to show.

Updating a text widget using onchanged

I am trying to update a text widget as the user types, its working but i don't see the input as I type
TextFormField(
autofocus: true,
controller: amountController,
keyboardType: TextInputType.number,
cursorColor: Colors.amber,
decoration: InputDecoration(
hintText: "Enter Amount",
prefixIcon: Material(
elevation: 0,
borderRadius: BorderRadius.all(Radius.circular(30)),
child: Icon(
Icons.phone_outlined,
size: 15,
color: Color(0xff020529),
),
),
border: InputBorder.none,
contentPadding:
EdgeInsets.symmetric(horizontal: 25, vertical: 13)),
onChanged: (value) {
add(value);
},
),
this is the widget
Text( x.toString(),
style: TextStyle(fontWeight: FontWeight.bold),
),
This is my method
add(String value) {
var ans = 36 * double.parse(value);
print('ddd');
setState(() {
x = ans;
});
What's happening is double.parse is failing because the given value can't be converted to a number.
One option to handle this is by using a try catch (see following code sample), and then setting the displayed value to 0 (or could also do something like set an error message saying something like "invalid input" if the user enters something like "123d"
add(String value) {
double ans;
try {
ans = 36 * double.parse(value);
} on FormatException catch (e) {
// string couldn't be formatted to double
ans = 0;
}
setState(() {
x = ans;
});
}
I used a try catch, but you could also check if value is empty, and/or add an onError callback to double.parse.

Redraw all container if are created with method [DART]

I write my first app, i created three container with method, but if the method setState() in a container it's call the setState do not redraw other containers.
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
children: <Widget>[
//ELEMENT A
elementGS(size, value_A.text, Colors.cyan[600], GSConst.sizeA, 2),
//ELEMENT B
elementGS(size, value_B.text, Colors.blue, GSConst.sizeB, 1),
],
),
//ELEMENT C
elementGS(size, value_AB.text, Colors.red, 1, 3),
],
),
Method for draw a container
Container elementGS(
Size size, String textHint, Color color, double ratioWidth, int flagGS) {
return Container(
alignment: Alignment.center,
padding: EdgeInsets.symmetric(horizontal: GSConst.kDefaultPadding),
height: size.height * GSConst.kHeightElementRatio,
width: sizeInternalBody.width * ratioWidth,
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.all(Radius.circular(30)),
),
child: TextField(
decoration: InputDecoration(
hintText: textHint,
hintStyle: TextStyle(
color: Colors.white,
fontFamily: 'ChristopherDone',
fontSize: 30,
),
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
alignLabelWithHint: true,
),
style: TextStyle(
color: Colors.white,
fontFamily: 'ChristopherDone',
fontSize: 30,
),
textAlign: TextAlign.center,
autocorrect: false,
showCursor: true,
onChanged: (String value) {
setState(() {
if (value.isNotEmpty) {
Map<String, TextEditingController> contr = {
"A": value_A,
"B": value_B,
"AB": value_AB,
};
switch (flagGS) {
case 1:
//In calculateGS calculate the golden ratio and set value in the textcontroller.
contr = calculateGS(contr, b: double.parse(value));
break;
case 2:
contr = calculateGS(contr, a: double.parse(value));
break;
case 3:
contr = calculateGS(contr, ab: double.parse(value));
break;
}
}
});
},
keyboardType: TextInputType.number,
),
);
if I call setState in "element C" I also want redraw "element A" and "element B".
It's possible?
Here you will find all the code
GITHUB GOLDEN RATIO
if I call setState in "element C" I also want redraw "element A" and "element B". It's possible?
Sure! That what youre doing is just not really that what you are expecting because every widget you create, has just its own state which gets updated.
If you want to observe states in other widgets too, you should read (or watch, or learn) a little bit about the BLoC Pattern(Link: https://bloclibrary.dev/#/ ), or in your case, bc its not very big, Provider (https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple ) both are recommended from the flutter team, so choose one what fits your knowlegde and your requirements.