用容器包裹脚手架作为渐变背景,如何在Flutter中设置渐变到容器背景?

xggvc2p6  于 2023-05-18  发布在  Flutter
关注(0)|答案(7)|浏览(140)

我想用一个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),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
fhg3lkii

fhg3lkii1#

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

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

v9tzhpje2#

只需在AppBar中添加FlexibleSpace并装饰容器。然后应用程序栏是背景中的渐变。

appBar: AppBar(

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

vsnjm48y3#

我使用这段代码将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,
                )),
          ),
        ),
      );
moiiocjp

moiiocjp4#

您只需在应用栏中使用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
            ])
           ),
          ),
         ),
ffdz8vbo

ffdz8vbo5#

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

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])
         )
       ),
    );
  }
}
92dk7w1h

92dk7w1h6#

你不需要在appBar和Container中设置两次颜色,只需要像这样设置appBar,它们将具有相同的颜色:

appBar: AppBar(
  backgroundColor: Colors.transparent,
  elevation: 0, //this will make the shadow of the appBar disappear and show the exact color of the background you setted.
  //and here put the rest of the configurations you need for the appBar.
)
tez616oj

tez616oj7#

你也可以像这样给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: ...,
    );

LinearGradient参数:

  • colors:渐变中使用的颜色数组,在本例中为两种蓝色。
  • beginend:第一种颜色和最后一种颜色的位置,在这种情况下,
  • 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: Align(
          alignment: Alignment.center,
          child: Image.asset(
            "images/logo.png",
            width: 128,
            height: 128,
          )),
    ),

希望这能帮上忙。

相关问题