flutter
Visibility控件,代替if,else选择语句
const Visibility({
Key? key,
required this.child,//子控件
this.replacement = const SizedBox.shrink(),//不可见时显示的组件,默认为sizedbox.shrink,
const SizedBox.shrink({ Key? key, Widget? child })
: width = 0.0,
height = 0.0,
super(key: key, child: child);
this.visible = true,//可见属性,true可见,false不可见
this.maintainState = false,//不可见时的属性,true维持状态,false不维持
this.maintainAnimation = false,//同上,不过它指动画可见与否
this.maintainSize = false,
//不可见时是否保留空间,但是设置true会报错,报错信息为:
Cannot maintain size if animations are not maintained.
'package:flutter/src/widgets/visibility.dart':
Failed assertion: line 76 pos 10: 'maintainAnimation == true || maintainSize == false'
this.maintainSemantics = false,//不可见时是否维持语义
this.maintainInteractivity = false,//不可见时是否具有交互
})
2、底部弹出框
flutter中文书里面写了一种方法,showModalBottomSheet
弹出一个Material风格的底部菜单列表对话框,示例如下:
// 弹出底部菜单列表模态对话框
Future<int> _showModalBottomSheet() {
return showModalBottomSheet<int>(
context: context,
builder: (BuildContext context) {
return ListView.builder(
itemCount: 30,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text("$index"),
onTap: () => Navigator.of(context).pop(index),
);
},
);
},
);
}
但是在我使用该控件的时候遇到了一些困难。下面给出我遇到困难的解决方法。
1、该弹出框的高度始终是app高度的一半,无法修改。
原因是因为在该控件的源码里面,直接限制了控件的最大高度,无法修改而导致
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
return BoxConstraints(
minWidth: constraints.maxWidth,
maxWidth: constraints.maxWidth,
minHeight: 0.0,
maxHeight: isScrollControlled
? constraints.maxHeight
: constraints.maxHeight * 9.0 / 16.0,
);
}
解决方法:
1.修改源码(不建议)
2.因为maxHeight是个三元选择语句,所以我们可以直接修改isScrollControlled属性为true,此时maxHeight的值就是屏幕的最大高度了,然后我们可以使用GestureDetector再套层container设置高度即可。
2、在该界面里面的setState()方法无效。
解决方法:
使用StatefulBuilder()控件把要写的层次结构全部包裹集中,具体使用是
return StatefulBuilder(builder: (context, state) {
你要写的内容
state((){//用自己指定名字的state代替setState
想要刷新的内容
})
}