Flutter SDK Set Background image

后端 未结 10 1625
礼貌的吻别
礼貌的吻别 2020-11-30 20:38

I am trying to set a background image for the home page. I am getting the image place from start of the screen and filling the width but not the height. Am I missing someth

相关标签:
10条回答
  • 2020-11-30 20:52
    decoration: BoxDecoration(
          image: DecorationImage(
            image: ExactAssetImage("images/background.png"),
            fit: BoxFit.cover
          ),
        ),
    

    this also works inside a container.

    0 讨论(0)
  • 2020-11-30 20:53

    If you use a Container as the body of the Scaffold, its size will be accordingly the size of its child, and usually that is not what you want when you try to add a background image to your app.

    Looking at this other question, @collin-jackson was also suggesting to use Stack instead of Container as the body of the Scaffold and it definitely does what you want to achieve.

    This is how my code looks like

    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        body: new Stack(
          children: <Widget>[
            new Container(
              decoration: new BoxDecoration(
                image: new DecorationImage(image: new AssetImage("images/background.jpg"), fit: BoxFit.cover,),
              ),
            ),
            new Center(
              child: new Text("Hello background"),
            )
          ],
        )
      );
    }
    
    0 讨论(0)
  • 2020-11-30 20:54

    You can use DecoratedBox.

    @override
    Widget build(BuildContext context) {
      return DecoratedBox(
        decoration: BoxDecoration(
          image: DecorationImage(image: AssetImage("your_asset"), fit: BoxFit.cover),
        ),
        child: Center(child: FlutterLogo(size: 300)),
      );
    }
    

    Output:

    0 讨论(0)
  • 2020-11-30 21:01

    I was able to apply a background below the Scaffold (and even it's AppBar) by putting the Scaffold under a Stack and setting a Container in the first "layer" with the background image set and fit: BoxFit.cover property.

    Both the Scaffold and AppBar has to have the backgroundColor set as Color.transparent and the elevation of AppBar has to be 0 (zero).

    Voilà! Now you have a nice background below the whole Scaffold and AppBar! :)

    import 'package:flutter/material.dart';
    import 'package:mynamespace/ui/shared/colors.dart';
    import 'package:mynamespace/ui/shared/textstyle.dart';
    import 'package:mynamespace/ui/shared/ui_helpers.dart';
    import 'package:mynamespace/ui/widgets/custom_text_form_field_widget.dart';
    
    class SignUpView extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Stack( // <-- STACK AS THE SCAFFOLD PARENT
          children: [
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage("assets/images/bg.png"), // <-- BACKGROUND IMAGE
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Scaffold(
              backgroundColor: Colors.transparent, // <-- SCAFFOLD WITH TRANSPARENT BG
              appBar: AppBar(
                title: Text('NEW USER'),
                backgroundColor: Colors.transparent, // <-- APPBAR WITH TRANSPARENT BG
                elevation: 0, // <-- ELEVATION ZEROED
              ),
              body: Padding(
                padding: EdgeInsets.all(spaceXS),
                child: Column(
                  children: [
                    CustomTextFormFieldWidget(labelText: 'Email', hintText: 'Type your Email'),
                    UIHelper.verticalSpaceSM,
                    SizedBox(
                      width: double.maxFinite,
                      child: RaisedButton(
                        color: regularCyan,
                        child: Text('Finish Registration', style: TextStyle(color: white)),
                        onPressed: () => {},
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        );
      }
    }
    
    0 讨论(0)
  • 2020-11-30 21:04

    To set a background image without shrinking after adding the child, use this code.

      body: Container(
        constraints: BoxConstraints.expand(),
          decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage("assets/aaa.jpg"),
            fit: BoxFit.cover,
            )
          ),
    
        //You can use any widget
        child: Column(
          children: <Widget>[],
        ),
        ),
    
    0 讨论(0)
  • 2020-11-30 21:08
    body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('images/background.png'),fit:BoxFit.cover
          )
        ),
    );
    
    0 讨论(0)
提交回复
热议问题