I've been struggling with this for months. I have a TextField that appears as the first child of a Row in my Android app. The TextField's text appears outside of the borders. Additionally, the bottom border of the TextField appears to be center aligned in my row, and it doesn't matter how I change the main or cross axis alignments of the row.
I want the center of the TextField to align with the center of the row, and for the text to appear in the center of the borders. What am I doing wrong?
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 50,
height: 40,
child: TextField(
controller: myController,
keyboardType: TextInputType.number,
maxLength: 2,
textAlign: TextAlign.center,
textAlignVertical: TextAlignVertical.bottom,
style: TextStyles.mediumWhiteText,
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be enter
I've been struggling with this for months. I have a TextField that appears as the first child of a Row in my Android app. The TextField's text appears outside of the borders. Additionally, the bottom border of the TextField appears to be center aligned in my row, and it doesn't matter how I change the main or cross axis alignments of the row.
I want the center of the TextField to align with the center of the row, and for the text to appear in the center of the borders. What am I doing wrong?
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: 50,
height: 40,
child: TextField(
controller: myController,
keyboardType: TextInputType.number,
maxLength: 2,
textAlign: TextAlign.center,
textAlignVertical: TextAlignVertical.bottom,
style: TextStyles.mediumWhiteText,
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
], // Only numbers can be enter
Share
Improve this question
asked 4 hours ago
Danny Ellis Jr.Danny Ellis Jr.
1,7062 gold badges25 silver badges46 bronze badges
1 Answer
Reset to default 1If you still want to use the maxLength
attribute, you can use this code and modify it as you need. Another way, you can separate the counter to the new widget.
To center the text below the TextField
, you need to modify the buildCounter
.
buildCounter: (
context, {
required currentLength,
required isFocused,
required maxLength,
}) {
return SizedBox(
width: double.infinity,
child: Text(
'$currentLength/$maxLength',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12.0),
),
);
},
Full Code
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Row(
spacing: 16.0,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
width: 50.0,
child: TextField(
keyboardType: TextInputType.number,
maxLength: 2,
textAlign: TextAlign.center,
textAlignVertical: TextAlignVertical.center,
decoration: const InputDecoration(
isDense: true,
border: OutlineInputBorder(),
),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
style: TextStyle(fontSize: 16.0),
cursorHeight: 16.0,
buildCounter: (
context, {
required currentLength,
required isFocused,
required maxLength,
}) {
return SizedBox(
width: double.infinity,
child: Text(
'$currentLength/$maxLength',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12.0),
),
);
},
),
),
IconButton.filled(
icon: Icon(Icons.remove, size: 16.0),
onPressed: () {},
),
IconButton.filled(
icon: Icon(Icons.add, size: 16.0),
onPressed: () {},
),
],
),
),
),
);
}
}