如何在 App Bar Flutter 中创建线性渐变?

pow*_*941 5 gradient dart android-appbarlayout flutter flutter-appbar

我正在尝试在应用程序栏中添加线性渐变,但到目前为止我还没有设法做到这一点。有谁知道如何将其添加到我的应用程序栏中?谢谢

decoration: BoxDecoration(
                gradient: LinearGradient(
                    colors: [const Color(0xFFF06292), const Color(0xff2A75BC)]),
Run Code Online (Sandbox Code Playgroud)

我的代码看起来像这样

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,
            )
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

ouz*_*ari 5

您还可以使用这个:

appBar: AppBar(

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


Gui*_*oux 2

您可以通过用渐变AppBar包裹 a来创建自己的可重用 appbar 小部件:Container

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);
}
Run Code Online (Sandbox Code Playgroud)

在 DartPad 上尝试完整示例

截屏

在此输入图像描述