【问题标题】:Get.height and Get.width are printing 0.0 on first time in release mode?Get.height 和 Get.width 在发布模式下第一次打印 0.0?
【发布时间】:2022-10-17 14:12:25
【问题描述】:

我在用Getx 状态管理对于我的项目。 Get.widthGet.height 正在打印0.0当使用setState((){}) 刷新屏幕后第一次构建小部件时,它会打印实际值。我通常已经声明并初始化了高度和宽度。

这是我的代码:

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      enableLog: true,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

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

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    print(Get.height);
    print(Get.width);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

调试输出:

发布输出:

首先,它在按下浮动按钮后打印值0.0,开始打印实际值。

我该如何解决这个问题?在我以前的项目中,它运行良好。

得到:^4.6.5

我的颤抖医生:

【问题讨论】:

    标签: flutter height width flutter-getx state-management


    【解决方案1】:

    根据 github 上的issues #2523,目前无法修复,但您可以尝试使用:

    MediaQuery.of(context).size.width
    MediaQuery.of(context).size.height
    

    有关Get and MediaQuery 的更多信息

    但是当 UI 尚未调整大小并在屏幕上布局时,可能会发生此错误

    【讨论】:

    • 在我之前的项目中,它运行良好。但是,我无法重现它。希望他们尽快解决这个问题。
    • 关注github,更新会提到
    【解决方案2】:

    在启动画面中,我使用了MediaQuery.of(context).size.widthMediaQuery.of(context).size.height 用于高度和宽度。然后在其他页面中,我使用了Get.widthGet.height。现在一切正常。如果Splash screen() 中没有高度和宽度属性,则使用MediaQuery.of(context).size.width 和 登录页面和主页中的MediaQuery.of(context).size.height。希望这能解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多