skip to Main Content

I need to have a gradient behind my appBar so my scaffold has the extendBodyBehindAppBar set to true:

Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFEEEEEE),
      drawer: const Drawer(),
      extendBodyBehindAppBar: true,
      appBar: CustomAppBar(
        title: 'Wine Shop',
        appContext: context,
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(
              height: 380,
              child: Stack(
                alignment: Alignment.topLeft,
                children: [
                  Container(
                    width: double.infinity,
                    decoration: const BoxDecoration(
                      gradient: LinearGradient(
                        begin: Alignment.topLeft,
                        end: Alignment.bottomRight,
                        colors: [
                          Color(0xFFF9C14C),
                          Color(0xFFF5A328),
                        ],
                      ),
                    ),
                    child: SafeArea(
                      child: _searchInput(),
                    ),
                  ),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: SizedBox(
                      height: 160,
                      width: double.infinity,
                      child: ListView.builder(
                        scrollDirection: Axis.horizontal,
                        shrinkWrap: true,
                        itemBuilder: (c, i) {
                          return _slideCard();
                        },
                        itemCount: 5,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            _buildPopularSection(context),
            _buildRecommendedSection(context),
          ],
        ),
      ),
    );
  }

the problem is that when I scroll the view the content is placed above the appBar:

enter image description here
enter image description here

This is my AppBar widget:

class CustomAppBar extends StatelessWidget with PreferredSizeWidget {
  final String title;
  final BuildContext appContext;
  const CustomAppBar({
    super.key,
    required this.title,
    required this.appContext,
  });

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: Colors.transparent,
      elevation: 0.0,
      title: Text(
        title,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 26.0,
          fontWeight: FontWeight.bold,
        ),
      ),
      actions: [
        AppBarIcon(
          icon: Icons.shopping_cart,
          onPressed: () {},
        ),
      ],
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(56.0);
}

If I remove extendBodyBehindAppBar the content goes under the appBar, what am I missing?

2

Answers


  1. give this a try

    SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(
                  height: 380,
                  child: Stack(
                    alignment: Alignment.topLeft,
                    children: [
                      Container(
                        width: double.infinity,
                        decoration: const BoxDecoration(
                          gradient: LinearGradient(
                            begin: Alignment.topLeft,
                            end: Alignment.bottomRight,
                            colors: [
                              Color(0xFFF9C14C),
                              Color(0xFFF5A328),
                            ],
                          ),
                        ),
                      ),
            // add Column here & put ur searchInput inside of it
                   Column(
                    children: [
                     SafeArea(child: _searchInput()), 
                      Align(
                        alignment: Alignment.bottomCenter,
                        child: SizedBox(
                          height: 160,
                          width: double.infinity,
                          child: ListView.builder(
                            scrollDirection: Axis.horizontal,
                            shrinkWrap: true,
                            itemBuilder: (c, i) {
                              return _slideCard();
                            },
                            itemCount: 5,
                          ),
                        ),
                      ),
    
    Login or Signup to reply.
  2. You should move your SingleScrollView into Stack and adjust your layout a little bit, check code below:

    @override
    Widget build(BuildContext context) {
        return Scaffold(
          extendBodyBehindAppBar: true,
          appBar: CustomAppBar(
            title: "Title",
            appContext: context,
          ),
          body: Stack(
            children: [
              SizedBox(
                height: 380,
                child: Container(
                  width: double.infinity,
                  decoration: const BoxDecoration(
                    gradient: LinearGradient(
                      begin: Alignment.topLeft,
                      end: Alignment.bottomRight,
                      colors: [
                        Color(0xFFF9C14C),
                        Color(0xFFF5A328),
                      ],
                    ),
                  ),
                  // replace with your search input here
                  child: Container(
                    alignment: Alignment.topCenter,
                    margin: const EdgeInsets.only(top: 100),
                    child: const Text(
                      "Search",
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: 220),
                // replace ListView with your SingleScrollView
                child: ListView(
                  padding: EdgeInsets.zero,
                  children: [
                    Container(
                      height: 200,
                      color: Colors.white,
                      alignment: Alignment.center,
                      child: const Text("item 1"),
                    ),
                    Container(
                      height: 200,
                      color: Colors.white,
                      alignment: Alignment.center,
                      child: const Text("item 2"),
                    ),
                    Container(
                      height: 200,
                      color: Colors.white,
                      alignment: Alignment.center,
                      child: const Text("item 3"),
                    ),
                    Container(
                      height: 200,
                      color: Colors.white,
                      alignment: Alignment.center,
                      child: const Text("item 4"),
                    ),
                    Container(
                      height: 200,
                      color: Colors.white,
                      alignment: Alignment.center,
                      child: const Text("item 5"),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      }
    

    Effect:
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search