简体   繁体   English

无法更改 TextField 边框颜色

[英]Not able to change TextField Border Color

I am trying to change color of the border of my TextField using a BorderSide , but it does not work.我正在尝试使用BorderSide更改我的TextField边框的颜色,但它不起作用。

This is my code below.这是我下面的代码。

new TextField(
  decoration: new InputDecoration(
    border: new OutlineInputBorder(
      borderSide: new BorderSide(color: Colors.teal)
    ),
    hintText: 'Tell us about yourself',
    helperText: 'Keep it short, this is just a demo.',
    labelText: 'Life story',
    prefixIcon: const Icon(Icons.person, color: Colors.green,),
    prefixText: ' ',
    suffixText: 'USD',
    suffixStyle: const TextStyle(color: Colors.green)),
  )
)

Screenshot of the result is shown below.结果截图如下所示。

The new way to do it is to use enabledBorder like this:这样做的新方法是使用enabledBorder像这样:

new TextField(
  decoration: new InputDecoration(
    enabledBorder: const OutlineInputBorder(
      borderSide: const BorderSide(color: Colors.grey, width: 0.0),
    ),
    focusedBorder: ...
    border: ...
  ),
)

That is not changing due to the default theme set to the screen.由于设置到屏幕的默认主题,这不会改变。

So just change them for the widget you are drawing by wrapping your TextField with new ThemeData()因此,只需通过用new ThemeData()包装 TextField 来为您正在绘制的小部件更改它们

child: new Theme(
          data: new ThemeData(
            primaryColor: Colors.redAccent,
            primaryColorDark: Colors.red,
          ),
          child: new TextField(
            decoration: new InputDecoration(
                border: new OutlineInputBorder(
                    borderSide: new BorderSide(color: Colors.teal)),
                hintText: 'Tell us about yourself',
                helperText: 'Keep it short, this is just a demo.',
                labelText: 'Life story',
                prefixIcon: const Icon(
                  Icons.person,
                  color: Colors.green,
                ),
                prefixText: ' ',
                suffixText: 'USD',
                suffixStyle: const TextStyle(color: Colors.green)),
          ),
        ));

在此处输入图片说明

Well, I really don't know why the color assigned to border does not work.好吧,我真的不知道为什么分配给边框的颜色不起作用。 But you can control the border color using other border properties of the textfield.但是您可以使用文本字段的其他边框属性来控制边框颜色。 They are:他们是:

  1. disabledBorder: Is activated when enabled is set to false disabledBorder:当 enabled 设置为 false 时被激活
  2. enabledBorder: Is activated when enabled is set to true (by default enabled property of TextField is true) enabledBorder:当 enabled 设置为 true 时激活(默认情况下 TextField 的 enabled 属性为 true)
  3. errorBorder: Is activated when there is some error (ie a failed validate) errorBorder:在出现错误时激活(即验证失败)
  4. focusedBorder: Is activated when we click/focus on the TextField. focusedBorder:当我们单击/聚焦于 TextField 时被激活。
  5. focusedErrorBorder: Is activated when there is error and we are currently focused on that TextField. focusedErrorBorder:在出现错误时激活,我们当前专注于该 TextField。

A code snippet is given below:下面给出了一个代码片段:

TextField(
 enabled: false, // to trigger disabledBorder
 decoration: InputDecoration(
   filled: true,
   fillColor: Color(0xFFF2F2F2),
   focusedBorder: OutlineInputBorder(
     borderRadius: BorderRadius.all(Radius.circular(4)),
     borderSide: BorderSide(width: 1,color: Colors.red),
   ),
   disabledBorder: OutlineInputBorder(
     borderRadius: BorderRadius.all(Radius.circular(4)),
     borderSide: BorderSide(width: 1,color: Colors.orange),
   ),
   enabledBorder: OutlineInputBorder(
     borderRadius: BorderRadius.all(Radius.circular(4)),
     borderSide: BorderSide(width: 1,color: Colors.green),
   ),
   border: OutlineInputBorder(
     borderRadius: BorderRadius.all(Radius.circular(4)),
     borderSide: BorderSide(width: 1,)
   ),
   errorBorder: OutlineInputBorder(
     borderRadius: BorderRadius.all(Radius.circular(4)),
     borderSide: BorderSide(width: 1,color: Colors.black)
   ),
   focusedErrorBorder: OutlineInputBorder(
     borderRadius: BorderRadius.all(Radius.circular(4)),
     borderSide: BorderSide(width: 1,color: Colors.yellowAccent)
   ),
   hintText: "HintText",
   hintStyle: TextStyle(fontSize: 16,color: Color(0xFFB3B1B1)),
   errorText: snapshot.error,
 ),
 controller: _passwordController,
 onChanged: _authenticationFormBloc.onPasswordChanged,
                            obscureText: false,
),

disabledBorder:禁用边框:

禁用边框


enabledBorder:启用边框:

启用边框

focusedBorder:重点边框:

重点边框

errorBorder:错误边界:

错误边框

errorFocusedBorder: errorFocusedBorder:

错误焦点边框

Hope it helps you.希望对你有帮助。

The code in which you change the color of the primaryColor and primaryColorDark does not change the color inicial of the border, only after tap the color stay black更改primaryColorprimaryColorDark颜色的代码不会更改边框的颜色,只有在点击颜色后才保持黑色

The attribute that must be changed is hintColor必须改变的属性是hintColor

BorderSide should not be used for this, you need to change Theme. BorderSide不应该用于此,您需要更改 Theme。

To make the red color default to put the theme in MaterialApp(theme: ...) and to change the theme of a specific widget, such as changing the default red color to the yellow color of the widget, surrounds the widget with:要使红色默认将主题放在MaterialApp(theme: ...)并更改特定小部件的主题,例如将默认红色更改为小部件的黄色,围绕小部件:

new Theme(
  data: new ThemeData(
    hintColor: Colors.yellow
  ),
  child: ...
)

Below is the code and gif:下面是代码和gif:

Note that if we define the primaryColor color as black, by tapping the widget it is selected with the color black请注意,如果我们将primaryColor颜色定义为黑色,则通过点击小部件,它会被选择为黑色

But to change the label and text inside the widget, we need to set the theme to InputDecorationTheme但是要更改小部件内部的标签和文本,我们需要将主题设置为InputDecorationTheme

The widget that starts with the yellow color has its own theme and the widget that starts with the red color has the default theme defined with the function buildTheme()以黄色开头的小部件具有自己的主题,以红色开头的小部件具有使用函数buildTheme()定义的默认主题

在此处输入图片说明

import 'package:flutter/material.dart';

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

ThemeData buildTheme() {
  final ThemeData base = ThemeData();
  return base.copyWith(
    hintColor: Colors.red,
    primaryColor: Colors.black,
    inputDecorationTheme: InputDecorationTheme(
      hintStyle: TextStyle(
        color: Colors.blue,
      ),
      labelStyle: TextStyle(
        color: Colors.green,
      ),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: buildTheme(),
      home: new HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String xp = '0';

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(),
      body: new Container(
        padding: new EdgeInsets.only(top: 16.0),
        child: new ListView(
          children: <Widget>[
            new InkWell(
              onTap: () {},
              child: new Theme(
                data: new ThemeData(
                  hintColor: Colors.yellow
                ),
                child: new TextField(
                  decoration: new InputDecoration(
                      border: new OutlineInputBorder(),
                      hintText: 'Tell us about yourself',
                      helperText: 'Keep it short, this is just a demo.',
                      labelText: 'Life story',
                      prefixIcon: const Icon(Icons.person, color: Colors.green,),
                      prefixText: ' ',
                      suffixText: 'USD',
                      suffixStyle: const TextStyle(color: Colors.green)),
                )
              )
            ),
            new InkWell(
              onTap: () {},                
              child: new TextField(
                decoration: new InputDecoration(
                    border: new OutlineInputBorder(
                      borderSide: new BorderSide(color: Colors.teal)
                    ),
                    hintText: 'Tell us about yourself',
                    helperText: 'Keep it short, this is just a demo.',
                    labelText: 'Life story',
                    prefixIcon: const Icon(Icons.person, color: Colors.green,),
                    prefixText: ' ',
                    suffixText: 'USD',
                    suffixStyle: const TextStyle(color: Colors.green)),
              )
            )
          ],
        ),
      )
    );
  }
}
enabledBorder: OutlineInputBorder(
  borderRadius: BorderRadius.circular(10.0),
  borderSide: BorderSide(color: Colors.red)
),

The best and most effective solution is just adding theme in your main class and add input decoration like these.最好和最有效的解决方案是在你的主类中添加主题并添加这样的输入装饰。

theme: ThemeData(
    inputDecorationTheme: InputDecorationTheme(
        border: OutlineInputBorder(
           borderSide: BorderSide(color: Colors.pink)
        )
    ),
)
TextField(
  decoration: InputDecoration(
    border: OutlineInputBorder(
      borderRadius: BorderRadius.all(Radius.circular(4.0)),
      borderSide: BorderSide(width: 1.0),
    ),
    enabledBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.grey),
    ),
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.blueGrey),
    ),
    errorBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.redAccent),
    ),
    focusedErrorBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.orangeAccent),
    ),
    disabledBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.white),
    ),
    contentPadding: EdgeInsets.all(10.0),
    hintText: 'Tell us about yourself',
    helperText: 'Keep it short, this is just a demo.',
    labelText: 'Life story',
    prefixIcon: const Icon(
      Icons.person,
      color: Colors.green,
    ),
    prefixText: ' ',
    suffixText: 'USD',
    suffixStyle: const TextStyle(color: Colors.green),
  ),
),
  1. Inside your lib file在你的 lib 文件中

  2. Create a folder called colors .创建一个名为colors的文件夹。

  3. Inside the colors folder create a dart file and name it color .colors文件夹中创建一个 dart 文件并将其命名为color

  4. Paste this code inside it将此代码粘贴在其中

    const MaterialColor primaryOrange = MaterialColor( _orangePrimaryValue, <int, Color>{ 50: Color(0xFFFF9480), 100: Color(0xFFFF9480), 200: Color(0xFFFF9480), 300: Color(0xFFFF9480), 400: Color(0xFFFF9480), 500: Color(0xFFFF9480), 600: Color(0xFFFF9480), 700: Color(0xFFFF9480), 800: Color(0xFFFF9480), 900: Color(0xFFFF9480), }, ); const int _orangePrimaryValue = 0xFFFF9480;
  5. Go to your main.dart file and place this code in your theme转到您的main.dart文件并将此代码放在您的主题中

    theme:ThemeData( primarySwatch: primaryOrange, ),
  6. Import the color folder in your main.dart like this import 'colors/colors.dart' ;像这样import 'colors/colors.dart'main.dart导入color文件夹;

在此处输入图像描述

border: OutlineInputBorder(borderSide: BorderSide(color: CustomColors.primaryColor),),

在此处输入图像描述

We have tried custom search box with the pasted snippet.我们已尝试使用粘贴的代码段自定义搜索框。 This code will useful for all kind of TextFiled decoration in Flutter.这段代码对 Flutter 中的所有TextFiled装饰都很有用。 Hope this snippet will helpful for others.希望这个片段对其他人有帮助。

Container(
        margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
        child:  new Theme(
          data: new ThemeData(
           hintColor: Colors.white,
            primaryColor: Colors.white,
            primaryColorDark: Colors.white,
          ),
          child:Padding(
          padding: EdgeInsets.all(10.0),
          child: TextField(
            style: TextStyle(color: Colors.white),
            onChanged: (value) {
              filterSearchResults(value);
            },
            controller: editingController,
            decoration: InputDecoration(
                labelText: "Search",
                hintText: "Search",
                prefixIcon: Icon(Icons.search,color: Colors.white,),
                enabled: true,
                enabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.white),
                    borderRadius: BorderRadius.all(Radius.circular(25.0))),
                border: OutlineInputBorder(
                    borderSide: const BorderSide(color: Colors.white, width: 0.0),
                    borderRadius: BorderRadius.all(Radius.circular(25.0)))),
          ),
        ),
        ),
      ),

You can use this code for bottom sheets as well as for normal text fields :您可以将此代码用于底部工作表以及普通文本字段:

class TextFieldForDropDown extends StatelessWidget {
      final String title;
      final String hintText;
      final TextEditingController textEditingController;
      bool isPassword;
      final Function onTap;
      final bool enable;
      TextFieldForDropDown({this.title, this.hintText, this.textEditingController, this.isPassword = false, this.onTap, this.enable});
      @override
      Widget build(BuildContext context) {
    
        var titleTextStyle = TextStyle(
          color: Color(0xff9098C8),
          fontSize: 12,
          fontWeight: FontWeight.w400,
          fontFamily: "Muli",
        );
    
        var textFieldTextStyle = TextStyle(
          color: Colors.white,
          fontSize: 14,
          fontWeight: FontWeight.w400,
          fontFamily: "Muli",
        );
    
        var borderSides = OutlineInputBorder(borderSide: new BorderSide(color: Color(0xff38406B)));
        var borderSides1 = OutlineInputBorder(borderSide: new BorderSide(color: Color(0xffdae4ff)));
    
        return InkWell(
          onTap: onTap,
          child: Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(this.title, style: titleTextStyle),
                SizedBox(height: 8),
                TextFormField(
                  enabled: enable,
                  // onTap: onTap,
                  obscureText: isPassword,
                  style: textFieldTextStyle,
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
                    hintText: this.hintText,
                    hintStyle: titleTextStyle,
                    border: textEditingController.text != "" ? borderSides1 :borderSides,
                    enabledBorder:  textEditingController.text != "" ? borderSides1 :borderSides,
                    disabledBorder: textEditingController.text != "" ? borderSides1 :borderSides,
                    focusedBorder: OutlineInputBorder(borderSide: new BorderSide(color: Color(0xffdae4ff))),
                  ),
                  controller: textEditingController,
                )
              ],
            ),
          ),
        );
      }
    }

and use like this :并像这样使用:

TextFieldForDropDown(
                                title: 'Phone Number*',
                                hintText: '+123-22-223-00',
                                textEditingController: viewModel.phoneController,
                              ),
Padding(
            padding: EdgeInsets.symmetric(vertical: 10, horizontal: 40),
            child: TextField(
              cursorColor: Color.fromRGBO(25, 118, 218, 1),
              decoration: new InputDecoration(
                border: new OutlineInputBorder(
                  borderSide:
                      new BorderSide(color: Color.fromRGBO(25, 118, 218, 1)),
                ),
                focusedBorder: new OutlineInputBorder(
                  borderSide:
                      new BorderSide(color: Color.fromRGBO(25, 118, 218, 1)),
                ),
                labelText: "Edit Phone",
                labelStyle: TextStyle(
                  color: Colors.grey,
                ),
                prefixIcon: const Icon(
                  Icons.phone_outlined,
                  color: Color.fromRGBO(25, 118, 218, 1),
                ),
              ),
            ),
          ),

Thank me later :)晚点再谢我 :)

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

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