【发布时间】:2022-10-17 14:12:25
【问题描述】:
我在用Getx 状态管理对于我的项目。 Get.width 和 Get.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