繁体   English   中英

如何在应用栏 Flutter 中创建线性渐变?

[英]How to create Linear Gradient in App Bar Flutter?

我正在尝试在 appBar 中添加线性渐变,但到目前为止我还没有设法做到这一点。 有人知道我如何在我的 appBar 中添加它吗? 谢谢

decoration: BoxDecoration(
                gradient: LinearGradient(
                    colors: [const Color(0xFFF06292), const Color(0xff2A75BC)]),

我的代码看起来像这样

class RegisterAgree extends StatefulWidget {
  @override
  _RegisterAgreeState createState() => _RegisterAgreeState();
}

class _RegisterAgreeState extends State<RegisterAgree> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.pink,
        title: Row(
          children: <Widget>[
            Image.asset(
              'assets/images/logox.png',
              fit: BoxFit.cover,
              height: 45.0,
            )
          ],
        ),
      ),
    );
  }
}

你也可以使用这个:

appBar: AppBar(

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

您可以通过将AppBar包装在带有渐变的Container中来创建自己的可重用 appbar 小部件:

class GradientAppBar extends StatelessWidget with PreferredSizeWidget {
  static const _defaultHeight = 56.0;

  final double elevation;
  final Gradient gradient;
  final Widget title;
  final double barHeight;

  GradientAppBar(
      {this.elevation = 3.0,
      this.gradient,
      this.title,
      this.barHeight = _defaultHeight});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 56.0,
      decoration: BoxDecoration(gradient: gradient, boxShadow: [
        BoxShadow(
          offset: Offset(0, elevation),
          color: Colors.black.withOpacity(0.3),
          blurRadius: 3,
        ),
      ]),
      child: AppBar(
        title: title,
        elevation: 0.0,
        backgroundColor: Colors.transparent,
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(barHeight);
}

在 DartPad 上尝试完整的示例

截屏

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM