Flutter 如果某些条件为真或假,则在颤振中显示行
在flatter中有一行小部件,我希望它在满足特定条件时可见,如果条件为true,则不应可见,如果条件为false,则应可见 下面是代码Flutter 如果某些条件为真或假,则在颤振中显示行,flutter,flutter-widget,Flutter,Flutter Widget,在flatter中有一行小部件,我希望它在满足特定条件时可见,如果条件为true,则不应可见,如果条件为false,则应可见 下面是代码 Container( child: Row( children: <Widget>[ // Button send image Text("Text1"), Text("Text2"), Text("
Container(
child: Row(
children: <Widget>[
// Button send image
Text("Text1"),
Text("Text2"),
Text("Text3"),
Text("Text4"),
],
),
width: double.infinity,
height: 50.0,
decoration: BoxDecoration(border: Border(top: BorderSide(color: greyColor2, width: 0.5)), color: Colors.white),
)
如果为true,则不可见,如果为false,则可见
如何实现它?容器(
Container(
child: Row(
children: <Widget>[
// Button send image
Text("Text1"),
if (a == b)
Text("Text2"),
Text("Text3"),
Text("Text4"),
],
),
width: double.infinity,
height: 50.0,
decoration: BoxDecoration(
border:
Border(top: BorderSide(color: greyColor2, width: 0.5)),
color: Colors.white),
),
孩子:排(
儿童:[
//按钮发送图像
文本(“文本1”),
如果(a==b)
文本(“文本2”),
文本(“文本3”),
文本(“文本4”),
],
),
宽度:double.infinity,
身高:50.0,
装饰:盒子装饰(
边界:
边框(顶部:边框边(颜色:灰色,宽度:0.5)),
颜色:颜色。白色),
),
您可以对父容器执行相同的操作,只需添加条件并基于添加小部件(容器/文本)。这是基于整个小部件是否可见的假设,这也适用于任何行 一种方法是将其嵌入到
可见性
小部件中
小部件的visible
属性采用boolean
值
Visibility(
visible: (a == b), // condition here
child: Container(
child: Row(
children: <Widget>[
// Button send image
Text("Text1"),
Text("Text2"),
Text("Text3"),
Text("Text4"),
],
),
width: double.infinity,
height: 50.0,
decoration: BoxDecoration(border: Border(top: BorderSide(color: greyColor2, width: 0.5)), color: Colors.white),
)
)
您可以使用
Container()
这是一个空的小部件,或者您可以简单地将Null
放在小部件中,这样就不会放置任何小部件您可以通过在dart中使用if语句
和扩展运算符
组合,将子项
(行)隐藏在行
小部件中。这几乎适用于所有具有子项
属性的小部件。代码应该是这样的-
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
"Row 1",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 2",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
//Our conditional Rows
if (_condition) ...[
Text(
"Row 3",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 4",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
]
],
),
这是输出-
希望这有帮助 您可以将其包装在可见性小部件中,并使用visible参数和setState更改其可见性
a == b ?
Container(
child: Row(
children: <Widget>[
// Button send image
Text("Text1"),
Text("Text2"),
Text("Text3"),
Text("Text4"),
],
),
width: double.infinity,
height: 50.0,
decoration: BoxDecoration(border: Border(top: BorderSide(color:
greyColor2, width: 0.5)), color: Colors.white),
) : Container(), // you can also replace this with Null
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
"Row 1",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 2",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
//Our conditional Rows
if (_condition) ...[
Text(
"Row 3",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 4",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
]
],
),
import 'package:flutter/material.dart';
void main() {
//Run the App Widget
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Demo App",
theme: ThemeData(
primarySwatch: Colors.amber,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
bool _condition = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Dynamic Row"),
),
body: Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
"Row 1",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 2",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
if (_condition) ...[
Text(
"Row 3",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
Text(
"Row 4",
style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.w600),
),
]
],
),
),
),
floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: RaisedButton(
color: Colors.red,
animationDuration: Duration(microseconds: 500),
child: Text(
"Toggle",
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.w600,
),
),
onPressed: () {
setState(() {
_condition = !_condition;
});
},
),
);
}
}