How to do a Gradient effect from topLeft to botttomRgiht in Flutter Shader?

℡╲_俬逩灬. 提交于 2021-01-03 08:21:45

问题


Hy everyone,

I need to know, how do I can create so a Gradient colors from topLeft to bottomRight withhin a shader in flutter like in example 2 in this image?

I tried with this mini shader code to do this, but it still doesn't working for me.

final Shader linearGradient = LinearGradient(
      colors: <Color>[
        Color(0xff002fff),
        Color(0xff00f4ff),
      ],
    ).createShader(Rect.fromCircle(center: Offset(200, 0), radius: 150));

Could anybody have a ideea, how can this be created? Or it's not impossible right now in Flutter 🤷‍♂️.


回答1:


You can use begin and end properties of LinearGradient.

Ex:

  LinearGradient(
     begin: Alignment.topLeft,
     end: Alignment.bottomRight,
     colors: <Color>[
     Color(0xff002fff),
     Color(0xff00f4ff),
     ],
 ),



回答2:


 @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: Column(
        children: <Widget>[

          Container(
            width: 200,
            height: 200,
            decoration: new BoxDecoration(
                gradient: new LinearGradient(
                    colors: [Color(0xFF11F4B5), Color(0xFFCA436B)],
                    begin: FractionalOffset.centerLeft,
                    end: FractionalOffset.centerRight,
                    stops: [0.0, 1.0],
                    tileMode: TileMode.clamp)),
          ),
          SizedBox(
            height: 20,
          ),


          Container(

            width: 200,
            height: 200,
            decoration: new BoxDecoration(
                gradient: new LinearGradient(
                    colors: [Color(0xFF1cb5e1), Color(0xFF000046),Color(0xFF000000)],
                    begin: FractionalOffset.topLeft,
                    end: FractionalOffset.bottomRight,
                    stops: [0.0, 1.0],
                    tileMode: TileMode.mirror)),
          ),
        ],
      )),
    );
  }

just check if this works, changing the color will get you the desired output



来源:https://stackoverflow.com/questions/59679564/how-to-do-a-gradient-effect-from-topleft-to-botttomrgiht-in-flutter-shader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!