CustomScrollview自定义滚动颤音

发布于 2025-01-23 16:23:55 字数 2433 浏览 0 评论 0原文

我一直在学习,并试图做一些项目并堆放。因此,我创建了CustomScrollview,并且必须完全打开或完全关闭SliverAppbar。我试图做,但在动画滚动方面有问题。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.deepPurple,
        ),
        body: const MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _scrollController = ScrollController();
  bool isClosed = false;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.offset >= 5 && _scrollController.offset <= 200) {
        if (isClosed) {
          _scrollController.jumpTo(0);
          close();
        } else {
          _scrollController.jumpTo(202);
          close();
        }
      }
    });
  }

  close() {
    setState(() {
      isClosed = !isClosed;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      controller: _scrollController,
      slivers: [
        SliverAppBar(
          expandedHeight: 200,
          flexibleSpace: SizedBox(
            child: Container(
              height: 200,
              decoration: const BoxDecoration(
                  image: DecorationImage(
                      image: NetworkImage(
                          "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS5-OgasVpm-kc2HaOUloxKVlLzLuM6Q53mfA&usqp=CAU"),
                      fit: BoxFit.cover)),
            ),
          ),
        ),
        SliverList(
            delegate: SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            return Container(
              color: Color(index),
              height: 100,
              width: MediaQuery.of(context).size.width,
              child: Text("$index"),
            );
          },
          childCount: 10,
        ))
      ],
    );
  }
}

当使用Jumpto时,它起作用,但是使用Animateto时会出现问题。谢谢你!

I have been studing and trying to do some projects and stack in this. So, I created CustomScrollView and the SliverAppBar has to be either fully opened or fully closed. I tried to do but have problems with animation scrolling.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.deepPurple,
        ),
        body: const MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _scrollController = ScrollController();
  bool isClosed = false;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.offset >= 5 && _scrollController.offset <= 200) {
        if (isClosed) {
          _scrollController.jumpTo(0);
          close();
        } else {
          _scrollController.jumpTo(202);
          close();
        }
      }
    });
  }

  close() {
    setState(() {
      isClosed = !isClosed;
    });
  }

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      controller: _scrollController,
      slivers: [
        SliverAppBar(
          expandedHeight: 200,
          flexibleSpace: SizedBox(
            child: Container(
              height: 200,
              decoration: const BoxDecoration(
                  image: DecorationImage(
                      image: NetworkImage(
                          "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS5-OgasVpm-kc2HaOUloxKVlLzLuM6Q53mfA&usqp=CAU"),
                      fit: BoxFit.cover)),
            ),
          ),
        ),
        SliverList(
            delegate: SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            return Container(
              color: Color(index),
              height: 100,
              width: MediaQuery.of(context).size.width,
              child: Text("$index"),
            );
          },
          childCount: 10,
        ))
      ],
    );
  }
}

It works when jumpTo is used but has problems when animateTo is used. Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2025-01-30 16:23:55

您需要在Future Delay内包装.animateto()
以下代码对我有用

Future.delayed(
  Duration(milliseconds: 600),
  () {
    _scrollController.animateTo(
    202,
    duration: Duration(
        milliseconds: 600),
    curve: Curves.ease);
});

,或者您也可以在FrameCallback中包装。

WidgetsBinding.instance.addPostFrameCallback((_) {
    _scrollController.animateTo(
    202,
    duration: Duration(
        milliseconds: 600),
    curve: Curves.ease);
}

You need to wrap .animateTo() inside a Future delay.
The below code worked for me

Future.delayed(
  Duration(milliseconds: 600),
  () {
    _scrollController.animateTo(
    202,
    duration: Duration(
        milliseconds: 600),
    curve: Curves.ease);
});

Or you can also wrap inside PostFrameCallback

WidgetsBinding.instance.addPostFrameCallback((_) {
    _scrollController.animateTo(
    202,
    duration: Duration(
        milliseconds: 600),
    curve: Curves.ease);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文