我正在寻找计算两种情况下可用的有效空间:(01)当MIDI键盘不是MIDI,和(02)当MIDI键盘是MIDI.effective space我指的是通过扣除AppBar占用的空间来获得的空间(也许这也会覆盖顶部状态栏?),然后是下面的TextField
,和底部的MIDI键盘(当MIDI)和底部的工具?/导航?wine 吧
我试过问题底部列出的sources个代码.到目前为止,我能够得到设备的物理和逻辑高度,逻辑高度的MIDI键盘,但我卡住了部分—获得的逻辑高度的小部件.我试过实施GlobalKey
,但没有成功.我无法得到高度,并且发生了一个错误,我没有成功地解决.错误的位置包含在以下代码的多行注释中.错误说—"属性'size'不能被无条件访问,因为接收器可以是null.... ".我try 使用?
和!
操作符来解决空条件,但没有任何工作.
如何解决这个问题以获得小部件的高度?此外,对于我的目标提出建议是最受欢迎的.
import 'dart:ui';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
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> {
final GlobalKey? _key01 = GlobalKey();
late final TextEditingController firstController;
String result = "";
String effective_available_screen_height = "";
String dimensions = "";
FlutterView view = WidgetsBinding.instance.platformDispatcher.views.first;
late Size size;
double width = 0;
double height = 0;
double height01 = 0;
@override
void initState() {
super.initState();
firstController = TextEditingController();
}
@override
void dispose() {
firstController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
Size ph_size = view.physicalSize;
double ph_height = ph_size.height;
size = MediaQuery.of(context).size;
width = size.width;
height = size.height;
final padding = MediaQuery.of(context).viewPadding;
height01 = height - padding.top - padding.bottom;
effective_available_screen_height = "$height01";
// Height of Keyboard when onscreen (ohterwise zero):
double keyboard_height = MediaQuery.of(context).viewInsets.bottom;
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Container(
padding: const EdgeInsets.all(5.0),
child: Column(
children: [
Container(
child: Row(
children: [
Expanded(
flex: 5,
child: Container(
padding: const EdgeInsets.all(5.0),
child: TextField(
key: _key01,
style: const TextStyle(
fontSize: 16,
),
controller: firstController,
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
result = value;
/*
// The following gave error:
dimensions =
"${_key01?.currentContext.size.height}";
*/
dimensions = "${_key01?.currentContext}";
});
},
),
),
),
const Expanded(
flex: 5,
child: Text("Input Value"),
),
],
),
),
Text("Result = $result"),
Text("Total physical height = $ph_height"),
Text("Total logical height = $height"),
Text("Onscreen Keyboard height = $keyboard_height"),
Text(
"Working Height Available (logical) = $effective_available_screen_height"),
Text("Dimensions: $dimensions"),
],
),
),
);
}
}
Sources of Help: