如何以编程方式在 Flutter 中用 Empty Container() 小部件替换 Text() 小部件?
How to replace a Text() widget by an Empty Container() widget in Flutter programatically?
我正在寻找 Android Visibility.GONE 等效选项。
以上 link 批准的解决方案将在将不透明度设置为 0 后采用 space。对于不采用 Android 中的任何 space 该解决方案说
为了不占用space,将其替换为空的Container()
。
有人请告诉我如何实现这一目标。我需要完全擦除一个 Widget,而无需以编程方式获取任何 space。
注意:我需要像 Android 本机代码 view.setVisibility(View.GONE)
一样执行类似的解决方案
试试后台:
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(){} 函数调用重建。
当 isShowing 为 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),
),
);
}
}
我正在寻找 Android Visibility.GONE 等效选项。
以上 link 批准的解决方案将在将不透明度设置为 0 后采用 space。对于不采用 Android 中的任何 space 该解决方案说
为了不占用space,将其替换为空的Container()
。
有人请告诉我如何实现这一目标。我需要完全擦除一个 Widget,而无需以编程方式获取任何 space。
注意:我需要像 Android 本机代码 view.setVisibility(View.GONE)
试试后台:
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(){} 函数调用重建。
当 isShowing 为 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),
),
);
}
}