Last active
July 23, 2020 19:37
-
-
Save rogood/3ee01c83a11ba4a9ffa6766649b0fd82 to your computer and use it in GitHub Desktop.
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'; | |
import 'dart:async'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Crossing(), | |
), | |
); | |
} | |
} | |
class Crossing extends StatefulWidget { | |
@override | |
CrossingState createState() => CrossingState(); | |
} | |
class CrossingState extends State<Crossing> { | |
TrafficStatus trafficStatus = TrafficStatus.go; | |
void onPressed() { | |
trafficPrepareToStop(); | |
} | |
void trafficPrepareToStop() { | |
setState(() { | |
trafficStatus = TrafficStatus.prepareToStop; | |
}); | |
Timer(Duration(seconds: 2), trafficStop); | |
} | |
void trafficStop() { | |
setState(() { | |
trafficStatus = TrafficStatus.stop; | |
}); | |
Timer(Duration(seconds: 5), trafficPrepareToGo); | |
} | |
void trafficPrepareToGo() { | |
setState(() { | |
trafficStatus = TrafficStatus.prepareToGo; | |
}); | |
Timer(Duration(seconds: 2), trafficGo); | |
} | |
void trafficGo() { | |
setState(() { | |
trafficStatus = TrafficStatus.go; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Row( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Padding( | |
padding: EdgeInsets.all(32), | |
child: TrafficLight(trafficStatus), | |
), | |
Padding( | |
padding: EdgeInsets.all(32), | |
child: | |
PedestrianLight(trafficStatus == TrafficStatus.stop, onPressed), | |
), | |
], | |
); | |
} | |
} | |
class PedestrianLight extends StatelessWidget { | |
PedestrianLight(this.isSafeToCross, this.onPressed); | |
final VoidCallback onPressed; | |
final isSafeToCross; | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: [ | |
Container( | |
color: Colors.black54, | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
Container( | |
height: 30, | |
width: 30, | |
margin: EdgeInsets.all(8), | |
decoration: BoxDecoration( | |
color: Colors.grey, | |
shape: BoxShape.circle, | |
), | |
child: !isSafeToCross | |
? Icon(Icons.person, color: Colors.red) | |
: null, | |
), | |
Container( | |
height: 30, | |
width: 30, | |
margin: EdgeInsets.all(8), | |
decoration: BoxDecoration( | |
color: Colors.grey, | |
shape: BoxShape.circle, | |
), | |
child: isSafeToCross | |
? Icon(Icons.person, color: Colors.green) | |
: null, | |
) | |
], | |
), | |
), | |
RaisedButton( | |
onPressed: () { | |
onPressed(); | |
}, | |
child: Text('I want to cross'), | |
), | |
], | |
); | |
} | |
} | |
class TrafficLight extends StatelessWidget { | |
TrafficLight(this.trafficStatus); | |
final TrafficStatus trafficStatus; | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.black54, | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
Container( | |
height: 30, | |
width: 30, | |
margin: EdgeInsets.all(8), | |
decoration: BoxDecoration( | |
color: trafficStatus == TrafficStatus.stop || | |
trafficStatus == TrafficStatus.prepareToGo | |
? Colors.red | |
: Colors.grey, | |
shape: BoxShape.circle, | |
), | |
), | |
Container( | |
height: 30, | |
width: 30, | |
margin: EdgeInsets.all(8), | |
decoration: BoxDecoration( | |
color: trafficStatus == TrafficStatus.prepareToGo || | |
trafficStatus == TrafficStatus.prepareToStop | |
? Colors.amber | |
: Colors.grey, | |
shape: BoxShape.circle, | |
), | |
), | |
Container( | |
height: 30, | |
width: 30, | |
margin: EdgeInsets.all(8), | |
decoration: BoxDecoration( | |
color: trafficStatus == TrafficStatus.go | |
? Colors.green | |
: Colors.grey, | |
shape: BoxShape.circle, | |
), | |
) | |
], | |
), | |
); | |
} | |
} | |
enum TrafficStatus { go, prepareToStop, stop, prepareToGo } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment