我有一个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) 我有一个从零高度开始的容器,需要在用户交互后扩展。
0,null但在这两种情况下,Flutter 抱怨它无法从 插入0到null。maxHeight = double.infinity)而不是显式高度,在这种情况下,Flutter 抱怨它无法从有限值插值到不定值。vsync抱怨null.如何以动画方式扩展小部件,使其动态增长到足以包裹其内容?如果这不能动态完成,那么有什么安全的方法来调整内容的大小,以便它们在不同的屏幕尺寸上都有意义?在 Web 开发中,我知道类似的事情em是相对大小的,但在 Flutter 的上下文中,我不知道如何可靠地控制事物的大小。
更新:正如 @pskink 所建议的,将子项包装在 Align 小部件中并为 Align 的 heightFactor 参数设置动画可实现折叠。然而,当崩溃的孩子本身有孩子时,我仍然很难让崩溃工作。例如,Column 小部件根本不会使用 ClipRect 进行剪辑(请参阅https://github.com/flutter/flutter/issues/29357),即使我使用 Wrap 而不是 Column,如果 Wrap 的孩子们是行。不知道如何让剪辑始终如一地工作。
Widget build(context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 300,
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: color ?? Colors.blue,
borderRadius: BorderRadius.circular(10)
),
child: msg
)
],
);
}
Run Code Online (Sandbox Code Playgroud)
这是我的小部件的构建方法,它根据我作为msg参数传递的内容来呈现此UI
现在我面临的问题是我无法在不设置宽度的情况下将文本包装在此容器/蓝色框中,但是如果我设置了容器/蓝色框的宽度,那么无论文本有多短,它将始终保持该宽度。
现在有一种方法可以设置容器/蓝盒的maxWidth(比如说500)?这样蓝框将变得像“ Loading”之类的小文本一样宽,而长文本则可以扩展到达到500个单位的宽度,然后包装文本?
长文本的必需输出:
user-interface word-wrap flutter flutter-layout flutter-container
我希望我的容器根据其内容(在本例中为文本)具有不同的大小,如果一个用户写入较长的文本,则容器会更大,如果另一个用户写入较短的文本,则容器会较小。
是否可以?如果没有,还有其他方法吗?
谢谢
我有一个包含一些文本的容器,当文本处于正常水平位置时,它被分成两行,因为它不适合一行,我理解:
Container(
width: 30,
height: 250,
color: Color.fromRGBO(254, 242, 0, 1),
child: Center(
child: Text(
"dB per H",
style: TextStyle(
fontSize: 12,
color: Colors.black,
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
这将呈现为:
现在我正在旋转文本,以便它在垂直方向上呈现,其中容器有足够的空间。然而,它仍然被分成两行,而预期现在它可以毫无问题地安装。
如何解决这个问题?
Container(
width: 30,
height: 250,
color: Color.fromRGBO(254, 242, 0, 1),
child: Center(
child: Transform.rotate(
angle: -pi / 2,
child: Text(
"dB per H",
style: TextStyle(
fontSize: 12,
color: Colors.black,
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
这将呈现为:
我想将容器内的图标大小调整为该容器的大小,以便由于对大小值进行硬编码,它在较大的设备中不会变小。我正在尝试这样的事情
Container(
child: Icon(
Icons.beach_access,
size: double.infinity,
)
)
Run Code Online (Sandbox Code Playgroud) 我想在容器周围绘制边框,并为背景着色。
Widget bodyWidget() {
return Container(
color: Colors.yellow,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: Text("Flutter"),
);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这个时我得到了错误
无法同时提供颜色和装饰
color参数只是“ decoration:new BoxDecoration(color:color)”的简写。
如何解决?
我正在创建一个搜索小部件,我希望它有一点高度,我可以通过使用 Material 小部件来做到这一点,但是 Material 还具有其他属性,例如颜色,并且当我用 Material 小部件包裹容器时,它会创建奇怪的边缘。
Widget search(BuildContext context) {
var theme = Provider.of<ThemeNotifier>(context);
return Container(
margin: EdgeInsets.only(top: 28, left: 10, right: 10),
child: Material(
elevation: 10,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: theme.getTheme().materialTheme.buttonColor,
),
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
InkWell(onTap: () => print("Menu tapped"), child: Icon(Icons.menu)),
Expanded(
child: TextField(
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 15, right: 15),
hintText: "Search City",
filled: false,
border: InputBorder.none),
),
),
Icon(Icons.search),
],
),
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
这是我的小部件的样子:
我想为自定义添加连锁反应Container。我已经使用Inkwell和Material小部件来实现此行为,但它不起作用。代码如下:
@override
Widget build(BuildContext context) {
return Center(
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: (){},
child: Container(
width: 150,
height: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.red, width: 2),
borderRadius: BorderRadius.circular(18),
color: Colors.deepPurpleAccent,
),
alignment: Alignment.center,
child: Text(
"A box container",
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
结果是:
我还使用了透明颜色Container和紫色,Material如下所示:
@override
Widget build(BuildContext context) {
return Center(
child: Material(
color: Colors.deepPurpleAccent,
child: InkWell(
onTap: () {},
child: Container( …Run Code Online (Sandbox Code Playgroud) 在抽屉中,在列表视图中想要在单击 onTap 时更改 CustomListTile 的颜色并将所有其他子项的颜色设置为默认值?
class CustomListTile extends StatelessWidget {
final Color itemContainerColor;
const CustomListTile({
//deafult color is Colors.white
this.itemContainerColor= Colors.white,
});
@override
Widget build(BuildContext context) {
return InkWell(
onTap: (){},
child: Container(
margin: EdgeInsets.symmetric(vertical: 4),
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 16),
width: 150,
decoration: BoxDecoration(
color: itemContainerColor,
)
child:
Text(
"ListTile 1",
style: TextStyle(
fontSize: 20,
color: Colors.green,
fontWeight: FontWeight.w600),
),
),
));
}
}
Run Code Online (Sandbox Code Playgroud) flutter flutter-layout flutter-animation flutter-container flutter-listview
我是颤振的新手。现在正在学习如何定位或对齐小部件。我的行小部件中有两个容器。我想在左上角设置我的第一个容器(红色容器),还想在右上角设置我的第二个容器(蓝色容器)。我怎样才能做到这一点?
这是代码示例:
class MyRow extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Row(
children: <Widget>[
Container(
width: 100.0,
height: 100.0,
color: Colors.red,
),
Container(
width: 100.0,
height: 100.0,
color: Colors.blue,
),
],
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud) 我试图将图标置于圆形背景的中心,但即使我使用中心小部件作为子项,除非增加容器大小,它也会失败。
Container(
height: 22,
width: 22,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xffF7825C),
),
child: Center(
child: Icon(
Icons.add,
color: Colors.white,
),
),
)
Run Code Online (Sandbox Code Playgroud)