Flutter 如何在颤振中使用柱的固有高度
我正努力在颤振中建立一个分数 我想让我的专栏呈现两个独立的容器:命名器和分母。我希望它们的大小和我的一样。我的意思是当提名人有另一个分数时,分母应该是相同的高度 是否可能有一个列包含两个元素,每个元素的大小取决于最高的元素 我不想使用“展开”,因为我是动态使用高度的。我在SingleChildScrollView中使用它 我无法预测确切的高度,因为想象一下这样的分数 1/2/3 我希望一个“3”物体的高度和“1/2”一样 我的代码:Flutter 如何在颤振中使用柱的固有高度,flutter,widget,height,Flutter,Widget,Height,我正努力在颤振中建立一个分数 我想让我的专栏呈现两个独立的容器:命名器和分母。我希望它们的大小和我的一样。我的意思是当提名人有另一个分数时,分母应该是相同的高度 是否可能有一个列包含两个元素,每个元素的大小取决于最高的元素 我不想使用“展开”,因为我是动态使用高度的。我在SingleChildScrollView中使用它 我无法预测确切的高度,因为想象一下这样的分数 1/2/3 我希望一个“3”物体的高度和“1/2”一样 我的代码: Column( children: [
Column(
children: [
IntrinsicWidth(
child: Column( children: [
numerator,
SizedBox(height: 5),
fractionBar,
SizedBox(height: 5),
denominator
],),
)
],
),
正如您所知道的,实际上在flatter中,一切都是一个
小部件
,这也意味着您可以将每个小部件
存储为变量,并使用getter获取参数
下面是一个示例代码:
import 'package:flutter/material.dart';
class Test extends StatelessWidget {
//saving the Container as variables, your nominator & dominator widgets could be placed as child's inside them
final Container nominator = Container(height: 50.0, color: Colors.purple);
final Container denominator = Container(height: 100.0, color: Colors.red);
double getContainerHeight () { //helper function for dynamically setting the height depending on which widget is bigger.
var _height;
nominator.constraints.minHeight < denominator.constraints.minHeight ?
_height = denominator.constraints.minHeight :
_height = nominator.constraints.minHeight;
return _height;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(height: 100.0),
Container(
height: getContainerHeight(),
color: Colors.green,
child: Center(child: nominator),
),
Container(
height: getContainerHeight(),
color: Colors.blue,
child: Center(child: denominator),
)
],
),
)
);
}
}
导入“包装:颤振/材料.省道”;
类测试扩展了无状态小部件{
//将容器保存为变量,您的命名器和支配器小部件可以作为子部件放置在其中
最终容器命名者=容器(高度:50.0,颜色:Colors.purple);
最终容器分母=容器(高度:100.0,颜色:Colors.red);
double-getContainerHeight(){//helper函数,用于根据哪个小部件更大来动态设置高度。
var_高度;
命名器.constraints.minHeight<分母.constraints.minHeight?
_高度=分母。约束。最小高度:
_高度=提名人.constraints.minHeight;
返回高度;
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:SingleChildScrollView(
子:列(
儿童:[
尺寸箱(高度:100.0),
容器(
高度:getContainerHeight(),
颜色:颜色。绿色,
儿童:中心(儿童:提名人),
),
容器(
高度:getContainerHeight(),
颜色:颜色,蓝色,
子对象:中心(子对象:分母),
)
],
),
)
);
}
}
在本例中,我使用Container
包装分母和命名器,以便轻松访问其高度参数:Container.constraints.minHeight
还要注意,命名符和分母是在列中的容器之前生成的
以下是输出:
正如您所知道的,实际上在flatter中,一切都是一个小部件
,这也意味着您可以将每个小部件
存储为变量,并使用getter获取参数
下面是一个示例代码:
import 'package:flutter/material.dart';
class Test extends StatelessWidget {
//saving the Container as variables, your nominator & dominator widgets could be placed as child's inside them
final Container nominator = Container(height: 50.0, color: Colors.purple);
final Container denominator = Container(height: 100.0, color: Colors.red);
double getContainerHeight () { //helper function for dynamically setting the height depending on which widget is bigger.
var _height;
nominator.constraints.minHeight < denominator.constraints.minHeight ?
_height = denominator.constraints.minHeight :
_height = nominator.constraints.minHeight;
return _height;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(height: 100.0),
Container(
height: getContainerHeight(),
color: Colors.green,
child: Center(child: nominator),
),
Container(
height: getContainerHeight(),
color: Colors.blue,
child: Center(child: denominator),
)
],
),
)
);
}
}
导入“包装:颤振/材料.省道”;
类测试扩展了无状态小部件{
//将容器保存为变量,您的命名器和支配器小部件可以作为子部件放置在其中
最终容器命名者=容器(高度:50.0,颜色:Colors.purple);
最终容器分母=容器(高度:100.0,颜色:Colors.red);
double-getContainerHeight(){//helper函数,用于根据哪个小部件更大来动态设置高度。
var_高度;
命名器.constraints.minHeight<分母.constraints.minHeight?
_高度=分母。约束。最小高度:
_高度=提名人.constraints.minHeight;
返回高度;
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:SingleChildScrollView(
子:列(
儿童:[
尺寸箱(高度:100.0),
容器(
高度:getContainerHeight(),
颜色:颜色。绿色,
儿童:中心(儿童:提名人),
),
容器(
高度:getContainerHeight(),
颜色:颜色,蓝色,
子对象:中心(子对象:分母),
)
],
),
)
);
}
}
在本例中,我使用Container
包装分母和命名器,以便轻松访问其高度参数:Container.constraints.minHeight
还要注意,命名符和分母是在列中的容器之前生成的
以下是输出:
也许有一些图像描述了你的问题?@pskink想象一下,分子比分母高2倍。我希望这两个元素都有分子的高度。我想让它自动和动态计算。如果分母高于分子,则分子应与分母一样高。这就是为什么我想在列中使用IntriniScheight,但它似乎不起作用。它不起作用,因为它说高度没有定义,列是SingleChildScrollView的子级之一,它可以无限扩展。我从未尝试过后台
,但它的文档说:“后台可用于测量小部件的尺寸,而无需将其显示在屏幕上。“……谢谢!检查过了,但是没有关于如何用它测量维度的答案。也许有一些图像描述了你的问题?@pskink想象一下分子比分母高2倍。我希望这两个元素都有分子的高度。我想让它自动和动态计算。如果分母高于分子,则分子应与分母一样高。这就是为什么我想在列中使用IntriniScheight,但它似乎不起作用。它不起作用,因为它说高度没有定义,列是SingleChildScrollView的子级之一,它可以无限扩展。我从未尝试过后台
,但它的文档说:“后台可用于测量小部件的尺寸,而无需将其显示在屏幕上。”…谢谢!已检查,但没有关于如何使用它测量尺寸的答案。感谢您提供如此详细的说明