最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论