Skip to content

Commit

Permalink
fix: Close RelaySelectSheet.dart when it's too small
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed May 21, 2023
1 parent 121e8af commit 8cc4909
Showing 1 changed file with 131 additions and 103 deletions.
234 changes: 131 additions & 103 deletions lib/widgets/RelaySelectSheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class _RelaySelectSheetState extends State<RelaySelectSheet> {
final _searchController = TextEditingController();
late final DraggableScrollableController _sheetController;
String _newValue = '';
bool _isPoppingNavigation = false;

Set<String> get checkedRelaysSet => Set.from(widget.controller.relays);

Expand Down Expand Up @@ -102,7 +103,7 @@ class _RelaySelectSheetState extends State<RelaySelectSheet> {
}

final normalizedSelectedRelays =
widget.controller.relays.map(removeProtocol);
widget.controller.relays.map(removeProtocol);

if (normalizedSelectedRelays.contains(value)) {
setState(() {
Expand All @@ -126,6 +127,21 @@ class _RelaySelectSheetState extends State<RelaySelectSheet> {
});

_sheetController = DraggableScrollableController();
_sheetController.addListener(() {
if (_sheetController.size <= 0.4) {
_closeSheet();
}
});
}

_closeSheet() {
if (_isPoppingNavigation) {
return;
}

Navigator.pop(context);

_isPoppingNavigation = true;
}

@override
Expand Down Expand Up @@ -185,66 +201,70 @@ class _RelaySelectSheetState extends State<RelaySelectSheet> {
itemBuilder: (context, rawIndex) {
if (isValueNew && rawIndex == 0) {
return PlatformWidget(
material: (context, _) => ListTile(
title: Text(
l10n.addNewValueLabel(_newValue),
),
leading: const Icon(
Icons.add,
),
onTap: () {
widget.controller.add(_searchController.value.text);
_searchController.clear();
},
),
cupertino: (context, _) => CupertinoButton(
child: Text(
l10n.addNewValueLabel(_newValue),
),
onPressed: () {
widget.controller.add(_searchController.value.text);
_searchController.clear();
},
),
material: (context, _) =>
ListTile(
title: Text(
l10n.addNewValueLabel(_newValue),
),
leading: const Icon(
Icons.add,
),
onTap: () {
widget.controller.add(_searchController.value.text);
_searchController.clear();
},
),
cupertino: (context, _) =>
CupertinoButton(
child: Text(
l10n.addNewValueLabel(_newValue),
),
onPressed: () {
widget.controller.add(_searchController.value.text);
_searchController.clear();
},
),
);
}

final index = isValueNew ? rawIndex - 1 : rawIndex;
final relay = allRelays[index];

return PlatformWidget(
material: (context, _) => CheckboxListTile(
title: Text(
relay.length >= 6 ? relay.substring(6) : relay,
),
value: widget.controller.relays.contains(relay),
onChanged: (newValue) {
if (newValue == null) {
return;
}

if (newValue) {
widget.controller.add(relay);
} else {
widget.controller.remove(relay);
}
},
),
cupertino: (context, _) => CupertinoListTile(
title: Text(
relay.length >= 6 ? relay.substring(6) : relay,
),
trailing: CupertinoSwitch(
value: widget.controller.relays.contains(relay),
onChanged: (newValue) {
if (newValue) {
widget.controller.add(relay);
} else {
widget.controller.remove(relay);
}
},
),
),
material: (context, _) =>
CheckboxListTile(
title: Text(
relay.length >= 6 ? relay.substring(6) : relay,
),
value: widget.controller.relays.contains(relay),
onChanged: (newValue) {
if (newValue == null) {
return;
}

if (newValue) {
widget.controller.add(relay);
} else {
widget.controller.remove(relay);
}
},
),
cupertino: (context, _) =>
CupertinoListTile(
title: Text(
relay.length >= 6 ? relay.substring(6) : relay,
),
trailing: CupertinoSwitch(
value: widget.controller.relays.contains(relay),
onChanged: (newValue) {
if (newValue) {
widget.controller.add(relay);
} else {
widget.controller.remove(relay);
}
},
),
),
);
},
);
Expand All @@ -259,58 +279,66 @@ class _RelaySelectSheetState extends State<RelaySelectSheet> {
return DraggableScrollableSheet(
expand: false,
controller: _sheetController,
builder: (context, controller) => ModalSheet(
child: Column(
children: <Widget>[
if (loadStatus == LoadStatus.loading)
Expanded(
child: Center(
child: PlatformCircularProgressIndicator(),
builder: (context, controller) =>
ModalSheet(
child: Column(
children: <Widget>[
if (loadStatus == LoadStatus.loading)
Expanded(
child: Center(
child: PlatformCircularProgressIndicator(),
),
)
else
if (loadStatus == LoadStatus.error)
Text(
l10n.unknownError,
style: TextStyle(
color: getErrorColor(context),
),
)
else
if (availableRelays.isNotEmpty)
Expanded(
child: buildRelaySelectSheet(controller),
),
const SizedBox(height: MEDIUM_SPACE),
PlatformTextButton(
material: (_, __) =>
MaterialTextButtonData(
icon: const Icon(Icons.shuffle),
),
onPressed: loadStatus == LoadStatus.success
? () {
final relays = availableRelays.toList();
relays.shuffle();

widget.controller.clear();
widget.controller.addAll(relays.take(5).toList());
}
: null,
child: Text(l10n.relaySelectSheet_selectRandomRelays(5)),
),
)
else if (loadStatus == LoadStatus.error)
Text(
l10n.unknownError,
style: TextStyle(
color: getErrorColor(context),
const SizedBox(height: SMALL_SPACE),
PlatformElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
material: (_, __) =>
MaterialElevatedButtonData(
icon: const Icon(Icons.done),
),
child: Text(l10n.closePositiveSheetAction),
),
)
else if (availableRelays.isNotEmpty)
Expanded(
child: buildRelaySelectSheet(controller),
),
const SizedBox(height: MEDIUM_SPACE),
PlatformTextButton(
material: (_, __) => MaterialTextButtonData(
icon: const Icon(Icons.shuffle),
),
onPressed: loadStatus == LoadStatus.success
? () {
final relays = availableRelays.toList();
relays.shuffle();

widget.controller.clear();
widget.controller.addAll(relays.take(5).toList());
}
: null,
child: Text(l10n.relaySelectSheet_selectRandomRelays(5)),
),
const SizedBox(height: SMALL_SPACE),
PlatformElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
material: (_, __) => MaterialElevatedButtonData(
icon: const Icon(Icons.done),
),
child: Text(l10n.closePositiveSheetAction),
SizedBox(
height: MediaQuery
.of(context)
.viewInsets
.bottom,
)
],
),
SizedBox(
height: MediaQuery.of(context).viewInsets.bottom,
)
],
),
),
),
);
}
}

0 comments on commit 8cc4909

Please sign in to comment.