merge master peer_tab_page.dart peer_widget.dart

This commit is contained in:
csf 2022-09-21 14:56:01 +08:00
parent 9284850dff
commit 5625a061a4
2 changed files with 115 additions and 101 deletions

View File

@ -23,15 +23,31 @@ class _PeerTabPageState extends State<PeerTabPage>
@override @override
void initState() { void initState() {
() async {
await bind.mainGetLocalOption(key: 'peer-tab-index').then((value) {
if (value == '') return;
final tab = int.parse(value);
_tabIndex.value = tab;
_pageController.jumpToPage(tab);
});
await bind.mainGetLocalOption(key: 'peer-card-ui-type').then((value) {
if (value == '') return;
final tab = int.parse(value);
peerCardUiType.value =
tab == PeerUiType.list.index ? PeerUiType.list : PeerUiType.grid;
});
}();
super.initState(); super.initState();
} }
// hard code for now // hard code for now
void _handleTabSelection(int index) { Future<void> _handleTabSelection(int index) async {
// reset search text // reset search text
peerSearchText.value = ""; peerSearchText.value = "";
peerSearchTextController.clear(); peerSearchTextController.clear();
_tabIndex.value = index; _tabIndex.value = index;
await bind.mainSetLocalOption(
key: 'peer-tab-index', value: index.toString());
_pageController.jumpToPage(index); _pageController.jumpToPage(index);
switch (index) { switch (index) {
case 0: case 0:
@ -89,7 +105,7 @@ class _PeerTabPageState extends State<PeerTabPage>
shrinkWrap: true, shrinkWrap: true,
controller: ScrollController(), controller: ScrollController(),
children: super.widget.tabs.asMap().entries.map((t) { children: super.widget.tabs.asMap().entries.map((t) {
return Obx(() => GestureDetector( return Obx(() => InkWell(
child: Container( child: Container(
padding: const EdgeInsets.symmetric(horizontal: 8), padding: const EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration( decoration: BoxDecoration(
@ -111,7 +127,7 @@ class _PeerTabPageState extends State<PeerTabPage>
: MyTheme.color(context).lightText), : MyTheme.color(context).lightText),
), ),
)), )),
onTap: () => _handleTabSelection(t.key), onTap: () async => await _handleTabSelection(t.key),
)); ));
}).toList()); }).toList());
} }
@ -120,7 +136,9 @@ class _PeerTabPageState extends State<PeerTabPage>
final verticalMargin = isDesktop ? 12.0 : 6.0; final verticalMargin = isDesktop ? 12.0 : 6.0;
return Expanded( return Expanded(
child: PageView( child: PageView(
physics: const BouncingScrollPhysics(), physics: isDesktop
? NeverScrollableScrollPhysics()
: BouncingScrollPhysics(),
controller: _pageController, controller: _pageController,
children: super.widget.children, children: super.widget.children,
onPageChanged: (to) => _tabIndex.value = to) onPageChanged: (to) => _tabIndex.value = to)
@ -130,44 +148,30 @@ class _PeerTabPageState extends State<PeerTabPage>
Widget _createPeerViewTypeSwitch(BuildContext context) { Widget _createPeerViewTypeSwitch(BuildContext context) {
final activeDeco = BoxDecoration(color: MyTheme.color(context).bg); final activeDeco = BoxDecoration(color: MyTheme.color(context).bg);
return Row( return Row(
children: [ children: [PeerUiType.grid, PeerUiType.list]
Obx( .map((type) => Obx(
() => Container( () => Container(
padding: const EdgeInsets.all(4.0), padding: EdgeInsets.all(4.0),
decoration: decoration: peerCardUiType.value == type ? activeDeco : null,
peerCardUiType.value == PeerUiType.grid ? activeDeco : null, child: InkWell(
child: InkWell( onTap: () async {
onTap: () { await bind.mainSetLocalOption(
peerCardUiType.value = PeerUiType.grid; key: 'peer-card-ui-type',
}, value: type.index.toString());
child: Icon( peerCardUiType.value = type;
Icons.grid_view_rounded, },
size: 18, child: Icon(
color: peerCardUiType.value == PeerUiType.grid type == PeerUiType.grid
? MyTheme.color(context).text ? Icons.grid_view_rounded
: MyTheme.color(context).lightText, : Icons.list,
)), size: 18,
), color: peerCardUiType.value == type
), ? MyTheme.color(context).text
Obx( : MyTheme.color(context).lightText,
() => Container( )),
padding: const EdgeInsets.all(4.0), ),
decoration: ))
peerCardUiType.value == PeerUiType.list ? activeDeco : null, .toList(),
child: InkWell(
onTap: () {
peerCardUiType.value = PeerUiType.list;
},
child: Icon(
Icons.list,
size: 18,
color: peerCardUiType.value == PeerUiType.list
? MyTheme.color(context).text
: MyTheme.color(context).lightText,
)),
),
),
],
); );
} }
} }

View File

@ -92,68 +92,78 @@ class _PeerWidgetState extends State<_PeerWidget> with WindowListener {
return ChangeNotifierProvider<Peers>( return ChangeNotifierProvider<Peers>(
create: (context) => widget.peers, create: (context) => widget.peers,
child: Consumer<Peers>( child: Consumer<Peers>(
builder: (context, peers, child) => peers.peers.isEmpty builder: (context, peers, child) => peers.peers.isEmpty
? Center( ? Center(
child: Text(translate("Empty")), child: Text(translate("Empty")),
) )
: DesktopScrollWrapper( : _buildPeersView(peers)),
scrollController: _scrollController,
child: SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
controller: _scrollController,
child: ObxValue<RxString>((searchText) {
return FutureBuilder<List<Peer>>(
builder: (context, snapshot) {
if (snapshot.hasData) {
final peers = snapshot.data!;
final cards = <Widget>[];
for (final peer in peers) {
cards.add(Offstage(
key: ValueKey("off${peer.id}"),
offstage: widget.offstageFunc(peer),
child: Obx(
() => SizedBox(
width: 220,
height:
peerCardUiType.value == PeerUiType.grid
? 140
: 42,
child: VisibilityDetector(
key: ValueKey(peer.id),
onVisibilityChanged: (info) {
final peerId =
(info.key as ValueKey).value;
if (info.visibleFraction > 0.00001) {
_curPeers.add(peerId);
} else {
_curPeers.remove(peerId);
}
_lastChangeTime = DateTime.now();
},
child: widget.peerCardWidgetFunc(peer),
),
),
)));
}
return Wrap(
spacing: space,
runSpacing: space,
children: cards);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
future: matchPeers(searchText.value, peers.peers),
);
}, peerSearchText),
),
),
),
); );
} }
Widget _buildPeersView(Peers peers) {
final body = ObxValue<RxString>((searchText) {
return FutureBuilder<List<Peer>>(
builder: (context, snapshot) {
if (snapshot.hasData) {
final peers = snapshot.data!;
final cards = <Widget>[];
for (final peer in peers) {
final visibilityChild = VisibilityDetector(
key: ValueKey(peer.id),
onVisibilityChanged: (info) {
final peerId = (info.key as ValueKey).value;
if (info.visibleFraction > 0.00001) {
_curPeers.add(peerId);
} else {
_curPeers.remove(peerId);
}
_lastChangeTime = DateTime.now();
},
child: widget.peerCardWidgetFunc(peer),
);
cards.add(Offstage(
key: ValueKey("off${peer.id}"),
offstage: widget.offstageFunc(peer),
child: isDesktop
? Obx(
() => SizedBox(
width: 220,
height: peerCardUiType.value == PeerUiType.grid
? 140
: 42,
child: visibilityChild,
),
)
: SizedBox(width: mobileWidth, child: visibilityChild)));
}
return Wrap(spacing: space, runSpacing: space, children: cards);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
future: matchPeers(searchText.value, peers.peers),
);
}, peerSearchText);
if (isDesktop) {
return DesktopScrollWrapper(
scrollController: _scrollController,
child: SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
controller: _scrollController,
child: body),
);
} else {
return SingleChildScrollView(
physics: BouncingScrollPhysics(),
controller: _scrollController,
child: body,
);
}
}
// ignore: todo // ignore: todo
// TODO: variables walk through async tasks? // TODO: variables walk through async tasks?
void _startCheckOnlines() { void _startCheckOnlines() {