当前位置:网站首页>Flyter color gradient and follow button imitating Taobao gradient
Flyter color gradient and follow button imitating Taobao gradient
2022-06-22 03:45:00 【One leaf floating boat】
One 、 Linear color gradient
Implement a container , Most use Container Widget, Frame border adopt decoration Attribute control , and BoxDecoration In addition to supporting border Outside the control of , And support gradient The control of .
const BoxDecoration({
this.color,
this.image,
this.border,
this.borderRadius,
this.boxShadow,
this.gradient,
this.backgroundBlendMode,
this.shape = BoxShape.rectangle,
}) In general, it is used Linear gradient ,Fultter Pass through LinearGradient To support the implementation of linear gradient
// Linear gradient
Container(
width: 200,
height: 100,
decoration: BoxDecoration(
gradient: LinearGradient(
// Alignment(x,y) (0,0) By rectangle Calculated from the center point
begin: Alignment(0.0, -1.0),
end: Alignment(0.0, 1.0),
colors: <Color>[
Color(0xFFFF5000),
Color(0xFFFF9000),
Colors.black,
],
),
),
),LinearGradient The definition is as follows :
const LinearGradient({
this.begin = Alignment.centerLeft,
this.end = Alignment.centerRight,
@required List<Color> colors,
List<double> stops,
this.tileMode = TileMode.clamp,
})colors Multiple colors can be passed in , Used to implement gradient
The default value is Aligment.centerLeft and Aligment.centerRight
About Aligment Default properties and (x,y) The relation of coordinates is as follows :
/// The top left corner.
static const Alignment topLeft = Alignment(-1.0, -1.0);
/// The center point along the top edge.
static const Alignment topCenter = Alignment(0.0, -1.0);
/// The top right corner.
static const Alignment topRight = Alignment(1.0, -1.0);
/// The center point along the left edge.
static const Alignment centerLeft = Alignment(-1.0, 0.0);
/// The center point, both horizontally and vertically.
static const Alignment center = Alignment(0.0, 0.0);
/// The center point along the right edge.
static const Alignment centerRight = Alignment(1.0, 0.0);
/// The bottom left corner.
static const Alignment bottomLeft = Alignment(-1.0, 1.0);
/// The center point along the bottom edge.
static const Alignment bottomCenter = Alignment(0.0, 1.0);
/// The bottom right corner.
static const Alignment bottomRight = Alignment(1.0, 1.0);Flutter in View( The typical performance is Container Widget) Center point (0,0) In fact, it is the real central position , Instead of the usual understanding topLeft This point
So the effect of the above gradient is as follows :

Two 、 Horizontal linear gradient
The above implementation is a vertical linear gradient , If you want to use a horizontal linear gradient , It's really just the start and begin Change your point :
Container(
width: 200,
height: 50,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment(-1, 0),
end: Alignment(1.0, 0),
colors: <Color>[
const Color(0xFFFF5000),
const Color(0xFFFF9000),
],
),
),
),Alignment(-1, 0) Means left middle ,Alignment(1.0, 0) It means right middle
effect :

3、 ... and 、 Simulate the follow button of mobile Taobao

The core of this follow button is radius And horizontal linear color gradient
from iconfont Find a wechat follow icon ( Off-white , You can't see it here ):

The awkward thing is that the icon is white , I can't see it posted here . Make a background color as follows :

Color gradient analysis comes down to :const Color(0xFFFF5000), const Color(0xFFFF9000)
besides , Click follow , Become a concerned copywriter , And the state can be changed many times
Realization :
class FollowWidget extends StatefulWidget {
FollowWidget({Key key}) : super(key: key);
_FollowWidgetState createState() => _FollowWidgetState();
}
class _FollowWidgetState extends State<FollowWidget> {
bool _followStatus = false;
void _followClickHandle() {
setState(() {
this._followStatus = !this._followStatus;
});
}
List<Color> _getGradient() {
if (this._followStatus) {
return <Color>[Colors.grey, Colors.grey];
} else {
return <Color>[const Color(0xFFFF5000), const Color(0xFFFF9000)];
}
}
List<Widget> _getContent() {
List<Widget> defaultContent = <Widget>[
Text(
this._followStatus ? ' Followed ' : ' Focus on ',
style: TextStyle(
fontSize: 16,
color: Colors.white,
letterSpacing: 1.2,
),
)
];
List<Widget> prefixContent = <Widget>[
Image.network(
'https://gw.alicdn.com/tfs/TB1OC0TXMMPMeJjy1XcXXXpppXa-108-84.png',
height: 16,
),
SizedBox(width: 3)
];
if (!this._followStatus) {
defaultContent.insertAll(0, prefixContent);
}
return defaultContent;
}
@override
Widget build(BuildContext context) {
return InkWell(
child: Container(
width: 90,
height: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
gradient: LinearGradient(
begin: Alignment(-1, 0),
end: Alignment(1.0, 0),
colors: this._getGradient(),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: this._getContent(),
),
),
onTap: this._followClickHandle,
);
}
}effect :

Four 、 Complete code
If it doesn't open github, Just look at the source code here .
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(' The gradient '),
),
body: HomeContent(),
),
);
}
}
class HomeContent extends StatelessWidget {
const HomeContent({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Radial Gradient
Container(
width: 200,
height: 100,
decoration: BoxDecoration(
gradient: LinearGradient(
// Alignment(x,y) (0,0) By rectangle Calculated from the center point
begin: Alignment(0.0, -1.0),
end: Alignment(0.0, 1.0),
colors: <Color>[
Color(0xFFFF5000),
Color(0xFFFF9000),
Colors.black,
],
),
),
),
SizedBox(height: 10),
// Horizontal gradient
Container(
width: 200,
height: 50,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment(-1, 0),
end: Alignment(1.0, 0),
colors: <Color>[
const Color(0xFFFF5000),
const Color(0xFFFF9000),
],
),
),
),
SizedBox(height: 10),
FollowWidget(),
],
),
);
}
}
class FollowWidget extends StatefulWidget {
FollowWidget({Key key}) : super(key: key);
_FollowWidgetState createState() => _FollowWidgetState();
}
class _FollowWidgetState extends State<FollowWidget> {
bool _followStatus = false;
void _followClickHandle() {
setState(() {
this._followStatus = !this._followStatus;
});
}
List<Color> _getGradient() {
if (this._followStatus) {
return <Color>[Colors.grey, Colors.grey];
} else {
return <Color>[const Color(0xFFFF5000), const Color(0xFFFF9000)];
}
}
List<Widget> _getContent() {
List<Widget> defaultContent = <Widget>[
Text(
this._followStatus ? ' Followed ' : ' Focus on ',
style: TextStyle(
fontSize: 16,
color: Colors.white,
letterSpacing: 1.2,
),
)
];
List<Widget> prefixContent = <Widget>[
Image.network(
'https://gw.alicdn.com/tfs/TB1OC0TXMMPMeJjy1XcXXXpppXa-108-84.png',
height: 16,
),
SizedBox(width: 3)
];
if (!this._followStatus) {
defaultContent.insertAll(0, prefixContent);
}
return defaultContent;
}
@override
Widget build(BuildContext context) {
return InkWell(
child: Container(
width: 90,
height: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
gradient: LinearGradient(
begin: Alignment(-1, 0),
end: Alignment(1.0, 0),
colors: this._getGradient(),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: this._getContent(),
),
),
onTap: this._followClickHandle,
);
}
}import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AnimatedContainer Follow'),
),
body: FollowWidget(),
),
);
}
}
class FollowWidget extends StatefulWidget {
FollowWidget({Key key}) : super(key: key);
_FollowWidgetState createState() => _FollowWidgetState();
}
class _FollowWidgetState extends State<FollowWidget> {
bool _followStatus = false;
void _followClickHandle() {
setState(() {
this._followStatus = !this._followStatus;
});
}
List<Color> _getGradient() {
if (this._followStatus) {
return <Color>[Colors.grey, Colors.grey];
} else {
return <Color>[const Color(0xFFFF5000), const Color(0xFFFF9000)];
}
}
List<Widget> _getContent() {
List<Widget> defaultContent = <Widget>[
Text(
this._followStatus ? ' Followed ' : ' Focus on ',
style: TextStyle(
fontSize: 16,
color: Colors.white,
letterSpacing: 1.2,
),
)
];
List<Widget> prefixContent = <Widget>[
Image.network(
'https://gw.alicdn.com/tfs/TB1OC0TXMMPMeJjy1XcXXXpppXa-108-84.png',
height: 16,
),
SizedBox(width: 3)
];
if (!this._followStatus) {
defaultContent.insertAll(0, prefixContent);
}
return defaultContent;
}
@override
Widget build(BuildContext context) {
return InkWell(
child: AnimatedContainer(
width: 90,
height: 40,
duration: Duration(milliseconds: 500),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
gradient: LinearGradient(
begin: Alignment(-1, 0),
end: Alignment(1.0, 0),
colors: this._getGradient(),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: this._getContent(),
),
),
onTap: this._followClickHandle,
);
}
}边栏推荐
猜你喜欢

OAK相机如何实现同步?

Official competition volume and "answer" of "Cyberspace Security" of secondary vocational group in 2019 national vocational college skills competition

VIM from dislike to dependence (18) -- advanced search mode

3DE new simulation status

Pan micro e-cology V9 information disclosure vulnerability
![[网鼎杯 2018]Fakebook1 参考](/img/c8/a9d77308b6cc542c8c47a570b9234c.png)
[网鼎杯 2018]Fakebook1 参考

IIR滤波器设计基础及Matlab设计示例

利用jemalloc解决flink的内存溢出问题

AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了

【Leetcode】17回溯(电话号码的字母组合)
随机推荐
试用了多款报表工具,终于找到了基于.Net 6开发的一个了
MySQL 45 lecture notes (I) execution of an SQL statement
在请求目标中找到无效字符。有效字符在RFC 7230和RFC 3986中定义
基于51的超声波测距仪代码(截图版)
When 618 attacks, how to choose between Beibei X3 and Jimi h3s? Take you all-round in-depth analysis
力扣:用两个栈实现一个队列
3DE save to Favorites
3DE recover from design
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
倍福Twincat NC PTP使用介绍
Fatal NI connect error 12170.报错处理
逆向crackme之ESp定律脱壳
【牛客刷题-SQL大厂面试真题】NO1.某音短视频
Rabbmitmq publish subscribe mode < 2 >
PLC program variable definition and hardware IO Association in Bifu twincat3
Implementation of common function blocks for Beifu twincat3 servo control
Beifu twincat3 ads error query list
svn与cvs的区别有哪些
3000 yuan projector comparison and evaluation, dangbei D3x beats Jimi new Z6 x
H index problem