类代码
app_builder.dart
// 老中医2023-02-23
import 'package:flutter/material.dart';
class AppBuilder extends StatefulWidget {
final Function(BuildContext context,bool reload) builder;
const AppBuilder({Key? key,required this.builder}) : super(key: key);
@override
AppBuilderState createState() => new AppBuilderState();
static AppBuilderState of(BuildContext context) {
return context.findAncestorStateOfType<AppBuilderState>()!;
}
}
class AppBuilderState extends State<AppBuilder> {
var show = true;
var reload = false;
@override
Widget build(BuildContext context) {
return show ? widget.builder(context,reload) : SizedBox();
}
Future<void> rebuild() async {
reload = true;
setState((){
show = false;
});
await Future.delayed(const Duration(milliseconds:100));
setState((){
show = true;
});
}
}
使用方法
在你的UI树的根部如下定义
Widget build(BuildContext context) {
return AppBuilder(
// reload参数是 应用程序是否第一次绘制 第一次为false 第二次为true
builder:(context,reload){
return ...;
}
);
}
在需要重绘时调用此方法
AppBuilder.of(Get.context!).rebuild();
网友评论