我有一个这样的Expanded小部件列:
return new Container(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Expanded(
flex: 1,
child: convertFrom,
),
new Expanded(
flex: 1,
child: convertTo,
),
new Expanded(
flex: 1,
child: description,
),
],
),
);
Run Code Online (Sandbox Code Playgroud)
convertFrom,包括TextField.当我点击此文本字段时,Android键盘将出现在屏幕上.这会更改屏幕大小,因此小部件会像这样调整大小:

有没有办法让键盘"覆盖"屏幕,以便我的列不调整大小?如果我不使用Expanded小部件并为每个小部件硬编码高度,则小部件不会调整大小,但是当键盘出现时(因为没有足够的空间),我会收到黑黄条纹错误.对于所有屏幕尺寸,这也不灵活.
我不确定这是特定于Android还是特定于Flutter.
我试图将一个小部件置于底部中心位于Column的底部,但它始终与左侧对齐.
return new Column(
new Stack(
new Positioned(
bottom: 0.0,
new Center(
new Container(),
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
Positioned的存在迫使Container向左,而不是居中.但是,移除"定位"可将容器置于中间位置.
可能有一些我不知道的东西.是否有一个属性可以更改Flutter应用程序中所有文本的颜色?
我现在这样做的方式是,在我的MaterialApp中:
theme: ThemeData(
textTheme: Theme.of(context).textTheme.copyWith(
body1:
Theme.of(context).textTheme.body1.apply(color: Colors.pink),
body2:
Theme.of(context).textTheme.body2.apply(color: Colors.pink),
display1:
Theme.of(context).textTheme.display1.apply(color: Colors.pink),
display2:
Theme.of(context).textTheme.display2.apply(color: Colors.pink),
... // and so on
),
),
),
Run Code Online (Sandbox Code Playgroud)
我也试过了
textTheme: Theme.of(context).textTheme.apply(bodyColor: Colors.pink),
Run Code Online (Sandbox Code Playgroud)
但这适用于下拉文本,而不是常规文本.同样,displayColor适用于appBar文本和InputDecoration文本,但不适用于常规文本.我似乎没有任何decorationText代码,所以我不确定那是什么.
我注意到有一个textSelectionColor属性但只适用于TextField小部件.
我的图像与设备屏幕的宽高比不匹配.我想拉伸图像,使其完全填满屏幕,我不想裁剪图像的任何部分.
CSS具有百分比的概念,因此我可以将高度和宽度设置为100%.但看起来Flutter似乎没有这个概念,只是硬编码高度和宽度是不好的,所以我被卡住了.
这就是我所拥有的(我使用的是Stack,因为我在图像的前景中有一些东西):
Widget background = new Container(
height: // Not sure what to put here!
width: // Not sure what to put here!
child: new Image.asset(
asset.background,
fit: BoxFit.fill, // I thought this would fill up my Container but it doesn't
),
);
return new Stack(
children: <Widget>[
background,
foreground,
],
);
Run Code Online (Sandbox Code Playgroud) 我正在按照Flutter Networking/HTTP教程对我的localhost:8000上运行的服务器执行GET请求.通过我的浏览器访问我的本地主机工作正常.我的代码看起来像这样:
var url = 'http://localhost:8000';
Future<String> getUnits(String category) async {
var response = await httpClient.get('$url/$category');
return response.body;
}
Run Code Online (Sandbox Code Playgroud)
当我指向任何真实的URL时,这可以正常工作,例如https://example.com,当我指向https://localhost:8000或https://localhost(或这些的任何变体)时,我得到一个错误,从以下开始:
E/flutter ( 4879): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 4879): SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 47060
E/flutter ( 4879): #0 IOClient.send (package:http/src/io_client.dart:30:23)
Run Code Online (Sandbox Code Playgroud)
每次重新加载应用程序时,上述错误中的端口都会更改.我查看了http包代码,似乎没有办法为URL指定端口.我如何指向我的本地主机?
我从API中检索一段文本.我想为它分配一定量的空间(比如一个宽度为300.0且高度为100.0的最大容器).有时,这段文本适合此容器,字体大小为30.0.在其他时候,除非我将文本大小设置为24.0,否则它将不适合.
有没有办法根据其父容器空间动态调整文本大小?
我已经构建了一个带有ConstrainedBox的Container,它允许我定义文本空间的最大大小.我还用LayoutBuilder包装了我的Text.我希望我可以检查文本空间的高度,并在此基础上确定如何调整文本大小.像这样:
Container(
child: ConstrainedBox(
constraints: BoxConstraints(
minWidth: 300.0,
maxWidth: 300.0,
minHeight: 30.0,
maxHeight: 100.0,
),
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
if (/* height is larger than 100.0? height is over the constraints? */) { return textWithSize24(); }
return textWithSize30();
}),
),
),
Run Code Online (Sandbox Code Playgroud)
如何确定"文本大小为30.0时文本占用的高度"?也许我正在接近这个错误的方式而我应该maxLines用来确定这个呢?但是我们怎么知道我们达到了更多maxLines?
另一种方法是使用我的String中的字符数来确定何时更改字体大小.这似乎是一种手册.
我有一个Flutter Container小部件,并为其定义了一种颜色(粉红色),但是由于某种原因,BoxDecoration中的颜色将其覆盖(绿色)。为什么?
new Container(
color: Colors.pink,
decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(16.0),
color: Colors.green,
),
);
Run Code Online (Sandbox Code Playgroud) 我正在尝试在PhysicalModel中创建一个圆形边框.它可以工作,如果我不将我的小部件包装在PhysicalModel中,但一旦我这样做,我得到一个方形边框.
new PhysicalModel(
color: Colors.green,
child: new Container(
width: 50.0,
height: 50.0,
decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(25.0),
border: new Border.all(
width: 5.0,
color: Colors.red,
),
),
),
),
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的TextField:
new Flexible(
fit: FlexFit.loose,
child: new Container(
alignment: FractionalOffset.topLeft,
child: new TextField(
decoration: new InputDecoration(
hintText: 'Add a note',
),
maxLines: 1,
onChanged: (value) => _showSaveOptions(value),
),
),
),
Run Code Online (Sandbox Code Playgroud)
我想让我的TextField从maxLines设置为1开始,但是当我输入时,我希望maxLines增加,以便所有文本保留在页面上,我不必向上滚动.但是,我也不想从maxLines设置为5或10开始,因为空TextField占用了更多的空间.我想要实现的行为的一个示例是在Google表单中的长答案文本选项中,它以一行开头:
并在我输入时展开:

我已经考虑过设置maxLines一个计数器,每当用户点击一个换行符时它会递增,但是如何确定用户何时填满了一行?
我有一个小部件,我理想地想要采用基础材质颜色并输出以该颜色的阴影为主题的小部件.例如:
return new Container(
color: Colors.pink.shade50,
child: new Text(
'hello',
style: new TextStyle(
color: Colors.pink.shade100,
),
),
);
Run Code Online (Sandbox Code Playgroud)
要求我指定两种粉红色.理想情况下,我可以这样做:
Color color = getBaseColorForThisPage(); // returns something like Colors.pink, but on another page, it'll return something like Colors.purple
return new Container(
color: color.shade50,
child: new Text(
'hello',
style: new TextStyle(
color: color.shade100,
),
),
);
Run Code Online (Sandbox Code Playgroud)
有没有办法在调色板中返回材质颜色的"贴图",而不是只有一种颜色?当我查看IntelliJ中的自动完成时,我看到在输入后Colors.pink,我能够指定阴影.但是,如果我将颜色设置为变量,例如Color color = Colors.pink,我后来无法做到color.shade100或color[100].谢谢!