flutter - How to use Visibility to hide some elements in Flutter
问题描述
I am trying to use Visibility
in Flutter to hide some elements from my app. What I'm trying to do now:
I have multiple options in floatingActionButton: SpeedDial
. I also have a condition that I want to check. If the condition is met, some options are hidden in floatingActionButton: SpeedDial
.
The condition is as follows:
bool visibilityController = false;
@override
void initState() {
super.initState();
setState(() {
if(_myPreferences.id ==(UserPostID)){
visibilityController = true;
}
});
}
So if it is true user can see all options and if false some options will be hidden. But now I try to make SpeedDialChild
like this:
child: Visibility(
visible: visibilityController,
SpeedDialChild(
child: Icon(Icons.message),
backgroundColor: const Color(0xFFf6c626),
label: 'Comments',
onTap: () { }
),
)
When trying to do the packaging like that, I get an error. So how can I hide some options from floatingActionButton: SpeedDial
?
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'fg.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool visibilityController = false;
@override
void initState() {
super.initState();
setState(() {
if(_myPreferences.id ==(UserPostID)){
visibilityController = false;
}
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton: SpeedDial(
animatedIcon: AnimatedIcons.menu_close,
animatedIconTheme: IconThemeData(size: 22.0),
curve: Curves.bounceIn,
overlayColor: Colors.black,
overlayOpacity: 0.5,
onOpen: () => print('OPENING DIAL'),
onClose: () => print('DIAL CLOSED'),
tooltip: 'Speed Dial',
heroTag: 'speed-dial-hero-tag',
backgroundColor: const Color(0xFFf6c626),
foregroundColor: Colors.black,
elevation: 8.0,
shape: CircleBorder(),
children: [
SpeedDialChild(
child: Icon(Icons.message),
backgroundColor: const Color(0xFFf6c626),
label: 'Comments',
onTap: () { }
),
SpeedDialChild(
child: Icon(Icons.account_circle),
backgroundColor: const Color(0xFFf6c626),
label: 'About user',
onTap: () { }
),
],
),
);
}
}
解决方案
推荐阅读
- unity3d - UNITY : 无法取消订阅事件处理程序
- java - 使用 Firebase 属性从 Recyclerview 过滤元素
- c# - Unity 在编辑器中工作正常,但脚本没有在构建中开始
- java - 为什么我使用 Paper 库制作的“记住我”复选框无法正常工作?
- javascript - 如何在 discord.js 中标记播放器并接收他们的不和谐 ID
- python - 我应该如何处理包含大约 300000 个小数据表组的数据集?
- ssl - TLS 握手失败
- go - 列出模块的依赖项,而不是包
- android - 我的列表大小是整个屏幕,看不到下面的按钮或滚动
- python - 时间表不会显示