尝试制作渐变但结果是白色的?我的代码有什么问题?

Trying to make a gradient but it comes out white? What's wrong with my code?

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {

  @override
  Widget build (BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [const Color(0x50658C), const Color(0x2D4067)],
              begin: FractionalOffset.topCenter,
              end: FractionalOffset.bottomCenter,
              stops: [0.0,1.0],
                tileMode: TileMode.clamp
            ),
          ),
        ),
      ),
    );
  }
}

关注了这个 2 分钟的视频https://www.youtube.com/watch?v=AFiQsM0gojU&ab_channel=ProGrammer

您必须在您的十六进制颜色前加上 OxFF 前缀,您只用了 Ox 前缀。我以您的代码为例提供了一个演示。我只是在十六进制颜色前加上 0xFF.

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

class MyApp extends StatelessWidget {

  @override
  Widget build (BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [const Color(0xFF50658C), const Color(0xFF2D4067)], // adjust hex colors
              begin: FractionalOffset.topCenter,
              end: FractionalOffset.bottomCenter,
              stops: [0.0,1.0],
                tileMode: TileMode.clamp
            ),
          ),
        ),
      ),
    );
  }
}

您将颜色值作为十六进制代码传递,但我猜您错过了 Flutter 接受十六进制代码作为颜色 (0x-opacity-red-green-blue) 连字符的意思提高可读性。所以如果你想给它一个红色,即 0xff0000 那么你需要将它作为 0xffff0000 传递。请注意,0x 之后的前两个位置表示不透明度值。所以现在您的代码如下所示:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {

  @override
  Widget build (BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [const Color(0xFF50658C), const Color(0xFF2D4067)],
              begin: FractionalOffset.topCenter,
              end: FractionalOffset.bottomCenter,
              stops: [0.0,1.0],
                tileMode: TileMode.clamp
            ),
          ),
        ),
      ),
    );
  }
}