如何替换文本()小部件由一个空容器()小部件在颤动编程?



我正在寻找Android可见性。等效选项。

以编程方式显示/隐藏Flutter中的小部件

将不透明度设置为0后,上述链接批准的解决方案将占用空间。不像Android那样占用任何空间,解决方案是

为不占用空间,将其替换为空的Container()

谁来告诉我如何做到这一点。我需要使一个小部件完全擦除而不占用任何空间编程。

注意:我需要类似的执行解决方案,如Android本地代码view.setVisibility(View.GONE)

Try Offstage:

Offstage(offstage: isHide, child:Text('test'),);

statefullwidget中使用条件显示:请看下面的例子:

class _MyHomePageState extends State<MyHomePage> {
bool _isShowing = true;
void _toggle() {
setState(() {
_isShowing =!_isShowing ;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Visibility Page"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(color: Colors.red, height: 50),
if(_isShowing )
Container(color: Colors.green, height: 50),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _toggle,
tooltip: 'Toggle',
child: Icon(Icons.add),
),
);
}
}

有状态小部件是实现此目的的方法之一。

因此,一旦按下按钮,它就会触发_toggle()函数。该函数将布尔值转换为false。

SetState(){}函数调用重建。

当isshow为false时,它将显示容器…

在下面的示例:

import 'package:flutter/material.dart';
class HomePageScreen extends StatefulWidget {
@override
_HomePageScreenState createState() => _HomePageScreenState();
}
class _HomePageScreenState extends State<HomePageScreen> {
bool _isShowing = true;
void _toggle() {
setState(() {
_isShowing = !_isShowing;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Remove Text for Empty Container"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_isShowing
? Text("Remove Text")
: Container(),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _toggle,
tooltip: 'Toggle',
child: Icon(Icons.add),
),
);
}
}

最新更新