As a common appbar widget how to change appbar color when page is scrolled Flutter

Issue

This Content is from Stack Overflow. Question asked by Nazlı can

Good morning friends, I’m trying to make the appbar transparent or white in scrollable
parts.
For me, this solution An efficient way in Flutter to change appbar color when scrolled works, but as the person said, I don’t want to use setState continuously and do it in every separate component, so I’m trying to do what is mentioned in the comment. For this reason, I created a common appbar widget so that I can use it in other components. I made the CustomAppBar widget statefull, but I don’t know where to add the scrollController. Therefore, I see errors. If anyone has time, can you help?
The code below is the widget where I call CustomAppBar.

  import ...
    
      const ExtractionBody({Key? key, required this.goal}) : super(key: key);
      final Objective goal;
    
      @override
      ExtractionBodyState createState() => ExtractionBodyState();
    }
    
    class ExtractionBodyState extends ConsumerState<ExtractionBody> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          extendBodyBehindAppBar: true,
          appBar: PreferredSize(
            preferredSize: const Size.fromHeight(100),
            child: CustomAppBar(
              icon: IconButton(
                icon: const Icon(PhosphorIcons.xBold),
                onPressed: () => Navigator.of(context).pushNamedAndRemoveUntil(
                  HomePage.routeName,
                  (route) => false,
                ),
              ),
            ),
          ),
          body: ExtractionRequestContent(
              goal: widget.goal, scrollController: _scrollController),
        );
      }
    }

Finally, this is my CustomAppBar code. Thank you very much in advance. and have a good weekend everyone

class CustomAppBar extends StatefulHookConsumerWidget {
  static String routeName = "/extraction_body";

  const CustomAppBar({Key? key, this.icon})
      : preferredSize = const Size.fromWidth(50),
        super(key: key);
  final Widget? icon;
  @override
  final Size preferredSize; // default is 56.0
  @override
  CustomAppBarState createState() => CustomAppBarState();

}

class CustomAppBarState extends ConsumerState<CustomAppBar> {
  bool isAppbarCollapsing = false;
  final ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    _initializeController();
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  void _initializeController() {
    _scrollController.addListener(() {
      if (_scrollController.offset == 0.0 &&
          !_scrollController.position.outOfRange) {
        //Fully expanded situation
        if (!mounted) return;
        setState(() => isAppbarCollapsing = false);
      }
      if (_scrollController.offset >= 9.0 &&
          !_scrollController.position.outOfRange) {
        //Collapsing situation
        if (!mounted) return;
        setState(() => isAppbarCollapsing = true);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return AppBar(
      elevation: 0,
      backgroundColor:
          isAppbarCollapsing ? AppColors.monochromeWhite : Colors.transparent,
      title: Text(context.l10n.buttonCancel),
      titleSpacing: 4,
      leading: widget.icon,
    );
  }
}

Thanks!



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?