flutter 代码告诉我BoxFit没有定义

6ss1mwsb  于 2023-10-22  发布在  Flutter
关注(0)|答案(2)|浏览(153)

我想用Flutter做个应用第一次,我犯了一个错误:BoxFit isn't a type当我尝试向代码中添加背景图像时发生错误。我不知道该怎么修。
完整错误../flutter/packages/flutter/lib/src/painting/decoration_image.dart:485:3: Error: 'BoxFit' isn't a type. BoxFit? fit, ^^^^^^ ../flutter/packages/flutter/lib/src/painting/decoration_image.dart:511:33: Error: Undefined name 'BoxFit'. fit ??= centerSlice == null ? BoxFit.scaleDown : BoxFit.fill;
这里是代码

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

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


class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home:
          // ignore: sort_child_properties_last
          DecoratedBox(
        decoration: const BoxDecoration(
          image: DecorationImage(
              image: AssetImage("./assets/Trashban_background.svg"),
              centerSlice: Rect.fromLTRB(50, 50, 50, 50)),
        ),
        child: Center(
            child: Scaffold(
          body: Container(
              padding: const EdgeInsets.only(top: 150),
              child: const TitleText()),
        )),
      ),
    );
  }
}
htrmnn0y

htrmnn0y1#

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart'; // Import the flutter_svg package

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DecoratedBox(
        decoration: BoxDecoration(
          image: DecorationImage(
            // Use SvgPicture.asset to display the SVG image
            image: SvgPicture.asset(
              "assets/trashban_background.svg", // Adjust the path as needed
              semanticsLabel: 'SVG Image', // Provide a label for accessibility
            ),
            centerSlice: Rect.fromLTRB(50, 50, 50, 50),
          ),
        ),
        child: Center(
          child: Scaffold(
            body: Container(
              padding: const EdgeInsets.only(top: 150),
              child: const TitleText(),
            ),
          ),
        ),
      ),
    );
  }
}

我添加了fit = BoxFit.fill,尝试这个,让我知道如果这解决了你的问题.

6yjfywim

6yjfywim2#

在构建Flutter应用程序时,一定要记住以下几点:
1.为主页创建单独的无状态/有状态小部件而不是使用相同的MyApp小部件是一个很好的编程实践。

  1. Scaffold应该在开头。
  2. AssetImage不接受.svg图像
    下面是修改后的代码:
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: DecoratedBox(
          decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage('assets/images/protruding-squares.png'),
                centerSlice: Rect.fromLTRB(50, 50, 50, 50),
            ),
          ),
          child: Center(
            child: Container(
              padding: const EdgeInsets.only(top: 150),
              child: const Text('Hello World'),
            ),
          ),
        ),
      ),
    );
  }
}

不要忘记在pubspec.yaml文件中添加您的资产

assets:
    - assets/images/protruding-squares.png

因此输出:

相关问题