gpt4 book ai didi

flutter - 无法在 Flutter 中向 TextFormField 添加渐变

转载 作者:IT王子 更新时间:2023-10-29 07:02:41 27 4
gpt4 key购买 nike

我正在尝试向我的 TextFormField 添加渐变颜色,但无法执行,因为我不确定该怎么做。

下面是我的代码



return Scaffold(
appBar: AppBar(
title: Text('Simple Interest Calculator'),
),
body: Form(
key: _formKey,
child: Column (
children: [
Padding(
padding: EdgeInsets.only(top: 10.0, bottom: 5.0, left: 15.0, right: 15.0),
child: TextFormField(
keyboardType: textInputType,
style: Theme
.of(context)
.textTheme
.title,
controller: textController,
decoration: InputDecoration(
labelStyle: TextStyle(
color: Colors.grey,
fontSize: 16.0
),
contentPadding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
errorStyle: TextStyle(
color: Colors.red,
fontSize: 15.0
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: colorTextInput, width:1.0),
borderRadius: BorderRadius.circular(5.0)
)
)
)
),


),
);
}

我希望有类似于下图的渐变。而不是从上到下或从下到上的渐变。我正在寻找从左到右的渐变。我想在左侧以颜色“#fafafa”开始,在文本字段的右侧以颜色“#EDEDED”结束。有人可以帮助我如何在 flutter 中做到这一点吗?提前致谢

enter image description here

最佳答案

    // below is custom color class
class HexColor extends Color {
static int _getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
return int.parse(hexColor, radix: 16);
}

HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}

class SharedColor {
Color brightBlue = HexColor("cfe3e5");
}

// below is the container widget you need to add decoration in your text field container and you asked from left to right gradient change.

return Scaffold(
appBar: AppBar(
title: Text('Simple Interest Calculator'),
),
body: Form(

child: Column (
children: [

Container(
decoration: BoxDecoration(
// Box decoration takes a gradient
gradient: LinearGradient(
// Where the linear gradient begins and ends
begin: Alignment.topRight,
end: Alignment.bottomLeft,
// Add one stop for each color. Stops should increase from 0 to 1
stops: [0.1, 0.4, 0.7, 0.9],
colors: [
// Colors are easy thanks to Flutter's Colors class.
SharedColor().brightBlue.withAlpha(800),
SharedColor().brightBlue.withAlpha(500),
SharedColor().brightBlue.withAlpha(400),
SharedColor().brightBlue.withAlpha(100),
],
),
),
child: TextFormField(
style: Theme
.of(context)
.textTheme
.title,
controller: _textController,
decoration: InputDecoration(
labelStyle: TextStyle(
color: Colors.grey,
fontSize: 16.0
),
contentPadding: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
errorStyle: TextStyle(
color: Colors.red,
fontSize: 15.0
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.black, width:1.0),
borderRadius: BorderRadius.circular(5.0)
)
)
)
),
])));

关于flutter - 无法在 Flutter 中向 TextFormField 添加渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56812231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com