Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A wrapper widget to enable tapping to switch off or on an experimental mode setting
import 'package:flutter/material.dart';
import 'package:japanese_reading/settings_page/services/experimental_mode.dart';
class ExperimentalModeSwitcher extends StatefulWidget {
final Widget child;
final ExperimentalMode experimentalMode;
const ExperimentalModeSwitcher({Key key, @required this.child, @required this.experimentalMode}) : super(key: key);
@override
_ExperimentalModeSwitcherState createState() => _ExperimentalModeSwitcherState();
}
class _ExperimentalModeSwitcherState extends State<ExperimentalModeSwitcher> {
static final int tapsRequired = 5;
int _taps = 0;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _tapped,
child: widget.child
);
}
void _tapped() {
_taps += 1;
if (_taps >= tapsRequired) {
_taps = 0;
_toggleMode();
} else {
showToast("${tapsRequired - _taps} taps left", Duration(milliseconds: 100));
}
}
void _toggleMode() {
var mode = this.widget.experimentalMode.isExperimentalMode;
mode = !mode;
String message = mode ? 'Experimental mode on, please restart' : 'Experimental mode off, please restart';
showToast(message, Duration(seconds: 2));
this.widget.experimentalMode.setExperimentalMode(mode);
}
void showToast(String message, Duration duration) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
duration: duration,
margin: EdgeInsets.symmetric(vertical: 64.0, horizontal: 16.0),
behavior: SnackBarBehavior.floating,
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment