SliverAppBar(Flutter)中的渐变?

时间:2018-10-22 14:24:55

标签: flutter

在SliverAppBar中有人渐变了Gradient吗?展开后,我可以在FlexibleSpace中完成此操作,但收缩后它会变成纯色。有可能治疗吗?

3 个答案:

答案 0 :(得分:0)

显示来自Flutter documention的内容:

    CustomScrollView(
  slivers: <Widget>[
    const SliverAppBar(
      pinned: true,
      expandedHeight: 250.0,
      flexibleSpace: FlexibleSpaceBar(
        title: Text('Demo'),
      ),
    ),
    SliverGrid(
      gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
        maxCrossAxisExtent: 200.0,
        mainAxisSpacing: 10.0,
        crossAxisSpacing: 10.0,
        childAspectRatio: 4.0,
      ),
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return Container(
            alignment: Alignment.center,
            color: Colors.teal[100 * (index % 9)],
            child: Text('grid item $index'),
          );
        },
        childCount: 20,
      ),
    ),
    SliverFixedExtentList(
      itemExtent: 50.0,
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return Container(
            alignment: Alignment.center,
            color: Colors.lightBlue[100 * (index % 9)],
            child: Text('list item $index'),
          );
        },
      ),
    ),
  ],
)

答案 1 :(得分:0)

除非我缺少任何东西,否则这应该可以满足您的要求。

在滚动之前,它看起来像这样:

Screenshot showing app bar with gradient

在像这样滚动之后:

Screenshot showing app bar with gradient after scrolling a bit

import 'package:flutter/material.dart';

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

class GradientAppBar extends StatefulWidget {
  @override
  _GradientAppBarState createState() => _GradientAppBarState();
}

class _GradientAppBarState extends State<GradientAppBar> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
              pinned: true,
              expandedHeight: 100,
              flexibleSpace: Container(
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [
                      Colors.black,
                      Colors.white,
                    ],
                  ),
                ),
              ),
              title: Text("This app bar has a gradient!"),
            ),
            SliverList(
              delegate: SliverChildListDelegate(
                [
                  Container(
                    color: Colors.blue,
                    height: 500,
                  ),
                  Divider(),
                  Container(
                    color: Colors.black12,
                    height: 500,
                  ),
                  Divider(),
                  Container(
                    color: Colors.lightBlue,
                    height: 500,
                  ),
                  Divider(),
                  Container(
                    color: Colors.lightGreen,
                    height: 500,
                  ),
                  Divider(),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

答案 2 :(得分:0)

在FlexibleSpaceBar中包装一个新的Container Widget,然后在该容器下添加BoxDecoration,LinearGradient。enter image description here