Created
April 6, 2020 09:47
-
-
Save Neamar/f81c8888fa9eaaa9825b09106d4c9fe6 to your computer and use it in GitHub Desktop.
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
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: PageView.builder( | |
itemBuilder: (context, position) { | |
Key key = ValueKey(position); | |
return Level(key); | |
}, | |
itemCount: 3, | |
), | |
), | |
); | |
} | |
} | |
class Level extends StatefulWidget { | |
Level(Key key) : super(key: key); | |
@override | |
_LevelState createState() => _LevelState(); | |
} | |
class _LevelState extends State<Level> { | |
static const MaterialColor COLOR_GAME = Colors.deepPurple; | |
static const MaterialColor COLOR_SUCCESS = Colors.green; | |
static const STATE_PLAYING = 'playing'; | |
static const STATE_WON = 'won'; | |
String gameState; | |
_LevelState() { | |
gameState = STATE_PLAYING; | |
} | |
@override | |
void dispose() { | |
print("Disposing of level, state = " + gameState); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
MaterialColor headerColor = COLOR_GAME; | |
String textToDisplay = "Click the toggle to win"; | |
if (gameState == STATE_WON) { | |
headerColor = COLOR_SUCCESS; | |
textToDisplay = "You won!"; | |
} | |
return Column(children: <Widget>[ | |
Material( | |
color: headerColor[300], | |
child: InkWell( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text(textToDisplay), | |
), | |
), | |
), | |
Expanded( | |
child: SwitchListTile( | |
title: Text("Make me win"), | |
value: gameState != STATE_PLAYING, | |
onChanged: gameState != STATE_PLAYING | |
? null | |
: (bool value) { | |
setState(() { | |
gameState = STATE_WON; | |
}); | |
}, | |
), | |
) | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment