我正在使用Flutter SDK开发一个应用程序。当我使用
需要更改的截图如下。我只想更改下划线,而不是标签颜色。
TextField
小部件并将其聚焦时,下划线会变成蓝色。我需要将此颜色更改为红色,该怎么做?需要更改的截图如下。我只想更改下划线,而不是标签颜色。
您还可以按照以下方式更改其颜色。
将您的TextField
包装在Theme
中并提供accentColor
Theme(
data: Theme.of(context).copyWith(accentColor: Colors.red),
child: TextField(),
)
使用inputDecoration
属性。 TextField(
decoration: InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red),
),
),
)
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),
),
),
);
同时,如果你只想显示一个错误(红色),请使用InputDecoration
的errorText
。它会自动将颜色设置为红色。
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),
),
),
),
)
]),
);
}
}
TextField
包装在Theme
中要困难得多。特别是在alpha版本中,一切都迭代得非常快(一个月前inputDecorationTheme
甚至还不存在)。 - Rémi Rousselet我还没有尝试过,但我替你看了一下文档。
看一下TextField类,你可以设置InputDecoration,它又有一个InputBorder。将其设置为UnderlineInputBorder并使用自己的BorderSide应该就可以了。您可以设置{{BorderSide}}的颜色。
如果您希望所有文本字段具有相同的样式,则还可以在全局InputDecorationTheme中设置一个InputBorder。
TextField
在内部覆盖了 inputDecorationTheme
的 borderSide
属性,并使用主题颜色构建它。它无法正常工作。 - Rémi RousseletinputDecorationTheme
对我很有效。inputDecorationTheme: InputDecorationTheme(
labelStyle: TextStyle(color: Colors.white),
helperStyle: TextStyle(color: Colors.white),
hintStyle: TextStyle(color: Colors.grey),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
)
),
- surenyonjan
hintColor
,返回ThemeData.light().copyWith(hintColor: Colors.blueAccent,)
。 - AbdulMomen عبدالمؤمن