Flutter SDK Set Background image

后端 未结 10 1817
礼貌的吻别
礼貌的吻别 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 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: () => {},
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        );
      }
    }
    

提交回复
热议问题