Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit image as the background of status bar in flutter?

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,
              ),
            ),
          )
        ],
      ),
    );
  }
}
like image 772
Tushar Rai Avatar asked Jul 03 '19 06:07

Tushar Rai


People also ask

How do I customize my status bar in Flutter?

To change status bar color in Flutter, you should set the systemOverlayStyle . Inside the SystemUiOverlayStyle, you can use properties to specify the status bar color as well as the color for the icon and text.


Video Answer


2 Answers

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:

like image 168
Rezaul Islam Avatar answered Oct 13 '22 18:10

Rezaul Islam


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.

like image 39
Anish Vahora Avatar answered Oct 13 '22 18:10

Anish Vahora