如何在颤动中添加AppBar中的渐变颜色

Mie*_*eMn 15 flutter

我想在AppBar中添加渐变颜色,如果是,那么我怎么能这样做呢?那么我是否需要制作一个容器并在列中创建一个列装饰和渐变颜色.

Rik*_*137 74

这应该可以完美地工作:

return Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
    flexibleSpace: Container(
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: <Color>[Colors.black, Colors.blue]),
      ),
    ),
  ),
);
Run Code Online (Sandbox Code Playgroud)


Man*_*ans 20

我不相信你可以将渐变传递给AppBar,因为它需要Color而不是渐变.

但是,您可以创建自己的小部件来模仿AppBar,除非使用渐变.看一下这个我从Planets-Flutter教程拼凑在一起的例子以及它下面的代码.

在此输入图像描述

import "package:flutter/material.dart";

class Page extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Column(children : <Widget>[new GradientAppBar("Custom Gradient App Bar"), new Container()],);
  }
}


class GradientAppBar extends StatelessWidget {

  final String title;
  final double barHeight = 50.0;

  GradientAppBar(this.title);

  @override
  Widget build(BuildContext context) {
    final double statusbarHeight = MediaQuery
        .of(context)
        .padding
        .top;

    return new Container(
      padding: new EdgeInsets.only(top: statusbarHeight),
      height: statusbarHeight + barHeight,
      child: new Center(
        child: new Text(
          title,
          style: new TextStyle(fontSize: 20.0, color: Colors.white, fontWeight: FontWeight.bold),
        ),
      ),
      decoration: new BoxDecoration(
        gradient: new LinearGradient(
            colors: [Colors.red, Colors.blue],
            begin: const FractionalOffset(0.0, 0.0),
            end: const FractionalOffset(0.5, 0.0),
            stops: [0.0, 1.0],
            tileMode: TileMode.clamp
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.如果您有任何疑问,请告诉我.

  • 因为appbar的所有其他默认行为都需要重写,这不会有问题吗?以后退按钮为例。这个假设正确吗? (3认同)
  • 似乎使用此库是最终解决方案https://github.com/joostlek/GradientAppBar (2认同)

Arn*_*rge 8

AppBar默认不具有该功能。但是您可以制作一个类似于AppBar的小部件,它将具有如下渐变背景:

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new PreferredSize(
        child: new Container(
          padding: new EdgeInsets.only(
            top: MediaQuery.of(context).padding.top
          ),
          child: new Padding(
            padding: const EdgeInsets.only(
              left: 30.0,
              top: 20.0,
              bottom: 20.0
            ),
            child: new Text(
              'Arnold Parge',
              style: new TextStyle(
                fontSize: 20.0,
                fontWeight: FontWeight.w500,
                color: Colors.white
              ),
            ),
          ),
          decoration: new BoxDecoration(
            gradient: new LinearGradient(
              colors: [
                Colors.red,
                Colors.yellow
              ]
            ),
            boxShadow: [
              new BoxShadow(
                color: Colors.grey[500],
                blurRadius: 20.0,
                spreadRadius: 1.0,
              )
            ]
          ),
        ),
        preferredSize: new Size(
          MediaQuery.of(context).size.width,
          150.0
        ),
      ),
      body: new Center(
        child: new Text('Hello'),
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

这里boxShadow会给人以提升的感觉。

  • 这个答案很好,我对此表示赞同,但是它不支持Drawers和其他本机AppBar功能,而且这个库很棒,https://github.com/joostlek/GradientAppBar (3认同)
  • 只能使用 Appbar 来完成。请点击此链接。https://hackernoon.com/flutter-gradient-app-bar-jm8a32fu (2认同)