Created
August 4, 2020 13:31
-
-
Save ravindu9701/8976303b94af2afd3a9b21a332b134ea 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
class DrawScreen extends StatefulWidget { | |
@override | |
_DrawScreenState createState() => _DrawScreenState(); | |
} | |
class _DrawScreenState extends State<DrawScreen> { | |
final _points = List<Offset>(); | |
final _recognizer = Recognizer(); | |
List<Prediction> _prediction; | |
bool initialize = false; | |
@override | |
void initState() { | |
super.initState(); | |
_initModel(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
centerTitle: true, | |
title: Text('Digit Recognizer'), | |
), | |
body: Column( | |
children: <Widget>[ | |
SizedBox( | |
height: 10, | |
), | |
_drawCanvasWidget(), | |
SizedBox( | |
height: 10, | |
), | |
PredictionWidget( | |
predictions: _prediction, | |
), | |
], | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Text("Clear"), | |
onPressed: () { | |
setState(() { | |
_points.clear(); | |
_prediction.clear(); | |
}); | |
}, | |
), | |
); | |
} | |
Widget _drawCanvasWidget() { | |
return Container( | |
width: Constants.canvasSize + Constants.borderSize * 2, | |
height: Constants.canvasSize + Constants.borderSize * 2, | |
decoration: BoxDecoration( | |
border: Border.all( | |
color: Colors.black, | |
width: Constants.borderSize, | |
), | |
), | |
child: GestureDetector( | |
onPanUpdate: (DragUpdateDetails details) { | |
Offset _localPosition = details.localPosition; | |
if (_localPosition.dx >= 0 && | |
_localPosition.dx <= Constants.canvasSize && | |
_localPosition.dy >= 0 && | |
_localPosition.dy <= Constants.canvasSize) { | |
setState(() { | |
_points.add(_localPosition); | |
}); | |
} | |
}, | |
onPanEnd: (DragEndDetails details) { | |
_points.add(null); | |
_recognize(); | |
}, | |
child: CustomPaint( | |
painter: DrawingPainter(_points), | |
), | |
), | |
); | |
} | |
void _initModel() async { | |
var res = await _recognizer.loadModel(); | |
} | |
void _recognize() async { | |
List<dynamic> pred = await _recognizer.recognize(_points); | |
setState(() { | |
_prediction = pred.map((json) => Prediction.fromJson(json)).toList(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment