用 Container 包装 Scaffold 用于渐变背景,如何在 flutter 中将渐变设置为容器背景?

wrapping Scaffold with Container for gradient background, How to set gradient to container background in flutter?

我想用 Container 包裹 Scaffold,以获得也在 AppBar 下方的渐变背景。基本上是全屏 gradient 背景。但是,我的尝试没有做任何事情。 有没有另一种方法,我可以保留 AppBar 功能,但将它放在横跨整个屏幕的 gradient 之上?

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test',
      theme: ThemeData(
        primarySwatch: Colors.yellow,
      ),
      home: MyHomePage(title: 'Test'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topRight,
          end: Alignment.bottomLeft,
          stops: [0.1, 0.5, 0.7, 0.9],
          colors: [
            Colors.yellow[800],
            Colors.yellow[700],
            Colors.yellow[600],
            Colors.yellow[400],
          ],
        ),
      ),
      child: Scaffold(
        appBar: AppBar(
          title: Icon(Icons.menu),
          backgroundColor: Color(0x00000000),
          elevation: 0.0,
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'dummy text',
              ),
              Text(
                '5',
                style: Theme.of(context).textTheme.display1,
              ),
              FloatingActionButton(
                backgroundColor: Colors.white,
                foregroundColor: Colors.black45,
                elevation: 0.0,
                onPressed: () {},
                tooltip: 'Play',
                child: Icon(Icons.play_arrow),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

在您的代码中 - 在 Scaffold 下添加 - backgroundColor: Colors.transparent,

child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
        ..

您还可以像这样向 AppBar 添加渐变,

new Scaffold(
      appBar: AppBar(
        title: Center(child: Text('Awesome AppBar')),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
                colors: [
                  const Color(0xFF3366FF),
                  const Color(0xFF00CCFF),
                ],
                begin: const FractionalOffset(0.0, 0.0),
                end: const FractionalOffset(1.0, 0.0),
                stops: [0.0, 1.0],
                tileMode: TileMode.clamp),
          ),
        ),
        child: ...,
      ),
      body: ...,
    );

线性渐变参数:

  • colors:渐变中要使用的颜色数组,在本例中为两种蓝色。

  • begin,end:第一个颜色和最后一个颜色的位置,本例中,

  • FractionalOffset 允许我们将 x 和 y 的坐标视为 0 到 1 的范围。因为我们想要水平渐变,所以我们对两个度量使用相同的 Y,并且 x 从 0.0(左)变为 1.0(右)。

  • stops:该数组的大小应与颜色相同。它定义颜色将如何分布。 [0.0, 1.0] 将从左到右填充它。 [0.0, 0.5] 将从左到半条等填充颜色

  • tileMode: 如果停靠点没有填满整个区域怎么办。在本例中,我们添加了 clamp(它将重复使用上次使用的颜色),但是随着我们的渐变从 0.0 变为 1.0,这并不是真正必要的。

  • 您还可以在 Container 中添加一个 child。例如:一些徽标图片

        child: Align(
              alignment: Alignment.center,
              child: Image.asset(
                "images/logo.png",
                width: 128,
                height: 128,
              )),
        ),
    

希望这对您有所帮助。

只需在AppBar中添加FlexibleSpace并装饰容器。然后app bar是背景渐变

appBar: AppBar(

      title: Text('Flutter Demo'),
      flexibleSpace: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
            colors: <Color>[
              Colors.red,
              Colors.blue
            ],
          ),
        ),
      ),
    ),

您只需在应用栏中使用 FlexibleSpace

      appBar: AppBar(
      centerTitle: true,
      title: Text(widget.title),
      flexibleSpace: Container(
           decoration: BoxDecoration(
           gradient: LinearGradient(
           begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: <Color>[
              Colors.red,
              Colors.blue
            ])
           ),
          ),
         ),

我使用这段代码将 background 颜色设置为 gradient

return MaterialApp(
        home: Scaffold(
          body: Container(
            decoration: new BoxDecoration(
                gradient: new LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    Color.fromARGB(255, 25,178,238),
                    Color.fromARGB(255, 21,236,229)
                  ],
                )),
            child: Align(
                alignment: Alignment.center,
                child: Image.asset(
                  "images/app_logo.png",
                  width: 128,
                  height: 128,
                )),
          ),
        ),
      );

您可以直接在下面的代码中使用任何容器或视图的渐变颜色

class HomePage extends StatelessWidget {
 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dashboard"),
      ),
      body: Container(
         alignment: Alignment.center,
         decoration: BoxDecoration(
         gradient: LinearGradient(colors: [Colors.red, Colors.blue, Colors.black])
         )
       ),
    );
  }
}