0

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!

1 Answer 1

0

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.