簡體   English   中英

(使用不包含 MediaQuery 的上下文調用 MediaQuery.of()。)錯誤

[英](MediaQuery.of() called with a context that does not contain a MediaQuery.) error

我的 flutter 代碼是,此代碼是通過導出 Adobe XD 文件並添加默認main.dart代碼通過 Adobe XD 生成並通過導出放入 VS 代碼。 它在調試控制台中顯示此錯誤請幫助使用此代碼。

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:adobe_xd/blend_mask.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffffffff),
      body: Stack(
        children: <Widget>[
          Transform.translate(
            offset: Offset(-6.0, -9.0),
            child: SvgPicture.string(
              _svg_hscwvz,
              allowDrawingOutsideViewBox: true,
            ),
          ),
          Transform.translate(
            offset: Offset(-48.0, 65.0),
            child: SizedBox(
              width: 424.0,
              height: 266.0,
              child: Text.rich(
                TextSpan(
                  style: TextStyle(
                    fontFamily: 'Google Sans',
                    fontSize: 30,
                    color: const Color(0xff707070),
                  ),
                  children: [
                    TextSpan(
                      text: '               ',
                    ),
                    TextSpan(
                      text: 'DO YOU KNOW?\n',
                      style: TextStyle(
                        fontWeight: FontWeight.w700,
                      ),
                    ),
                    TextSpan(
                      text:
                          '\n             YOU CAN BETTER THE\n           PERFORMANCE OF YOUR\n              EARPHONE BY 63%.\n\n             JUST BY USING THIS APP',
                    ),
                  ],
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
          Transform.translate(
            offset: Offset(18.0, 433.0),
            child: SizedBox(
              width: 14.0,
              child: Text(
                '',
                style: TextStyle(
                  fontFamily: 'Google Sans',
                  fontSize: 30,
                  color: const Color(0xff707070),
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
          Transform.translate(
            offset: Offset(103.0, 402.0),
            child: SizedBox(
              width: 206.0,
              height: 114.0,
              child: Text(
                'JUST FOLLOW \nTHESE SIMPLE \nSTEPS …',
                style: TextStyle(
                  fontFamily: 'Google Sans',
                  fontSize: 30,
                  color: const Color(0xff707070),
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
          // Adobe XD layer: 'graphs' (group)
          BlendMask(
            blendMode: BlendMode.multiply,
            opacity: 1,
            region: Offset(-64.0, 554.9) & Size(495.0, 149.2),
            child: Stack(
              children: <Widget>[
                Transform.translate(
                  offset: Offset(-64.0, 554.86),
                  child: SvgPicture.string(
                    _svg_6t608q,
                    allowDrawingOutsideViewBox: true,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
    return MaterialApp(
      title: 'Ehtasham app',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
const String _svg_hscwvz =
    '<svg viewBox="-6.0 -9.0 424.0 866.0" ><defs><linearGradient id="gradient" x1="0.5" y1="0.2911" x2="0.5" y2="1.0"><stop offset="0.0" stop-color="#ff4fffb7"  /><stop offset="1.0" stop-color="#ff5173fb"  /><stop offset="1.0" stop-color="#ff76998b"  /><stop offset="1.0" stop-color="#ff808080"  /></linearGradient></defs><path transform="translate(-6.0, -9.0)" d="M 0 0 L 424 0 L 424 866 L 0 866 L 0 0 Z" fill="url(#gradient)" stroke="#090909" stroke-width="1" stroke-miterlimit="4" stroke-linecap="butt" /></svg>';
const String _svg_6t608q =
    '<svg viewBox="-64.0 554.9 495.0 149.2" ><path transform="translate(-1146.85, -6457.22)" d="M 1533.19384765625 7060.17333984375 C 1464.86669921875 7058.81982421875 1463.255249023438 7082.07958984375 1418.307250976562 7082.07958984375 C 1373.359252929688 7082.07958984375 1346.678833007812 7047.59765625 1300.068969726562 7047.59765625 C 1253.459106445312 7047.59765625 1239.491333007812 7052.82373046875 1189.326782226562 7052.82373046875 C 1139.162231445312 7052.82373046875 1082.85009765625 7012.07958984375 1082.85009765625 7012.07958984375 C 1082.85009765625 7012.07958984375 1082.85009765625 7161.27685546875 1082.85009765625 7161.27685546875 C 1082.85009765625 7161.27685546875 1533.19384765625 7161.27685546875 1533.19384765625 7161.27685546875 C 1533.19384765625 7161.27685546875 1533.19384765625 7060.17333984375 1533.19384765625 7060.17333984375 Z" fill="none" fill-opacity="0.05" stroke="#2699fb" stroke-width="10" stroke-opacity="0.05" stroke-miterlimit="4" stroke-linecap="butt" /><path transform="translate(-1150.85, -6434.0)" d="M 1533.19384765625 7062.5625 C 1464.86669921875 7061.75927734375 1470.591186523438 7037.07421875 1425.643188476562 7037.07421875 C 1380.695190429688 7037.07421875 1362.970458984375 7074.9140625 1295.2060546875 7074.9140625 C 1258.198974609375 7074.9140625 1227.359741210938 7037.07421875 1193.514282226562 7037.07421875 C 1159.668579101562 7037.07421875 1112.85009765625 7054.0546875 1112.85009765625 7054.0546875 C 1112.85009765625 7054.0546875 1112.85009765625 7109.0546875 1112.85009765625 7109.0546875 C 1112.85009765625 7109.0546875 1533.19384765625 7109.0546875 1533.19384765625 7109.0546875 C 1533.19384765625 7109.0546875 1533.19384765625 7062.5625 1533.19384765625 7062.5625 Z" fill="none" fill-opacity="0.2" stroke="#2699fb" stroke-width="5" stroke-opacity="0.2" stroke-miterlimit="4" stroke-linecap="butt" /><path transform="translate(-1181.85, -6419.0)" d="M 1142.850219726562 7049.0546875 C 1221.619995117188 7048.25146484375 1220.568115234375 7001.95703125 1272.385375976562 7001.95703125 C 1324.20263671875 7001.95703125 1339.0888671875 7071.9140625 1417.209594726562 7071.9140625 C 1472.696044921875 7071.9140625 1491.203125 7032.0546875 1530.221313476562 7032.0546875 C 1569.239501953125 7032.0546875 1612.850219726562 7049.0546875 1612.850219726562 7049.0546875 C 1612.850219726562 7049.0546875 1612.850219726562 7109.0546875 1612.850219726562 7109.0546875 C 1612.850219726562 7109.0546875 1142.850219726562 7109.0546875 1142.850219726562 7109.0546875 C 1142.850219726562 7109.0546875 1142.850219726562 7049.0546875 1142.850219726562 7049.0546875 Z" fill="none" fill-opacity="0.5" stroke="#2699fb" stroke-width="2" stroke-opacity="0.5" stroke-miterlimit="4" stroke-linecap="butt" /></svg>';

它在調試控制台中顯示此錯誤,請幫助處理此代碼。

I/flutter (24258): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (24258): The following assertion was thrown building MyApp:
I/flutter (24258): MediaQuery.of() called with a context that does not contain a MediaQuery.
I/flutter (24258): No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of().
I/flutter (24258): This can happen because you do not have a WidgetsApp or MaterialApp widget (those widgets introduce
I/flutter (24258): a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.
I/flutter (24258): The context used was:
I/flutter (24258):   Scaffold
I/flutter (24258):
I/flutter (24258): The relevant error-causing widget was:
I/flutter (24258):   MyApp 
lib\main.dart:6
I/flutter (24258):
I/flutter (24258): When the exception was thrown, this was the stack:
I/flutter (24258): ...     Normal element mounting (7 frames)
I/flutter (24258): (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
I/flutter (24258):
I/flutter (24258): ════════════════════════════════════════════════════════════════════════════════════════════════════

代碼通過 Adobe XD 生成並通過導出放入 VS 代碼。

您需要將生成的代碼放在 Material 應用程序中。 我將所有代碼移至MyHomePage

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:adobe_xd/blend_mask.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Ehtasham app',
      theme: ThemeData(
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffffffff),
      body: Stack(
        children: <Widget>[
          Transform.translate(
            offset: Offset(-6.0, -9.0),
            child: SvgPicture.string(
              _svg_hscwvz,
              allowDrawingOutsideViewBox: true,
            ),
          ),
          Transform.translate(
            offset: Offset(-48.0, 65.0),
            child: SizedBox(
              width: 424.0,
              height: 266.0,
              child: Text.rich(
                TextSpan(
                  style: TextStyle(
                    fontFamily: 'Google Sans',
                    fontSize: 30,
                    color: const Color(0xff707070),
                  ),
                  children: [
                    TextSpan(
                      text: '               ',
                    ),
                    TextSpan(
                      text: 'DO YOU KNOW?\n',
                      style: TextStyle(
                        fontWeight: FontWeight.w700,
                      ),
                    ),
                    TextSpan(
                      text:
                          '\nYOU CAN BETTER THE\n  PERFORMANCE OF YOUR\n              EARPHONE BY 63%.\n\n             JUST BY USING THIS APP',
                    ),
                  ],
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
          Transform.translate(
            offset: Offset(18.0, 433.0),
            child: SizedBox(
              width: 14.0,
              child: Text(
                '',
                style: TextStyle(
                  fontFamily: 'Google Sans',
                  fontSize: 30,
                  color: const Color(0xff707070),
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
          Transform.translate(
            offset: Offset(103.0, 402.0),
            child: SizedBox(
              width: 206.0,
              height: 114.0,
              child: Text(
                'JUST FOLLOW \nTHESE SIMPLE \nSTEPS …',
                style: TextStyle(
                  fontFamily: 'Google Sans',
                  fontSize: 30,
                  color: const Color(0xff707070),
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ),
          // Adobe XD layer: 'graphs' (group)
          BlendMask(
            blendMode: BlendMode.multiply,
            opacity: 1,
            region: Offset(-64.0, 554.9) & Size(495.0, 149.2),
            child: Stack(
              children: <Widget>[
                Transform.translate(
                  offset: Offset(-64.0, 554.86),
                  child: SvgPicture.string(
                    _svg_6t608q,
                    allowDrawingOutsideViewBox: true,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
const String _svg_hscwvz =
    '<svg viewBox="-6.0 -9.0 424.0 866.0" ><defs><linearGradient id="gradient" x1="0.5" y1="0.2911" x2="0.5" y2="1.0"><stop offset="0.0" stop-color="#ff4fffb7"  /><stop offset="1.0" stop-color="#ff5173fb"  /><stop offset="1.0" stop-color="#ff76998b"  /><stop offset="1.0" stop-color="#ff808080"  /></linearGradient></defs><path transform="translate(-6.0, -9.0)" d="M 0 0 L 424 0 L 424 866 L 0 866 L 0 0 Z" fill="url(#gradient)" stroke="#090909" stroke-width="1" stroke-miterlimit="4" stroke-linecap="butt" /></svg>';
const String _svg_6t608q =
    '<svg viewBox="-64.0 554.9 495.0 149.2" ><path transform="translate(-1146.85, -6457.22)" d="M 1533.19384765625 7060.17333984375 C 1464.86669921875 7058.81982421875 1463.255249023438 7082.07958984375 1418.307250976562 7082.07958984375 C 1373.359252929688 7082.07958984375 1346.678833007812 7047.59765625 1300.068969726562 7047.59765625 C 1253.459106445312 7047.59765625 1239.491333007812 7052.82373046875 1189.326782226562 7052.82373046875 C 1139.162231445312 7052.82373046875 1082.85009765625 7012.07958984375 1082.85009765625 7012.07958984375 C 1082.85009765625 7012.07958984375 1082.85009765625 7161.27685546875 1082.85009765625 7161.27685546875 C 1082.85009765625 7161.27685546875 1533.19384765625 7161.27685546875 1533.19384765625 7161.27685546875 C 1533.19384765625 7161.27685546875 1533.19384765625 7060.17333984375 1533.19384765625 7060.17333984375 Z" fill="none" fill-opacity="0.05" stroke="#2699fb" stroke-width="10" stroke-opacity="0.05" stroke-miterlimit="4" stroke-linecap="butt" /><path transform="translate(-1150.85, -6434.0)" d="M 1533.19384765625 7062.5625 C 1464.86669921875 7061.75927734375 1470.591186523438 7037.07421875 1425.643188476562 7037.07421875 C 1380.695190429688 7037.07421875 1362.970458984375 7074.9140625 1295.2060546875 7074.9140625 C 1258.198974609375 7074.9140625 1227.359741210938 7037.07421875 1193.514282226562 7037.07421875 C 1159.668579101562 7037.07421875 1112.85009765625 7054.0546875 1112.85009765625 7054.0546875 C 1112.85009765625 7054.0546875 1112.85009765625 7109.0546875 1112.85009765625 7109.0546875 C 1112.85009765625 7109.0546875 1533.19384765625 7109.0546875 1533.19384765625 7109.0546875 C 1533.19384765625 7109.0546875 1533.19384765625 7062.5625 1533.19384765625 7062.5625 Z" fill="none" fill-opacity="0.2" stroke="#2699fb" stroke-width="5" stroke-opacity="0.2" stroke-miterlimit="4" stroke-linecap="butt" /><path transform="translate(-1181.85, -6419.0)" d="M 1142.850219726562 7049.0546875 C 1221.619995117188 7048.25146484375 1220.568115234375 7001.95703125 1272.385375976562 7001.95703125 C 1324.20263671875 7001.95703125 1339.0888671875 7071.9140625 1417.209594726562 7071.9140625 C 1472.696044921875 7071.9140625 1491.203125 7032.0546875 1530.221313476562 7032.0546875 C 1569.239501953125 7032.0546875 1612.850219726562 7049.0546875 1612.850219726562 7049.0546875 C 1612.850219726562 7049.0546875 1612.850219726562 7109.0546875 1612.850219726562 7109.0546875 C 1612.850219726562 7109.0546875 1142.850219726562 7109.0546875 1142.850219726562 7109.0546875 C 1142.850219726562 7109.0546875 1142.850219726562 7049.0546875 1142.850219726562 7049.0546875 Z" fill="none" fill-opacity="0.5" stroke="#2699fb" stroke-width="2" stroke-opacity="0.5" stroke-miterlimit="4" stroke-linecap="butt" /></svg>';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM