【问题标题】:Flutter align TextFormField with maxLengthFlutter 将 TextFormField 与 maxLength 对齐
【发布时间】:2021-10-14 18:17:09
【问题描述】:

我想将一行中的 TextFormFields 与其中一个配置了 maxLength 对齐。移动设备和网络上的行为相同。

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

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

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

使用Align(alignment: Alignment.top) 什么都不做,因为两个TextFormField 具有相同的高度,即使其中一个具有maxLength。使用Padding(padding: EdgeInsets.only(bottom: paddingBottom)),由于 Padding 给出的偏移量,两个 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),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我实现如下。

    为 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),
          ),
        );
      }
    }
    
    

    【讨论】:

    • 谢谢!工作喜欢一种魅力。 Flutter 有很多对齐方式,我往往会忘记其中的一些。
    猜你喜欢
    • 2019-12-25
    • 2022-10-05
    • 2020-12-15
    • 2021-10-21
    • 2023-03-13
    • 2021-06-13
    • 2020-10-05
    • 1970-01-01
    • 2021-11-20
    相关资源
    最近更新 更多