使用不包含媒体查询的上下文调用 Flutter clippath 错误 mediquery.of()

Flutter clippath error mediquery.of() called with a context that does not contain a mediaquery

它说我的 mediaquery.of() 上下文错误主要是由于没有脚手架或没有 materialApp 造成的,但是你可以看到我的代码有这两者之一。另外,我也做了 context 或 this.context.

也不

我不能显示容器中的 Colors.red如果我 进行了 width/height 编辑,我会能够运行吧。然而;它只会显示带有应用栏的纯白色。

这是我的代码:

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: "UiUx",
        theme: ThemeData(),
        home: Scaffold(
            appBar: AppBar(
              title: Text("UiUx"),
            ),
            body: Container(
              child: ClipPath(
                clipper: MyClipper(),
                child: Container(
                  width: MediaQuery.of(context).size.width,
                  height: MediaQuery.of(context).size.height,
                  color: Colors.red,
                ),
              ),
            )));
  }
}

class MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var path = Path();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return true;
  }
}

这是错误:

Performing hot reload...
Syncing files to device vivo 1811...

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building MyApp(dirty, state: _MyAppState#9c3a2):
MediaQuery.of() called with a context that does not contain a MediaQuery.

No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
The context used was: MyApp
  dirty
  state: _MyAppState#9c3a2
The relevant error-causing widget was: 
  MyApp file:///D:/Programming/Flutter_Projects/uiuxstudy/lib/main.dart:4:23
When the exception was thrown, this was the stack: 
#0      MediaQuery.of (package:flutter/src/widgets/media_query.dart:813:5)
#1      _MyAppState.build (package:uiuxstudy/main.dart:25:37)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
#4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
Reloaded 1 of 495 libraries in 438ms.

我愿意听取一些提示和建议。专门寻找简约设计

您的 Material 应用放错了位置,它抛出该错误是因为没有可从中绘制上下文的父小部件。

将您的整个代码更改为:

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

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


class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: "UiUx",
        theme: ThemeData(),
        home: Scaffold(
            appBar: AppBar(
              title: Text("UiUx"),
            ),
            body: Builder(
              builder: (context) {
                return Container(
                  child: ClipPath(
                    clipper: MyClipper(),
                    child: Container(
                      width: MediaQuery.of(context).size.width,
                      height: MediaQuery.of(context).size.height,
                      color: Colors.red,
                    ),
                  ),
                );
              }
            )));
  }
}

class MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var path = Path();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return true;
  }
}

这应该有效。