flutter奇淫技巧
2020-09-02 本文已影响0人
JianLee
使用技巧笔记:
debugShowCheckedModeBanner: false//去掉debug标志
首页实现:PageView 搭配BottomNavigationBar(有icon),TabBarView(优先PageView)搭配row嵌套(TabBar)。
WillPopScope组件:实现首页双击退出
ListView.separated可以生成列表项之间的分割器,它比ListView.builder多了一个separatorBuilder。
Hero 指的是可以在路由(页面)之间“飞行”的 widget,打开另一个页面时产生一个过渡动画。
ClipRRect为图片添加圆角。
ClipOval可以用来裁剪圆形或者椭圆,ClipOval的定义跟ClipRect一样,只不过是ClipRect裁剪出来的是矩形。
share: ^0.6.4+3 分享
保持页面状态:AutomaticKeepAliveClientMixin
@override
bool get wantKeepAlive => true;
super.build(context);
Isolate compute 开启线程执行耗时任务,解决ui卡顿。
SafeArea适配刘海屏
Offstage控制是否显示 false:显示,Opacity:控制透明的来控制组件是否隐藏。(0.0 到 1.0,0.0表示完全透明,1.0表示完全不透明)
ConstrainedBox限制子元素的最大最小宽高
brightness: Brightness.light 导航栏字体
AutomaticKeepAliveClientMixin 设置页面缓存(tabbar和pageview联动)
SingleTickerProviderStateMixin 初始化TabController
CustomScrollView结合Sliver:SliverToBoxAdapter可以防止滚动冲突
使用CustomScrollView组件作为滚动容器,SliverList和SliverGrid分别替代List和Grid作为CustomScrollView的子组件防止滚动冲突。
ClipRRect 跟PhysicalModel 的区别在于不能设置z轴和,阴影,其他效果跟PhysicalModel 基本一致
// 避免软键盘导致resize
resizeToAvoidBottomInset: false,
键盘溢出使用 SingleChildScrolleView
List嵌套解决
shrinkWrap: true, //为true可以解决子控件必须设置高度的问题
physics:NeverScrollableScrollPhysics(),//禁用滑动事件
条目列表使用CupertinoPicker(性别、年龄、身高、体重...)
FadeInImage图片加载动画效果
OverRepaintBoundary 截图功能
SingleChildScrollView 滚动视图
Function 传递函数
ValueChanged<String> 传递有返回参数的函数
pushAndRemoveUntil()和pushNamedAndRemoveUntil()
回滚到指定页面(可以是新页面)
Navigator.of(context)
.pushNamedAndRemoveUntil("/home", ModalRoute.withName("/home"));
pushReplacement 类似popAndPushNamed(先退出当前再进入新页面)
打开指定页面替换当前页
Navigator.of(context).pushReplacementNamed('/screen4');
//Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context)=>Screen3()));
popUntil
一直退到指定页面
//当前在Screen4页面,点击回到Screen1,连带着Screen2,Screen3也一起退出
Navigator.of(context).popUntil(ModalRoute.withName('/screen1'));
遇到坑( 配套使用:
Navigator.pushNamed(context, "/settings");
Navigator.of(context).popUntil(ModalRoute.withName('/settings'));
)
SliverToBoxAdapter添加SliverAppBar内部布局
StreamBuilder实现局部刷新
ColorFiltered(
colorFilter: ColorFilter.mode(Colors.white, BlendMode.color),
child: App()) 页面灰度
SwitchListTile 开关按钮
AnimatedCrossFade 组件切换时加入渐入效果
StreamBuilder 异步更新ui,数据流变化时,刷新小控件
wakelock: ^0.1.4+2 屏幕常亮插件
// 如果不在当前页面就不刷新
if (!mounted) {
return;
}else{
setState((){
})
}
ValueChanged<String> 参数回调
VoidCallback 触发回调
Container点击事件无效,设置GestureDetector,behavior
HitTestBehavior.opaque/HitTestBehavior.translucent
图片背景文字居中
Container(
height: 33,
padding: EdgeInsets.only(left: 5, right: 5),
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage(DImages.formatPathPng('img_biaoqian'))),
),
child: Container(
child: Center(
child: Text(
'24',
style: TextStyle(
color: DColor.white,
fontSize: 22,
fontFamily: "DIN",
fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
),
)
ListView 去掉自带空白
MediaQuery.removePadding(context: context,removeTop: true, child: _buildListView('aaa'))
沉浸式
extendBodyBehindAppBar: true,
flutter 新建插件plugins flutter create --org com.example --template=plugin --platforms=android,ios -a kotlin pay_sdk
选项弹窗 DropdownMenuItem
点击页面关闭软键盘
behavior: HitTestBehavior.translucent,
onTap: (){
FocusScope.of(context).requestFocus(FocusNode());
},
弹出键盘布局溢出
resizeToAvoidBottomPadding: false, //输入框抵住键盘
Container 单向边框
decoration: BoxDecoration(
color: DColor.color_ff12121d,
border: Border(
top: BorderSide(
color: DColor.color_ff2f3045, width: 1)),
),
监听页面是否可见
class _DefaultFijkPanelState extends State<_DefaultFijkPanel> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this); //监听页面可见与不可见状态
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
//AppLifecycleState当前页面的状态(是否可见)
if (state == AppLifecycleState.paused) {
//页面不可见时,暂停视频
if (_playing == true) {
player.pause();
}
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this); //移除监听页面可见与不可见状态
super.dispose();
文本行间距
strutStyle:
const StrutStyle(
forceStrutHeight: true,
height: 1.2,
),