dart 如何在Flutter中为Widget添加背景图片

c2e8gylq  于 11个月前  发布在  Flutter
关注(0)|答案(2)|浏览(120)

试图添加背景图像的小部件在Flutter应用程序,但我不知道如何添加它.我是新的flutter.So,如果有人知道请帮助找到解决方案.
login.dart:

Widget build(BuildContext context) {
  return Scaffold( 
  body: SingleChildScrollView(
      child: Padding(
      padding: const EdgeInsets.all(20.0),
      child: Column(
        children: [
          const SizedBox(
            height: 1,
          ),
          Padding(
            padding: const EdgeInsets.all(20.0),
            child: Image.asset("images/logo.png"),
          ),
        
          const SizedBox(height: 10),
          TextField(
            .........
          ),
          const SizedBox(height: 20),
          TextField(
            ..........
          ),
          const SizedBox(height: 20),
          TextField(
            ..........
          ) 
        ],
      ),
    ),
  ),
);
}

字符串

shyt4zoc

shyt4zoc1#

使用Container Package SingleChildScrollView并添加装饰

Scaffold(
resizeToAvoidBottomInset : false,
 body: Container(
  height: MediaQuery.of(context).size.height,
  width: MediaQuery.of(context).size.width,
   decoration: BoxDecoration(
     image : DecorationImage(
      image: AssetImage('imagePathHere'),
     )
   ),
    child: SingleChildScrollView(

    )
 )
)

字符串

eqoofvh9

eqoofvh92#

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/background_image.jpg'), 
            fit: BoxFit.cover,
          ),
        ),
        child: Center(
          child: Text(
            'Hello, Flutter!',
            style: TextStyle(
              color: Colors.white,
              fontSize: 24.0,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }
}

字符串

相关问题