flutter 背景装饰覆盖其他元素的小部件

jfewjypa  于 2023-01-14  发布在  Flutter
关注(0)|答案(1)|浏览(157)

我刚接触flutter(和一般编程),遇到了以下问题:
我试着把我应用到屏幕背景的装饰变成一个小部件。它显然起作用了,因为效果是应用到容器的,但也导致它完全覆盖了所有其他元素。
这是代码(结果只是梯度覆盖整个屏幕):

import 'package:flutter/material.dart';
import 'widgets/background.dart';
import 'widgets/getCPF.dart';
import 'widgets/loginButton.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}
//Login Page
class _LoginPageState extends State<LoginPage> {
  @override
  Widget build(BuildContext context) {
    return Material(
    //Calling the Widget to decorate the background Container
      child: background(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Image.asset(
              'lib/assets/images/logo.png',
              color: const Color.fromARGB(255, 255, 255, 255),
            ),
            const Text(
              'Login',
              style: TextStyle(
                color: Colors.white,
                fontSize: 30,
                fontWeight: FontWeight.bold,
              ),
            ),
            getCPF(),
            loginButton()
          ],
        ),
      ),
    );
  }
}

import 'package:flutter/material.dart';
//Background decoration Widget
Widget background({required Column child}) {
  return Container(
    padding: const EdgeInsets.all(20),
    alignment: Alignment.topCenter,
    decoration: const BoxDecoration(
      gradient: LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [
          Color(0xff0b4257),
          Color(0xcc0b4257),
          Color(0x990b4257),
          Color(0x660b4257),
        ],
      ),
    ),
  );
}

当我放置background()Widget的内容而不是调用它时,它就像预期的那样工作,getCPF()文本输入和loginButton()位于后台。
结果

b5buobof

b5buobof1#

只需在后台小部件中传递子构造函数,在代码中不使用它,并将构造函数更改为required Widget child

Widget background({required Column child}) {
  return Container(
    padding: const EdgeInsets.all(20),
    alignment: Alignment.topCenter,
    decoration: const BoxDecoration(
      gradient: LinearGradient(
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
        colors: [
          Color(0xff0b4257),
          Color(0xcc0b4257),
          Color(0x990b4257),
          Color(0x660b4257),
        ],
      ),
    ),
   child: child  // here is the line you need to add
  );
}

相关问题