[英]Why the Visibility doesn't work in flutter?
我想通過單擊 AppBar 的IconButton
來隱藏我的 Image 容器。 但是我的 AppBar 的IconButton
不起作用。 我想,我在我的代碼中犯了一個錯誤......
import 'package:flutter/material.dart';
import 'package:icon_shadow/icon_shadow.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
bool viewVisible = true;
/* void showWidget() {
setState(() {
viewVisible = true;
});
print("Pressed");
}
*/
void hideWidget() {
setState(() {
viewVisible = false;
print("Work");
});
}
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text("Icon & Image Shadow Demo"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.arrow_drop_down_circle),
onPressed: hideWidget,
)
],
),
body: Stack(
children: <Widget>[
ListView(
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconShadowWidget(
Icon(
Icons.add_circle,
color: Colors.red,
size: 100.0,
),
),
IconShadowWidget(
Icon(
Icons.add_circle,
color: Colors.red,
size: 100.0,
),
shadowColor: Colors.black,
),
IconShadowWidget(
Icon(
Icons.add_circle,
color: Colors.red,
size: 100.0,
),
shadowColor: Colors.black,
showShadow: false,
),
],
),
SizedBox(
height: 5.0,
),
Visibility(
maintainAnimation: true,
maintainSize: true,
maintainState: true,
visible: viewVisible,
child: Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.8),
spreadRadius: 5,
blurRadius: 3,
offset:
Offset(5, 7), // changes position of shadow
),
],
),
child: Image.asset("assets/images/download.png"),
),
)
],
),
],
)
],
)),
);
}
}
移動bool viewVisible = true;
在構建方法之外,最好也移動 hideWidget,否則每次構建都會一次又一次地聲明 function
class _HomePageState extends State<HomePage> {
bool viewVisible = true;
void hideWidget() {
setState(() {
viewVisible = false;
print("Work");
});
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text("Icon & Image Shadow Demo"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.arrow_drop_down_circle),
onPressed: hideWidget,
)
],
),
body: Stack(
children: <Widget>[
ListView(
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconShadowWidget(
Icon(
Icons.add_circle,
color: Colors.red,
size: 100.0,
),
),
IconShadowWidget(
Icon(
Icons.add_circle,
color: Colors.red,
size: 100.0,
),
shadowColor: Colors.black,
),
IconShadowWidget(
Icon(
Icons.add_circle,
color: Colors.red,
size: 100.0,
),
shadowColor: Colors.black,
showShadow: false,
),
],
),
SizedBox(
height: 5.0,
),
Visibility(
maintainAnimation: true,
maintainSize: true,
maintainState: true,
visible: viewVisible,
child: Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.8),
spreadRadius: 5,
blurRadius: 3,
offset:
Offset(5, 7), // changes position of shadow
),
],
),
child: Image.asset("assets/images/download.png"),
),
)
],
),
],
)
],
)),
);
}
}
在頂部定義 viewVisible:
class _HomePageState extends State<HomePage> {
bool viewVisible = true;
@override
Widget build(BuildContext context) {
它沒有更新,因為您在 build 方法中初始化了 viewVisible 並且它曾經在每個 setstate 進行構建,使其成為 true 並且永遠不會變為 false。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.