xxxxxxxxxx
TextField(
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
textCapitalization: TextCapitalization.characters,
decoration: const InputDecoration(
labelText: 'Port',
border: OutlineInputBorder(),
hintText: "i.e. : 1883"),
),
xxxxxxxxxx
import 'package:flutter/services.dart';
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
xxxxxxxxxx
// import below library (if not used in code, import below library first).
import 'package:flutter/services.dart';
TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
),
// can be applied to (pub.dev package pin_code_fields: ^7.4.0)
PinCodeTextField(
length: 6,
obscureText: true,
animationType: AnimationType.fade,
keyboardType:TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
cursorColor:Colors.black,
pinTheme: PinTheme(
shape: PinCodeFieldShape.underline,
// borderRadius: BorderRadius.circular(5),
fieldHeight: 47,
fieldWidth: 47,
selectedColor:Color(0xFF12877F),
selectedFillColor: Color(0xFFF3FBFB),
activeColor: Color(0xFF12877F),
activeFillColor: Color(0xFFF3FBFB),
inactiveColor:Color(0xFF81C1BD),
inactiveFillColor:Color(0xFFF3FBFB),
errorBorderColor:Color(0xFFFF2121),
),
animationDuration: Duration(milliseconds: 300),
enableActiveFill: true,
errorAnimationController: errorController,
controller: otpController,
onCompleted: (v) {
print("Completed");
},
onChanged: (value) {
print(value);
},
beforeTextPaste: (text) {
print("Allowing to paste $text");
//if you return true then it will show the paste confirmation dialog. Otherwise if false, then nothing will happen.
//but you can show anything you want here, like your pop up saying wrong paste format or etc
return true;
}, appContext: context,
),
xxxxxxxxxx
limite characters number in textField
maxLength: 1,
//to hide characters number
decoration: InputDecoration(
labelText: 'Enter text (max 10 characters)',
counterText: '', // Set an empty string to hide the character count
),
xxxxxxxxxx
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Numeric Input Example'),
),
body: NumericInputWidget(),
),
);
}
}
class NumericInputWidget extends StatefulWidget {
@override
_NumericInputWidgetState createState() => _NumericInputWidgetState();
}
class _NumericInputWidgetState extends State<NumericInputWidget> {
TextEditingController _controller = TextEditingController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _onChanged(String value) {
// Remove non-numeric characters using a regular expression
String numericValue = value.replaceAll(RegExp(r'[^0-9]'), '');
// Update the text field value with the numeric value
_controller.value = _controller.value.copyWith(
text: numericValue,
selection: TextSelection.collapsed(offset: numericValue.length),
);
}
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(16.0),
child: TextField(
controller: _controller,
keyboardType: TextInputType.number,
onChanged: _onChanged,
decoration: InputDecoration(
labelText: 'Enter Numeric Value',
),
),
);
}
}