Skip to content

Instantly share code, notes, and snippets.

@hongsw
Last active May 2, 2023 13:06
Show Gist options
  • Save hongsw/348d3fd4e1701025953ee6813d4876d2 to your computer and use it in GitHub Desktop.
Save hongsw/348d3fd4e1701025953ee6813d4876d2 to your computer and use it in GitHub Desktop.
Jump Inheritance Example

Jump Inheritance Example

Created with <3 with dartpad.dev.

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