6

enter image description here

enter image description here

I am trying to apply image in the background of the status bar in an flutter project. So which widget can help me to apply the following expected result.

detailpage.dart

import 'package:flutter/material.dart';

class MyDetailPage extends StatefulWidget {
  @override
  _MyDetailPageState createState() => _MyDetailPageState();
}

class _MyDetailPageState extends State<MyDetailPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Container(
            height: 300,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage("assets/image2.png"),
                fit: BoxFit.fitHeight,
              ),
            ),
          )
        ],
      ),
    );
  }
}
Tushar Rai
  • 2,371
  • 4
  • 28
  • 57

5 Answers5

11

Use Scaffold property extendBodyBehindAppBar: true, and set statusBarColor: Colors.transparent

This code is for status bar colour.

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Colors.transparent
    ));

Here is an example of code in my current project.

  @override
  Widget build(BuildContext context) {
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Colors.transparent
    ));
    return Scaffold(
      extendBodyBehindAppBar: true,
      body: Column(
        children: [
          Container(
            height: 250,
            child: Container(
              child: Stack(
                alignment: Alignment.topLeft,
                children: [
                  Container(
                    width: screenWidthPercentage(context,percentage: 1),
                    child: Image.network(
                      contentImage,
                      fit: BoxFit.cover,
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 48.0,left: 12),
                    child: Icon(Icons.arrow_back,color: Colors.white,),
                  ),
                ],
              ),
            ),
          ),
     
        ],
      ),
    );
  }
}

Result:

Rezaul Islam
  • 603
  • 9
  • 12
3

enter image description here

you can do it by not keeping SafeArea widget and set statusbar color as transparent.

I have created an example as below.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

  @override
  _FullScreenState createState() => _FullScreenState();
}

class _FullScreenState extends State<FullScreen> {
  @override
  void initState() {
    super.initState();
    SystemChrome.setSystemUIOverlayStyle(
        SystemUiOverlayStyle(
            statusBarColor: Colors.transparent,
            statusBarIconBrightness: Brightness.light
          //color set to transperent or set your own color
        )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: buildBody(),
    );
  }

  Widget buildBody() {
    return SizedBox(
      height: 400,
      width: double.infinity,
      child: Card(
        clipBehavior: Clip.antiAlias,
        margin: EdgeInsets.all(0),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(40),
              bottomRight: Radius.circular(40)),
        ),
        child: Image.network(
          'https://source.unsplash.com/random',
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}

I have added card as it gives nice elevation effect to the image . FYI keep margin as 0 in card if you are using card.

Anish Vahora
  • 243
  • 3
  • 10
2

enter image description here

You can achieve it using FlexibleSpaceBar, here is the example code.

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: CustomScrollView(
      slivers: <Widget>[
        SliverAppBar(
          expandedHeight: 300,
          flexibleSpace: FlexibleSpaceBar(
            background: Image.asset("assets/images/chocolate.jpg", fit: BoxFit.cover),
          ),
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, index) => ListTile(title: Text("Item ${index}")),
            childCount: 100,
          ),
        ),
      ],
    ),
  );
}
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
1

When we using a ListView on a top tree,

The ListView makes a default padding.top even without a SafeArea.

So we need a padding: EdgeInsets.zero.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 15 '22 at 20:55
0

Tried all the answers, but either it only works on iOS, or they make against scaffold recommendations within scaffold (visit Multiple Scaffolds for each page inside a Flutter App).

The solution is to use SystemChrome.setSystemUIOverlayStyle.

Widget build(BuildContext context) {
    setBrightnessWithoutAppBar(context, AppColors.transparent, Brightness.light); //look this
     return Scaffold(
       extendBodyBehindAppBar: true,
       body: Stack(children: [
         Positioned(
           top: 0,
           child: Container(
             width: MedSize.widthPhysical,
             height: MedSize.heightPhysical * 0.7.byUI(true),
             decoration: const BoxDecoration(
               image: DecorationImage(
                 image: AssetImage('assets/images/jesus/lovesYou.jpg'),
                 fit: BoxFit.fitWidth,
               )

And in some new file you make your method available:

SystemUiOverlayStyle setBrightnessWithoutAppBar(BuildContext context, Color backgroundColor, Brightness brightness) {
  SystemUiOverlayStyle style = SystemUiOverlayStyle(
    statusBarColor: backgroundColor,
    statusBarIconBrightness: brightness,
    statusBarBrightness: brightness,
  );
  SystemChrome.setSystemUIOverlayStyle(style);
  return style;
}

It seems to me that until Flutter version 3.12, using the AppBar on Android, even with height 0, overlaps the body, even with the use of forceMaterialTransparency = true -regarding the use of image, of course.

  • Please don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates. See [Is it acceptable to add a duplicate answer to several questions?](https://meta.stackexchange.com/q/104227/206345) – Nick Aug 29 '23 at 23:32