Flutter 解决Navigator跳转导致Rebuild的一
2020-07-13 本文已影响0人
mtko
Flutter开发者都知道,页面每次push和pop 时都会build当前页,滑动位置等数据因此被销毁,这或许是Flutter团队的一种调优策略,但在实际业务中反而造成了一些困扰,github关于此问题也争论了很久,相关issue,个人认为rebuild问题还是需要解决下的(或许是没有完全适应Flutter的UI模式,至少目前需要解决)
/// !解决rebuild问题 !
///
/// 相关issue:https://github.com/flutter/flutter/issues/11655?tdsourcetag=s_pcqq_aiomsg
///
/// 所有State类请继承[XCState],勿继承[State]
///
/// 所有State类请重写[XCState.shouldBuild],勿重写[State.build];
///
/// 刷新请使用[XCState.reload],勿使用[State.setState]
源码
XCShouldBuild.dart
import 'package:flutter/widgets.dart';
typedef XCShouldBuildFunction<T> = bool Function(
T oldSubstance, T newSubstance);
class XCShouldBuild<T> extends StatefulWidget {
final T substance; // substance
final XCShouldBuildFunction<T> shouldBuildFunction;
final WidgetBuilder builder;
XCShouldBuild(
{this.substance, this.shouldBuildFunction, @required this.builder})
: assert(() {
if (builder == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('error in XCShouldBuild: builder must exist')
]);
}
return true;
}());
@override
_XCShouldBuildState createState() => _XCShouldBuildState<T>();
}
class _XCShouldBuildState<T> extends State<XCShouldBuild> {
Widget oldWidget;
T oldSubstance;
bool _isInit = true;
@override
Widget build(BuildContext context) {
final newSubstance = widget.substance;
if (_isInit ||
(widget.shouldBuildFunction == null
? true
: widget.shouldBuildFunction(oldSubstance, newSubstance))) {
_isInit = false;
oldSubstance = newSubstance;
oldWidget = widget.builder(context);
}
return oldWidget;
}
@override
void dispose() {
super.dispose();
debugPrint("_XCShouldBuildState dispose");
}
}
XCState.dart
import 'package:flutter/widgets.dart';
import 'XCShouldBuild.dart';
/// ## Sumarry
/// - this is a way of resolving the issue of rebuilding stateful widget when a navigator is pushing or popping
/// - associated issue:https://github.com/flutter/flutter/issues/11655?tdsourcetag=s_pcqq_aiomsg
/// ## Usage
/// 1. import XCShouldBuild.dart and XCState.dart
/// 2. make all classes of State inherit from [XCState],do not inherit from [State]
/// 3. override [XCState.shouldBuild],do not override [State.build]
/// 4. use [XCState.reload] to reload,do not use [State.setState] to reload
abstract class XCState<T> extends State {
bool _isShouldBuild = false;
@override
Widget build(BuildContext context) {
bool willUseSubstance = useSubstance();
return XCShouldBuild<T>(
substance: willUseSubstance ? substance() : null,
shouldBuildFunction: (oldSubstance, newSubstance) {
bool willReload;
if (_isShouldBuild) {
willReload = true;
} else {
if (willUseSubstance) {
willReload = oldSubstance != newSubstance;
} else {
willReload = false;
}
}
return willReload;
},
builder: (BuildContext context) {
_isShouldBuild = false;
return shouldBuild(context);
});
}
Widget shouldBuild(BuildContext context);
bool useSubstance() => false;
T substance() => null;
void reload([VoidCallback fn]) {
if (!mounted) return;
setState(() {
_isShouldBuild = true;
if (fn != null) {
fn();
}
});
}
@override
void dispose() {
super.dispose();
debugPrint("XCState dispose");
}
}
场景1:
在Push或Pop时不rebuild(默认)
class _XCTestViewState extends XCState {
@override
Widget shouldBuild(BuildContext context) {
return Center(
child: Text(
"_XCTestView, if you use `shouldBuild`, it can avoid rebuilding when pushing or popping"));
}
void aFunc() {
reload()
}
}
场景2:
也可以重写参照物
方法,决定Push或Pop时是否rebuild
class _XCUseSubstanceTestViewState extends XCState {
var _aCountAsSubstance = 0;
@override
Widget shouldBuild(BuildContext context) {
return Center(
child: Text(
"_XCUseSubstanceTestView, if you use `shouldBuild`, it can avoid rebuilding when pushing or popping, _aCountAsSubstance = $_aCountAsSubstance "));
}
/// only effect to pushing or poping, no effect to [XCState.reload]
@override
bool useSubstance() {
return true;
}
/// only effect to pushing or poping, no effect to [XCState.reload]
@override
substance() {
return _aCountAsSubstance;
}
void aFunc() {
reload();
}
}