【问题标题】:Fading Edge ListView - Flutter衰落边缘列表视图 - 颤振
【发布时间】:2018-12-06 13:50:11
【问题描述】:

有没有人在 Android for Flutter 中遇到过类似 fadingEdgeLength 的问题,这样当您向上滚动时,项目开始淡入屏幕顶部?

下面是我用小部件构建的界面。

如果有帮助,这些是我所指的属性:

android:fadingEdgeLength="10dp"

android:requiresFadingEdge="horizo​​ntal">

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('CMS Users'),
  ),
  body: ListView.builder(
      padding: EdgeInsets.only(top: 20.0, left: 4.0),
      itemExtent: 70.0,
      itemCount: data == null ? 0 : data.length,
      itemBuilder: (BuildContext context, int index) {
        return Card(
          elevation: 10.0,
          child: InkWell(
            onTap: () {
              Navigator.push(
                  context,
                  new MaterialPageRoute(
                    builder: (BuildContext context) =>
                        new PeopleDetails("Profile Page", profiles[index]),
                  ));
            },
            child: ListTile(
              leading: CircleAvatar(
                child: Text(profiles[index].getInitials()),
                backgroundColor: Colors.deepPurple,
                radius: 30.0,
              ),
              title: Text(
                  data[index]["firstname"] + "." + data[index]["lastname"]),
              subtitle: Text(
                  data[index]["email"] + "\n" + data[index]["phonenumber"]),
            ),
          ),
        );
      }),
   );
 }
}

【问题讨论】:

    标签: dart flutter flutter-layout


    【解决方案1】:

    您可以在ListView 之上应用ShaderMask 并使用BlendMode 来获得您想要的。

    Widget animationsList() {
        return Expanded(
          child: ShaderMask(
            shaderCallback: (Rect bounds) {
                return LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: <Color>[Colors.transparent,Colors.red],
                ).createShader(bounds);
            },
            child: Container(height: 200.0, width: 200.0, color: Colors.blue,),
            blendMode: BlendMode.dstATop,
         ),
    );
    

    【讨论】:

    • 这是最好的答案,因为与使用 Stack 覆盖 Container 不同,此解决方案不会在 ListView-children 淡出的区域阻止 ListView 的 GestureDetector。
    • 这是一个很好的答案,但是,由于颤振与重绘和构建的工作方式,如果您希望在用户开始滚动后阴影消失,您必须强制 getState(.. .),然后在计算用户是否滚动等后取消整个结构。好主意。我通过在我的文本末尾添加几个换行符来解决这个快速补丁。稍后我会回到更优雅的解决方案;我必须把这个愚蠢的补丁弄出来。谢谢你,@Martin
    • 有什么方法可以将此应用于SliverList
    【解决方案2】:

    正如其他人所提到的,您可以将 ListView 放在 ShaderMask 下,但是通过少量的额外参数化,您可以获得更好的结果 - 至少如果您想实现我想要的效果。

    您可以选择为 LinearGradient 设置 [stops] 列表:

    [stops] 列表(如果指定)必须与 [colors] 具有相同的长度。它为每种颜色指定向量从开始到结束的分数,介于 0.0 和 1.0 之间。

    另外:有混合模式,其中源的颜色通道被忽略,只有不透明度有效果。 BlendMode.dstOut 在下面的例子中也是如此。正如您在屏幕截图中看到的那样,没有使用紫色,具体而言,仅用于向量的部分。

    您可以使用不同的 [blendMode] 设置,其中有很多。

    void main() {
      runApp(
        MaterialApp(
          home: Scaffold(
            body: FadingListViewWidget(),
          ),
        ),
      );
    }
    
    class FadingListViewWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Container(
            height: 320,
            child: ShaderMask(
              shaderCallback: (Rect rect) {
                return LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [Colors.purple, Colors.transparent, Colors.transparent, Colors.purple],
                  stops: [0.0, 0.1, 0.9, 1.0], // 10% purple, 80% transparent, 10% purple
                ).createShader(rect);
              },
              blendMode: BlendMode.dstOut,
              child: ListView.builder(
                itemCount: 100,
                itemBuilder: (BuildContext context, int index) {
                  return Card(
                    color: Colors.orangeAccent,
                    child: ListTile(
                      title: Text('test test test test test test'),
                    ),
                  );
                },
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 为了避免(对称)LinearGradient 中的冗余: CHANGE: "end: Alignment.bottomCenter" -> "end: Alignment.center" ADD: "tileMode: TileMode.mirror," (这是关键!)删除:“颜色”和“停止”的最后两个条目,并将“停止”的第二个条目乘以两倍:)
    【解决方案3】:

    我有类似的要求,所以我为此任务创建了一个库。 你可以在这里找到它:fading_edge_scrollview

    要使用它,您需要将ScrollController 添加到您的ListView,然后将此ListView 作为子级传递给FadingEdgeScrollView.fromScrollView 构造函数

    【讨论】:

    • 你知道是否有可能使它与SliverList一起工作?
    【解决方案4】:

    用Stack包裹Listview,添加Listview作为第一个子,第二个是带有LinearGradient的Positioned Container。 我的代码示例:

    堆栈:

    return Stack(
                        children: <Widget>[
                          ListView(
                            scrollDirection: Axis.horizontal,
                            children: _myListOrderByDate,
                          ),
                          FadeEndListview(),
                        ],
                      );
    

    覆盖类:

    class FadeEndListview extends StatelessWidget {
      const FadeEndListview({
        Key key,
      }) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return Positioned(
          right: 0,
          width: 8.0,
          height: kYoutubeThumbnailsHeight,
          child: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.centerRight,
                end: Alignment.centerLeft,
                stops: [0.0, 1.0],
                colors: [
                    Theme.of(context).scaffoldBackgroundColor,
                    Theme.of(context).scaffoldBackgroundColor.withOpacity(0.0),
                ],
              ),
            ),
          ),
        );
      }
    }
    

    它看起来像这样:

    【讨论】:

    • 如果手势被堆栈中较高的项目吃掉/阻挡,你会怎么做?
    【解决方案5】:

    尝试使用

    Text(
            'This is big text, I am using Flutter and trying to fade text',
            overflow: TextOverflow.fade,
            maxLines: 1,
          ),
    

    【讨论】:

    • 您好,感谢您的回答,但这并不是我想要的。我希望实现的是,随着每个 ListTile 上升,它会淡入屏幕顶部
    • 这有助于我的演员阵容,我希望单个文本褪色,但是是否可以控制褪色强度?
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2018-04-26
    • 2019-04-25
    相关资源
    最近更新 更多