首页 > 解决方案 > 如何在颤动中固定背景图像的大小和位置?

问题描述

Widget build(BuildContext context){
    var height2 = AppBar().preferredSize.height;
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        children: <Widget>[
          Image(
              image: AssetImage("assets/oral_structure.png"),
              fit: BoxFit.fill
          ),
          Padding(
          padding: EdgeInsets.only(top:100, left: 100),
          child: Container(
              height: 60,
              width: 60,
              child: FlatButton(
                child: Text('ㅂ', style: TextStyle(fontSize: 30,fontWeight: FontWeight.bold)),

                onPressed: (){
                  Navigator.push(context, MaterialPageRoute(builder: (context) => B()),);
                },
              )
          ),
          ),

以上是我的代码的一部分。我正在制作的应用程序是将适当的按钮放置在背景图像顶部的位置。但是,如果您进行上述代码,则按钮的位置和图像的位置会根据智能手机的屏幕尺寸而有所不同。固定高度和宽度也是如此。怎么没有办法?

标签: flutterflutter-layoutflutter-widget

解决方案


@override
Widget build(BuildContext context) {
    
    return Scaffold(
        key: v.scaffoldKey,
        body: Stack(
            children: [
            getBackgroundLight(),
            ],
        ),
    );
} 

Widget getBackgroundLight() {
    return Stack(
        children: [
        Image.asset('assets/background.jpg', width: double.infinity),
        Transform.rotate(
            angle: -SizeConfig.calculateBlockVertical(180) *
              (math.pi / SizeConfig.calculateBlockVertical(180)),
              child: Container(
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.center,
                    end: Alignment.bottomCenter,
                    colors: [
                    Get.theme.scaffoldBackgroundColor,
                    Get.theme.scaffoldBackgroundColor.withOpacity(0.5),
                    ],
                  ),
                ),
              ),
            ),
        ],
    );
}

推荐阅读