Created with <3 with dartpad.dev.
Last active
May 2, 2023 13:06
-
-
Save hongsw/348d3fd4e1701025953ee6813d4876d2 to your computer and use it in GitHub Desktop.
Jump Inheritance Example
This file contains hidden or 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 StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
int score = 0; | |
void runCar(Jump jump) { | |
setState(() { | |
score = score + jump.move(); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: Text('Jump Inheritance Example')), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Text('Distance: ${score} meters'), | |
SizedBox(height: 20), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
ElevatedButton( | |
onPressed: () => runCar(NormalJump()), | |
child: Text('Jump'), | |
), | |
ElevatedButton( | |
onPressed: () => runCar(SuperJump()), | |
child: Text('Super jump'), | |
), | |
], | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class Jump { | |
int move() { | |
return 0; | |
} | |
} | |
class NormalJump extends Jump { | |
@override | |
int move() { | |
return 10; | |
} | |
} | |
class SuperJump extends Jump { | |
@override | |
int move() { | |
return 50; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment