Skip to content

Commit

Permalink
refact: flutter, ChangeNotifier, reduce rebuild (rustdesk#9392)
Browse files Browse the repository at this point in the history
Signed-off-by: fufesou <[email protected]>
  • Loading branch information
fufesou authored Sep 19, 2024
1 parent 88a9921 commit c6e3f60
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions flutter/lib/common/widgets/peer_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ class _PeerCardState extends State<_PeerCard>
color: Colors.transparent,
elevation: 0,
margin: EdgeInsets.zero,
// to-do: memory leak here, more investigation needed.
// Continious rebuilds of `Obx()` will cause memory leak here.
// The simple demo does not have this issue.
child: Obx(
() => Container(
foregroundDecoration: deco.value,
Expand Down
10 changes: 4 additions & 6 deletions flutter/lib/common/widgets/peer_tab_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class _PeerTabPageState extends State<PeerTabPage>
Widget build(BuildContext context) {
final model = Provider.of<PeerTabModel>(context);
Widget selectionWrap(Widget widget) {
return model.multiSelectionMode ? createMultiSelectionBar() : widget;
return model.multiSelectionMode ? createMultiSelectionBar(model) : widget;
}

return Column(
Expand Down Expand Up @@ -362,8 +362,7 @@ class _PeerTabPageState extends State<PeerTabPage>
.toList());
}

Widget createMultiSelectionBar() {
final model = Provider.of<PeerTabModel>(context);
Widget createMultiSelectionBar(PeerTabModel model) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expand All @@ -381,7 +380,7 @@ class _PeerTabPageState extends State<PeerTabPage>
Row(
children: [
selectionCount(model.selectedPeers.length),
selectAll(),
selectAll(model),
closeSelection(),
],
)
Expand Down Expand Up @@ -512,8 +511,7 @@ class _PeerTabPageState extends State<PeerTabPage>
);
}

Widget selectAll() {
final model = Provider.of<PeerTabModel>(context);
Widget selectAll(PeerTabModel model) {
return Offstage(
offstage:
model.selectedPeers.length >= model.currentTabCachedPeers.length,
Expand Down
6 changes: 6 additions & 0 deletions flutter/lib/common/widgets/peers_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ class _PeersViewState extends State<_PeersView>

@override
Widget build(BuildContext context) {
// We should avoid too many rebuilds. MacOS(m1, 14.6.1) on Flutter 3.19.6.
// Continious rebuilds of `ChangeNotifierProvider` will cause memory leak.
// Simple demo can reproduce this issue.
return ChangeNotifierProvider<Peers>(
create: (context) => widget.peers,
child: Consumer<Peers>(builder: (context, peers, child) {
Expand Down Expand Up @@ -245,6 +248,9 @@ class _PeersViewState extends State<_PeersView>
: Container(child: visibilityChild);
}

// We should avoid too many rebuilds. Win10(Some machines) on Flutter 3.19.6.
// Continious rebuilds of `ListView.builder` will cause memory leak.
// Simple demo can reproduce this issue.
final Widget child = Obx(() => stateGlobal.isPortrait.isTrue
? ListView.builder(
itemCount: peers.length,
Expand Down
9 changes: 8 additions & 1 deletion flutter/lib/models/peer_tab_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,17 @@ class PeerTabModel with ChangeNotifier {
notifyListeners();
}

// `notifyListeners()` will cause many rebuilds.
// So, we need to reduce the calls to "notifyListeners()" only when necessary.
// A better way is to use a new model.
setCurrentTabCachedPeers(List<Peer> peers) {
Future.delayed(Duration.zero, () {
final isPreEmpty = _currentTabCachedPeers.isEmpty;
_currentTabCachedPeers = peers;
notifyListeners();
final isNowEmpty = _currentTabCachedPeers.isEmpty;
if (isPreEmpty != isNowEmpty) {
notifyListeners();
}
});
}

Expand Down

0 comments on commit c6e3f60

Please sign in to comment.