Danny Ellis Jr.
Danny Ellis Jr.

Reputation: 1706

Flutter TextField Text appears outside of borders. Bottom Border or TextField aligns with the center of the Row

See the gray textfield border and where the zero appears?

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

Upvotes: 0

Views: 41

Answers (1)

dezombiecat
dezombiecat

Reputation: 86

If 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: () {},
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Result

Upvotes: 1

Related Questions