简体   繁体   English

在 Flutter 中更改 TextField 的下划线

[英]Change TextField's Underline in Flutter

I'm working on an application using Flutter SDK.我正在使用 Flutter SDK 开发一个应用程序。 When I use a TextField widget, and I focus it, the underline becomes blue.当我使用TextField小部件并聚焦它时,下划线变为蓝色。 I need to change this color to red, how can I do it?我需要把这个颜色改成红色,我该怎么做?

Screenshot of what I need to change.我需要更改的屏幕截图。 I want just the underline to change, , not the label color.我只想改变下划线,而不是标签颜色。

我需要更改的屏幕截图。 (我希望改变下划线,而不是标签颜色)

While these other answers may somehow work, you should definitely not use it.虽然这些其他答案可能以某种方式起作用,但您绝对应该使用它。 That's not the proper way to get a custom theme in Flutter.这不是在 Flutter 中获得自定义主题的正确方法。

A much more elegant solution is as followed :一个更优雅的解决方案如下:

final theme = Theme.of(context);

return new Theme(
  data: theme.copyWith(primaryColor: Colors.red),
  child: new TextField(
    decoration: new InputDecoration(
      labelText: "Hello",
      labelStyle: theme.textTheme.caption.copyWith(color: theme.primaryColor),
    ),
  ),
);

At the same time, if you just want to show an error (Red), use errorText of InputDecoration instead.同时,如果只想显示错误(红色),请改用errorTextInputDecoration It will automatically set the color to red.它会自动将颜色设置为红色。

You can also change its color by following ways.您还可以通过以下方式更改其颜色。

  1. Wrap your TextField in Theme and provide accentColor将您的TextField包裹在Theme并提供accentColor

     Theme( data: Theme.of(context).copyWith(accentColor: Colors.red), child: TextField(), )
  2. Using inputDecoration property.使用inputDecoration属性。

     TextField( decoration: InputDecoration( focusedBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.red), ), ), )

I have used InputDecoration.collapsed to remove the divider and I am changing the color of the bottom border.我已经使用InputDecoration.collapsed删除了分隔线,我正在更改底部边框的颜色。

If you enter a name the bottom border color is blue and if you enter a number or other special characters then the bottom border color is red如果您输入名称,则底部边框颜色为蓝色,如果您输入数字或其他特殊字符,则底部边框颜色为红色

演示

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  static const EdgeInsets _padding = const EdgeInsets.symmetric(horizontal: 20.0, vertical: 8.0);
  Color borderColor = Colors.blue;
  bool nameFlag = false;

  @override
  void initState() {
    super.initState();
  }

  void validateName(String value) {
    final RegExp nameExp = new RegExp(r'^[A-Za-z ]+$');
    if (!nameExp.hasMatch(value) || value.isEmpty)
      borderColor = Colors.red;
    else
      borderColor = Colors.blue;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Column(children: <Widget>[
        new Flexible(
          child: new Container(
            margin: _padding,
            padding: _padding,
            child: new TextField(
              decoration: new InputDecoration.collapsed(
                hintText: "Enter Name",
              ),
              onChanged: (s) {
                setState(() => validateName(s));
              },
            ),
            decoration: new BoxDecoration(
              color: Colors.white,
              border: new Border(
                bottom: new BorderSide(color: borderColor, style: BorderStyle.solid),
              ),
            ),
          ),
        )
      ]),
    );
  }
}

Let me know if this answers your question :)如果这能回答您的问题,请告诉我:)

We can do this by using enabledBorder: UnderlineInputBorder() property of decoration in TextField widget.我们可以通过在 TextField 小部件中使用enabledBorder: UnderlineInputBorder()装饰属性来做到这一点。 The enabled border has a sub-property borderSide: BorderSide(color) which is used to define underline color.启用的边框有一个子属性borderSide: BorderSide(color)用于定义下划线颜色。 So in this tutorial, we would Change Text Input TextField Bottom Underline Color in the Flutter Android iOS app example.因此,在本教程中,我们将在 Flutter Android iOS 应用示例中更改文本输入 TextField 底部下划线颜色。 We would also change TextField underline color on focus.我们还将更改焦点上的 TextField 下划线颜色。

Create TextField widget in Center widget wrap in Container widget.在中心小部件中创建 TextField 小部件包装在容器小部件中。

enabledBorder -> borderSide: BorderSide(color) : Used to set Text Input underline color without focus. enabledBorder -> borderSide: BorderSide(color) : 用于设置没有焦点的文本输入下划线颜色。 focusedBorder -> borderSide: BorderSide(color) : Used to set Text Input underline color on focus. focusBorder -> borderSide: BorderSide(color) : 用于设置焦点上的文本输入下划线颜色。

import 'package:flutter/material.dart';
  
  void main() => runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        home: Scaffold(
          body: SafeArea(
            child: Center(
              child : Container(
                width: 290,
                padding: EdgeInsets.all(10.0),
                child : TextField(
                        autocorrect: true,
                        decoration: InputDecoration(
                          hintText: 'Type Text Here',        
                          enabledBorder: UnderlineInputBorder(      
                            borderSide: BorderSide(color: Colors.red),   
                            ),  
                          focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Colors.green),
                      ),  
                    )
                )
              )
            )
        )
      ));
    }
  }

I haven't tried it yet, but I had a look at the docs for you.我还没有尝试过,但我为你查看了文档。

Looking at the TextField class you can set an InputDecoration , which in turn has anInputBorder .查看TextField类,您可以设置一个InputDecoration ,而后者又具有一个InputBorder Setting that to an UnderlineInputBorder with your own BorderSide should do the trick I guess.我想,将其设置为使用您自己的BorderSideUnderlineInputBorder应该可以解决问题。 You can set the color of the BorderSide.您可以设置 BorderSide 的颜色。

You can also set an InputBorder in the global InputDecorationTheme if you want all textfields to have the same style.如果您希望所有文本字段都具有相同的样式,您还可以在全局InputDecorationTheme 中设置 InputBorder。

I have solved by using : 我用以下方法解决了:

    TextField(
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.red)),),)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM