Make AppBar transparent and show background image which is set to whole screen

后端 未结 9 1437
滥情空心
滥情空心 2020-12-28 12:42

I have added AppBar in my flutter application. My screen already have a background image, where i don\'t want to set appBar color or don\'t want set separat

相关标签:
9条回答
  • 2020-12-28 12:59

    You can use Scaffold's property "extendBodyBehindAppBar: true" Don't forget to wrap child with SafeArea

      @Override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              widget.title,
              style: TextStyle(color: Colors.black),
            ),
            backgroundColor: Colors.transparent,
            elevation: 0.0,
          ),
          extendBodyBehindAppBar: true,
          body: Container(
            width: double.infinity,
            height: double.infinity,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/background/home.png'),
                fit: BoxFit.cover,
              ),
            ),
            child: SafeArea(
                child: Center(
              child: Container(
                width: 300,
                height: 300,
                decoration: BoxDecoration(
                  color: Colors.green,
                ),
                child: Center(child: Text('Test')),
              ),
            )),
          ),
        );
      }
    

    0 讨论(0)
  • 2020-12-28 12:59

    You can Try this This code work for me

    @override
      Widget build(BuildContext context) {
        _buildContext = context;
        sw = MediaQuery.of(context).size.width;
        sh = MediaQuery.of(context).size.height;
    
        return new Container(
          child: new Stack(
            children: <Widget>[
              new Container(
                child: Stack(
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.all(20.0),
                      decoration: BoxDecoration(image: backgroundImage),
                    ),
                  ],
                ),
              ),
              new Scaffold(
                backgroundColor: Colors.transparent,
                appBar: new AppBar(
                  title: new Text(Strings.page_register),
                  backgroundColor: Colors.transparent,
                  elevation: 0.0,
                  centerTitle: true,
                ),
                body: SingleChildScrollView(
                  padding: EdgeInsets.all(20.0),
                  physics: BouncingScrollPhysics(),
                  scrollDirection: Axis.vertical,
                  child: new Form(
                    key: _formKey,
                    autovalidate: _autoValidate,
                    child: FormUI(),
                  ),
                ),
              )
            ],
          ),
        );
      }
    

    backgroundImage

    DecorationImage backgroundImage = new DecorationImage(
      image: new ExactAssetImage('assets/images/welcome_background.png'),
      fit: BoxFit.cover,
    );
    
    0 讨论(0)
  • 2020-12-28 13:00

    you can use Stack widget to do so. Follow below example.

    import 'package:flutter/material.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Home(),
        );
      }
    }
    
    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Stack(
            children: <Widget>[
              Scaffold(
                backgroundColor: Colors.transparent,
                appBar: new AppBar(
                  title: new Text(
                    "Hello World",
                    style: TextStyle(color: Colors.amber),
                  ),
                  backgroundColor: Colors.transparent,
                  elevation: 0.0,
                ),
                body: new Container(
                  color: Colors.red,
                ),
              ),
            ],
          ),
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-28 13:00

    In my case I did it as follows:

    Additional create an app bar with a custom back button (in this case with a FloatingActionButton). You can still add widgets inside the Stack.

    class Home extends StatefulWidget {
      @override
      _EditProfilePageState createState() => _EditProfilePageState();
    }
    
    class _HomeState extends State< Home > {
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Stack(
            children: <Widget>[
              this._backgroundImage(), // --> Background Image
              Positioned( // --> App Bar
                child: AppBar(
                  backgroundColor: Colors.transparent,
                  elevation: 0.0,
                  leading: Padding( // --> Custom Back Button
                    padding: const EdgeInsets.all(8.0),
                    child: FloatingActionButton(
                      backgroundColor: Colors.white,
                      mini: true,
                      onPressed: this._onBackPressed,
                      child: Icon(Icons.arrow_back, color: Colors.black),
                    ),
                  ),
                ),
              ),
              // ------ Other Widgets ------
            ],
          ),
        );
      }
    
      Widget _backgroundImage() {
        return Container(
          height: 272.0,
          width: MediaQuery.of(context).size.width,
          child: FadeInImage(
            fit: BoxFit.cover,
            image: NetworkImage(
                'https://images.unsplash.com/photo-1527555197883-98e27ca0c1ea?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80'),
            placeholder: AssetImage('assetName'),
          ),
        );
      }
    
      void _onBackPressed() {
        Navigator.of(context).pop();
      }
    }
    

    In the following link you can find more information Link

    0 讨论(0)
  • 2020-12-28 13:04

    Output:

    A lot of answers but nobody explains why extendBodyBehindAppBar works? It works because when we assigned extendBodyBehindAppBar as true, then the body of the widget takes the height of AppBar, and we see an image covering the AppBar area.

    Simple Example:

    Size size = MediaQuery.of(context).size;
        return Scaffold(
          extendBodyBehindAppBar: true,
          body: Container(
            // height: size.height * 0.3,
            child: Image.asset(
              'shopping_assets/images/Fruits/pineapple.png',
              fit: BoxFit.cover,
              height: size.height * 0.4,
              width: size.width,
            ),
          ),
        );
    
    0 讨论(0)
  • 2020-12-28 13:10

    that's what I did and it's working

    This is supported by Scaffold now (in stable - v1.12.13+hotfix.5).

    Set Scaffold extendBodyBehindAppBar to true, Set AppBar elevation to 0 to get rid of shadow, Set AppBar backgroundColor transparency as needed.

    Best regards

    0 讨论(0)
提交回复
热议问题