Flutter 将 TextFormField 与 maxLength 对齐

奥马特

我想将一行中的 TextFormFields 与其中一个配置了 maxLength 对齐。同样的行为发生在移动和网络上。

Row(
  children: [
    Expanded(
      child: TextFormField(
        maxLength: characterLimit,
      ),
    ),
    Expanded(
      child: TextFormField(),
    ),
  ]
)

可以理解,maxLength 会增加 TextFormField 底部的空间。由于未对齐,它在没有 maxLength 的 TextFormField 旁边的外观是不可取的。

TextFormField 未对齐

显示验证器消息会对齐两个 TextFormField。

对齐的 TextFormField 验证器

UsingAlign(alignment: Alignment.top)什么都不做,因为两个 TextFormField 具有相同的高度,即使其中一个具有 maxLength。使用Padding(padding: EdgeInsets.only(bottom: paddingBottom)),由于 Padding 给出的偏移量,两个 TextFormField 现在默认具有相同的对齐方式。但是在显示验证器消息时会出现错位。

TextFormField 验证器未对齐

最小复制

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _subscriptionFormKey = GlobalKey<FormState>();

  void _validate() {
    if (_subscriptionFormKey.currentState!.validate()) {}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Form(
        key: _subscriptionFormKey,
        child: Row(
          children: [
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: TextFormField(
                  validator: (message) {
                    if (message == null || message.isEmpty) {
                      return 'Empty field';
                    }
                    return null;
                  },
                  maxLength: 300,
                ),
              ),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: TextFormField(
                  validator: (message) {
                    if (message == null || message.isEmpty) {
                      return 'Empty field';
                    }
                    return null;
                  },
                ),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _validate,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
指甲

我实现如下。

向 Row 小部件添加了“crossAxisAlignment”参数。

在此处输入图片说明

crossAxisAlignment: CrossAxisAlignment.start,
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, @required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _subscriptionFormKey = GlobalKey<FormState>();

  void _validate() {
    if (_subscriptionFormKey.currentState.validate()) {}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Form(
        key: _subscriptionFormKey,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: TextFormField(
                  autovalidateMode: AutovalidateMode.always,
                  validator: (message) {
                    if (message == null || message.isEmpty) {
                      return 'Empty field';
                    }
                    return null;
                  },
                  maxLength: 300,
                ),
              ),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: TextFormField(
                  validator: (message) {
                    if (message == null || message.isEmpty) {
                      return 'Empty field';
                    }
                    return null;
                  },
                ),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _validate,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章