-
-
Save slightfoot/ab452f09c91aca0766e5ad560f31bb2c to your computer and use it in GitHub Desktop.
SecretPage page tap - solved by Simon Lightfoot on #HumpdayQandA - 6th November 2024 :: https://www.youtube.com/watch?v=txmWGhgPKuU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Please solve this one. | |
// | |
// If I tap on any one of the eight areas app navigate to “PageTwo()” | |
// | |
// If I tap on four of the areas simultaneous | |
// (I use four fingers to tap with), I want to navigate the “SecretPage()“. | |
// | |
// Lets pick “One”, “Two”, “Five” and “Six” as the as the secrete tap areas. | |
import 'package:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
const pageTwo = '/pagetwo'; | |
const secretPage = '/secretpage'; | |
class ButtonState { | |
ButtonState({ | |
required this.navigatorKey, | |
required this.magicButtons, | |
}); | |
final GlobalKey<NavigatorState> navigatorKey; | |
final Set<int> magicButtons; | |
final _buttonsPressed = <int>{}; | |
void buttonDown(int id) { | |
// print('down $id'); | |
_buttonsPressed.add(id); | |
// check if magic happened | |
if (setEquals(_buttonsPressed, magicButtons)) { | |
final navigator = navigatorKey.currentState!; | |
navigator.push(SecretPage.route()); | |
} | |
} | |
void buttonUp(int id) { | |
// print('up $id'); | |
final navigator = navigatorKey.currentState!; | |
navigator.push(PageTwo.route()); | |
_buttonsPressed.remove(id); | |
} | |
void buttonCancel(int id) { | |
// print('cancel $id'); | |
_buttonsPressed.remove(id); | |
} | |
} | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
const MyApp({super.key}); | |
static ButtonState buttonStateOf(BuildContext context) { | |
return context.findAncestorStateOfType<_MyAppState>()!._buttonState; | |
} | |
@override | |
State<MyApp> createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
final _navigatorKey = GlobalKey<NavigatorState>(); | |
late final _buttonState = ButtonState( | |
navigatorKey: _navigatorKey, | |
magicButtons: {1, 2, 5, 6}, | |
); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
navigatorKey: _navigatorKey, | |
title: 'Touch Four', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const MyHomePage(title: 'Touch Four'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(widget.title), | |
), | |
body: const Center( | |
child: Column( | |
children: <Widget>[ | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
TouchArea(id: 1, name: 'One'), | |
TouchArea(id: 2, name: 'Two'), | |
], | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
TouchArea(id: 3, name: 'Three'), | |
TouchArea(id: 4, name: 'Four'), | |
], | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
TouchArea(id: 5, name: 'Five'), | |
TouchArea(id: 6, name: 'Six'), | |
], | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
TouchArea(id: 7, name: 'Seven'), | |
TouchArea(id: 8, name: 'Eight'), | |
], | |
), | |
], | |
), | |
), | |
// This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
} | |
class TouchArea extends StatefulWidget { | |
const TouchArea({ | |
required this.id, | |
required this.name, | |
super.key, | |
}); | |
final int id; | |
final String name; | |
@override | |
State<TouchArea> createState() => _TouchAreaState(); | |
} | |
class _TouchAreaState extends State<TouchArea> { | |
@override | |
Widget build(BuildContext context) { | |
return InkWell( | |
onTapDown: (_) { | |
MyApp.buttonStateOf(context).buttonDown(widget.id); | |
}, | |
onTapUp: (_) { | |
MyApp.buttonStateOf(context).buttonUp(widget.id); | |
}, | |
onTapCancel: () { | |
MyApp.buttonStateOf(context).buttonCancel(widget.id); | |
}, | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: SizedBox( | |
width: 100, | |
height: 40, | |
child: DecoratedBox( | |
decoration: BoxDecoration( | |
border: Border.all(color: Colors.black), | |
), | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Center( | |
child: Text(widget.name), | |
), | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
class PageTwo extends StatelessWidget { | |
const PageTwo._(); | |
static Route<void> route() { | |
return MaterialPageRoute( | |
builder: (context) => const PageTwo._(), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Page Two'), | |
), | |
body: const Center( | |
child: Text('Page Two'), | |
), | |
); | |
} | |
} | |
class SecretPage extends StatelessWidget { | |
const SecretPage._(); | |
static Route<void> route() { | |
return MaterialPageRoute( | |
builder: (context) => const SecretPage._(), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Secret Page'), | |
), | |
body: const Center( | |
child: Text('Secret Page'), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment