From 7e1aba1e0fae717b973882d8c1000314b4d6f9bd Mon Sep 17 00:00:00 2001 From: Dante8276 <53953222+Dante8276@users.noreply.github.com> Date: Mon, 24 Aug 2020 14:37:34 +0530 Subject: [PATCH] Add files via upload --- lib/main.dart | 498 ++++++++++++------------ lib/main_body.dart | 122 +++--- lib/paints/bars.dart | 69 ++-- lib/paints/dots.dart | 93 +++-- lib/sotring_algorithms/bubble_sort.dart | 40 +- 5 files changed, 412 insertions(+), 410 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index a8b50aa..1ce46d7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,247 +1,251 @@ -import 'dart:async'; -import 'dart:math'; - -import 'package:flutter/material.dart'; -import 'package:sorting_visulalization/sotring_algorithms/bubble_sort.dart'; -import 'package:sorting_visulalization/main_body.dart'; - -StreamController> streamController = StreamController(); -List numbers = []; -var width, height; -String currentSortAlgo = 'bubble'; -String currentPlotStyle = 'bar'; -double sampleSize = 320; -bool isSorted = false; -bool isSorting = false; -int speed = 0; -int duration = 1500; -GlobalKey scaffoldKey = GlobalKey(); -Duration getDuration() { - return Duration(microseconds: duration); -} - -void main() => runApp(MyApp()); - -class MyApp extends StatelessWidget { - @override - Widget build(BuildContext context) { - return MaterialApp( - title: 'Flutter Demo', - theme: ThemeData( - scaffoldBackgroundColor: Color(0xFF2e294e), - primarySwatch: Colors.blue, - ), - home: MyHomePage(), - debugShowCheckedModeBanner: false, - ); - } -} - -class MyHomePage extends StatefulWidget { - @override - MyHomePageState createState() => MyHomePageState(); -} - -class MyHomePageState extends State { - @override - void didChangeDependencies() { - super.didChangeDependencies(); - sampleSize = MediaQuery.of(context).size.width / 2; - for (int i = 0; i < sampleSize; ++i) { - numbers.add(Random().nextInt(500)); - } - setState(() {}); - } - - @override - void dispose() { - streamController.close(); - super.dispose(); - } - - reset() { - isSorted = false; - numbers = []; - for (int i = 0; i < sampleSize; ++i) { - numbers.add(Random().nextInt(500)); - } - streamController.add(numbers); - } - - setSortAlgo(String type) { - setState(() { - currentSortAlgo = type; - }); - } - - setPlotStyle(String type) { - setState(() { - currentPlotStyle = type; - }); - } - - checkAndResetIfSorted() async { - if (isSorted) { - reset(); - await Future.delayed(Duration(milliseconds: 200)); - } - } - - String getTitle() { - switch (currentSortAlgo) { - case "bubble": - return "Bubble Sort"; - break; - } - return "done"; - } - - changeSpeed() { - if (speed >= 3) { - speed = 0; - duration = 1500; - } else { - speed++; - duration = duration ~/ 2; - } - - print(speed.toString() + " " + duration.toString()); - setState(() {}); - } - - sort() async { - setState(() { - isSorting = true; - }); - - await checkAndResetIfSorted(); - - Stopwatch stopwatch = new Stopwatch()..start(); - - switch (currentSortAlgo) { - case "bubble": - await bubbleSort(); - break; - } - - stopwatch.stop(); - - scaffoldKey.currentState.removeCurrentSnackBar(); - scaffoldKey.currentState.showSnackBar( - SnackBar( - content: Text( - "Sorting completed in ${stopwatch.elapsed.inMilliseconds} ms.", - ), - ), - ); - setState(() { - isSorting = false; - isSorted = true; - }); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - key: scaffoldKey, - body: SafeArea( - child: Column( - children: [ - Container( - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - DropdownButtonHideUnderline( - child: DropdownButton( - iconSize: 30, - style: TextStyle( - color: Colors.white, - fontSize: 18, - fontWeight: FontWeight.bold), - dropdownColor: Color(0xFF011638), - value: currentSortAlgo, - items: [ - DropdownMenuItem( - child: Text( - "Bubble Sort", - ), - value: 'bubble', - ), - ], - onChanged: (String value) { - reset(); - setSortAlgo(value); - }, - ), - ), - DropdownButtonHideUnderline( - child: DropdownButton( - iconSize: 30, - style: TextStyle( - color: Colors.white, - fontSize: 18, - fontWeight: FontWeight.bold), - dropdownColor: Color(0xFF011638), - value: currentPlotStyle, - items: [ - DropdownMenuItem( - child: Text("Bars"), - value: 'bar', - ), - DropdownMenuItem( - child: Text("Dots"), - value: 'dot', - ) - ], - onChanged: (String value) { - reset(); - setPlotStyle(value); - }, - ), - ) - ], - )), - Container( - child: MainBody(), - ), - ], - ), - ), - bottomNavigationBar: BottomAppBar( - color: Color(0xFF011638), - child: Row( - children: [ - Expanded( - child: FlatButton( - onPressed: isSorting - ? null - : () { - reset(); - setSortAlgo(currentSortAlgo); - }, - child: Text( - "RESET", - style: TextStyle(color: Colors.white), - ))), - Expanded( - child: FlatButton( - padding: EdgeInsets.all(18), - onPressed: isSorting ? null : sort, - child: Text("SORT", - style: TextStyle( - color: Colors.white, - fontSize: 20, - fontWeight: FontWeight.bold)))), - Expanded( - child: FlatButton( - onPressed: isSorting ? null : changeSpeed, - child: Text( - "${speed + 1}x", - style: TextStyle(fontSize: 18, color: Colors.white), - ))), - ], - ), - ), - ); - } -} +import 'dart:async'; +import 'dart:math'; + +import 'package:flutter/material.dart'; +import 'package:sorting_visulalization/sotring_algorithms/bubble_sort.dart'; +import 'package:sorting_visulalization/main_body.dart'; + +StreamController> streamController = StreamController(); +List numbers = []; +List colors = []; +var width, height; +String currentSortAlgo = 'bubble'; +String currentPlotStyle = 'bar'; +double sampleSize = 320; +bool isSorted = false; +bool isSorting = false; +int speed = 0; +int duration = 1500; +GlobalKey scaffoldKey = GlobalKey(); +Duration getDuration() { + return Duration(microseconds: duration); +} + +void main() => runApp(MyApp()); + +class MyApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Flutter Demo', + theme: ThemeData( + scaffoldBackgroundColor: Color(0xFF2e294e), + primarySwatch: Colors.blue, + ), + home: MyHomePage(), + debugShowCheckedModeBanner: false, + ); + } +} + +class MyHomePage extends StatefulWidget { + @override + MyHomePageState createState() => MyHomePageState(); +} + +class MyHomePageState extends State { + @override + void didChangeDependencies() { + super.didChangeDependencies(); + sampleSize = MediaQuery.of(context).size.width / 2; + for (int i = 0; i < sampleSize; ++i) { + numbers.add(Random().nextInt(500)); + colors.add(0); + } + print(numbers.length); + setState(() {}); + } + + @override + void dispose() { + streamController.close(); + super.dispose(); + } + + reset() { + isSorted = false; + numbers = []; + for (int i = 0; i < sampleSize; ++i) { + numbers.add(Random().nextInt(500)); + colors.add(0); + } + streamController.add(numbers); + } + + setSortAlgo(String type) { + setState(() { + currentSortAlgo = type; + }); + } + + setPlotStyle(String type) { + setState(() { + currentPlotStyle = type; + }); + } + + checkAndResetIfSorted() async { + if (isSorted) { + reset(); + await Future.delayed(Duration(milliseconds: 200)); + } + } + + String getTitle() { + switch (currentSortAlgo) { + case "bubble": + return "Bubble Sort"; + break; + } + return "done"; + } + + changeSpeed() { + if (speed >= 3) { + speed = 0; + duration = 1500; + } else { + speed++; + duration = duration ~/ 2; + } + + print(speed.toString() + " " + duration.toString()); + setState(() {}); + } + + sort() async { + setState(() { + isSorting = true; + }); + + await checkAndResetIfSorted(); + + Stopwatch stopwatch = new Stopwatch()..start(); + + switch (currentSortAlgo) { + case "bubble": + await bubbleSort(); + break; + } + + stopwatch.stop(); + + scaffoldKey.currentState.removeCurrentSnackBar(); + scaffoldKey.currentState.showSnackBar( + SnackBar( + content: Text( + "Sorting completed in ${stopwatch.elapsed.inMilliseconds} ms.", + ), + ), + ); + setState(() { + isSorting = false; + isSorted = true; + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + key: scaffoldKey, + body: SafeArea( + child: Column( + children: [ + Container( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + DropdownButtonHideUnderline( + child: DropdownButton( + iconSize: 30, + style: TextStyle( + color: Colors.white, + fontSize: 18, + fontWeight: FontWeight.bold), + focusColor: Color(0xFF011638), + value: currentSortAlgo, + items: [ + DropdownMenuItem( + child: Text( + "Bubble Sort", + ), + value: 'bubble', + ), + ], + onChanged: (String value) { + reset(); + setSortAlgo(value); + }, + ), + ), + DropdownButtonHideUnderline( + child: DropdownButton( + iconSize: 30, + style: TextStyle( + color: Colors.white, + fontSize: 18, + fontWeight: FontWeight.bold), + focusColor: Color(0xFF011638), + value: currentPlotStyle, + items: [ + DropdownMenuItem( + child: Text("Bars"), + value: 'bar', + ), + DropdownMenuItem( + child: Text("Dots"), + value: 'dot', + ) + ], + onChanged: (String value) { + reset(); + setPlotStyle(value); + }, + ), + ) + ], + )), + Container( + child: MainBody(), + ), + ], + ), + ), + bottomNavigationBar: BottomAppBar( + color: Color(0xFF011638), + child: Row( + children: [ + Expanded( + child: FlatButton( + onPressed: isSorting + ? null + : () { + reset(); + setSortAlgo(currentSortAlgo); + }, + child: Text( + "RESET", + style: TextStyle(color: Colors.white), + ))), + Expanded( + child: FlatButton( + padding: EdgeInsets.all(18), + onPressed: isSorting ? null : sort, + child: Text("SORT", + style: TextStyle( + color: Colors.white, + fontSize: 20, + fontWeight: FontWeight.bold)))), + Expanded( + child: FlatButton( + onPressed: isSorting ? null : changeSpeed, + child: Text( + "${speed + 1}x", + style: TextStyle(fontSize: 18, color: Colors.white), + ))), + ], + ), + ), + ); + } +} diff --git a/lib/main_body.dart b/lib/main_body.dart index 48af092..09318f0 100644 --- a/lib/main_body.dart +++ b/lib/main_body.dart @@ -1,61 +1,61 @@ -import 'dart:ui'; - -import 'package:sorting_visulalization/main.dart'; -import 'package:flutter/material.dart'; -import 'package:sorting_visulalization/paints/bars.dart'; -import 'package:sorting_visulalization/paints/dots.dart'; - -class MainBody extends StatelessWidget { - const MainBody({ - Key key, - }) : super(key: key); - - @override - Widget build(BuildContext context) { - return SafeArea( - child: Container( - padding: const EdgeInsets.only(top: 0.0), - child: StreamBuilder( - initialData: numbers, - stream: streamController.stream, - builder: (context, snapshot) { - List numbers = snapshot.data; - int counter = 0; - width = MediaQuery.of(context).size.width; - height = MediaQuery.of(context).size.height; - if (currentPlotStyle == 'bar') { - return Row( - children: numbers.map((int num) { - counter++; - return Container( - child: CustomPaint( - painter: BarPainter( - index: counter, - value: num, - width: - MediaQuery.of(context).size.width / sampleSize), - ), - ); - }).toList(), - ); - } else { - return Row( - children: numbers.map((int num) { - counter++; - return Container( - child: CustomPaint( - painter: DotPainter( - index: counter, - value: num, - width: - MediaQuery.of(context).size.width / sampleSize), - ), - ); - }).toList(), - ); - } - }), - ), - ); - } -} +import 'dart:ui'; + +import 'package:sorting_visulalization/main.dart'; +import 'package:flutter/material.dart'; +import 'package:sorting_visulalization/paints/bars.dart'; +import 'package:sorting_visulalization/paints/dots.dart'; + +class MainBody extends StatelessWidget { + const MainBody({ + Key key, + }) : super(key: key); + + @override + Widget build(BuildContext context) { + return SafeArea( + child: Container( + padding: const EdgeInsets.only(top: 0.0), + child: StreamBuilder( + initialData: numbers, + stream: streamController.stream, + builder: (context, snapshot) { + List numbers = snapshot.data; + int counter = 0; + width = MediaQuery.of(context).size.width; + height = MediaQuery.of(context).size.height; + if (currentPlotStyle == 'bar') { + return Row( + children: numbers.map((int num) { + counter++; + return Container( + child: CustomPaint( + painter: BarPainter( + index: counter, + value: num, + width: + MediaQuery.of(context).size.width / sampleSize), + ), + ); + }).toList(), + ); + } else { + return Row( + children: numbers.map((int num) { + counter++; + return Container( + child: CustomPaint( + painter: DotPainter( + index: counter, + value: num, + width: + MediaQuery.of(context).size.width / sampleSize), + ), + ); + }).toList(), + ); + } + }), + ), + ); + } +} diff --git a/lib/paints/bars.dart b/lib/paints/bars.dart index 6f0d7f1..9624261 100644 --- a/lib/paints/bars.dart +++ b/lib/paints/bars.dart @@ -1,36 +1,33 @@ -import 'package:flutter/material.dart'; -import 'package:sorting_visulalization/main.dart'; - -class BarPainter extends CustomPainter { - final double width; - final int value; - final int index; - - BarPainter({this.width, this.value, this.index}); - - @override - void paint(Canvas canvas, Size size) { - Paint paint = Paint(); - if (value < 500 * .25) - paint.color = Color(0xFFf38375); - else if (value < 500 * .50) - paint.color = Color(0xFFf7a399); - else if (value < 500 * .75) - paint.color = Color(0xFFfbc3bc); - else - paint.color = Color(0xFFffe3e0); - paint.strokeWidth = width; - paint.strokeCap = StrokeCap.round; - - canvas.drawLine( - Offset(index * this.width, height - height / 20), - Offset(index * this.width, - height - .25 * height - this.value.ceilToDouble()), - paint); - } - - @override - bool shouldRepaint(CustomPainter oldDelegate) { - return true; - } -} +import 'package:flutter/material.dart'; +import 'package:sorting_visulalization/main.dart'; + +class BarPainter extends CustomPainter { + final double width; + final int value; + final int index; + + BarPainter({this.width, this.value, this.index}); + + @override + void paint(Canvas canvas, Size size) { + Paint paint = Paint(); + if (colors[index] == 1) { + paint.color = Colors.red; + } else { + paint.color = Colors.white; + } + paint.strokeWidth = width; + paint.strokeCap = StrokeCap.round; + + canvas.drawLine( + Offset(index * this.width, height - height / 20), + Offset(index * this.width, + height - .25 * height - this.value.ceilToDouble()), + paint); + } + + @override + bool shouldRepaint(CustomPainter oldDelegate) { + return true; + } +} diff --git a/lib/paints/dots.dart b/lib/paints/dots.dart index 53123ed..c8b6c32 100644 --- a/lib/paints/dots.dart +++ b/lib/paints/dots.dart @@ -1,48 +1,45 @@ -import 'dart:async'; -import 'dart:math'; -import 'dart:ui'; - -import 'package:sorting_visulalization/main.dart'; -import 'package:flutter/material.dart'; - -class DotPainter extends CustomPainter { - final double width; - final int value; - final int index; - - DotPainter({this.width, this.value, this.index}); - - @override - void paint(Canvas canvas, Size size) { - Paint paint = Paint(); - if (value < 500 * .25) - paint.color = Color(0xFFf38375); - else if (value < 500 * .50) - paint.color = Color(0xFFf7a399); - else if (value < 500 * .75) - paint.color = Color(0xFFfbc3bc); - else - paint.color = Color(0xFFffe3e0); - paint.strokeWidth = width; - paint.strokeCap = StrokeCap.round; - canvas.drawLine( - Offset(index * this.width, - height - .25 * height - this.value.ceilToDouble()), - Offset(index * this.width, - height - .25 * height - this.value.ceilToDouble()), - paint); - paint.strokeWidth = width * 5; - canvas.drawPoints( - PointMode.points, - [ - Offset(index * this.width, - height - .25 * height - this.value.ceilToDouble()) - ], - paint); - } - - @override - bool shouldRepaint(CustomPainter oldDelegate) { - return true; - } -} +import 'dart:async'; +import 'dart:math'; +import 'dart:ui'; + +import 'package:sorting_visulalization/main.dart'; +import 'package:flutter/material.dart'; + +class DotPainter extends CustomPainter { + final double width; + final int value; + final int index; + + DotPainter({this.width, this.value, this.index}); + + @override + void paint(Canvas canvas, Size size) { + Paint paint = Paint(); + if (colors[index] == 1) { + paint.color = Colors.red; + } else { + paint.color = Colors.white; + } + paint.strokeWidth = width; + paint.strokeCap = StrokeCap.round; + canvas.drawLine( + Offset(index * this.width, + height - .25 * height - this.value.ceilToDouble()), + Offset(index * this.width, + height - .25 * height - this.value.ceilToDouble()), + paint); + paint.strokeWidth = width * 5; + canvas.drawPoints( + PointMode.points, + [ + Offset(index * this.width, + height - .25 * height - this.value.ceilToDouble()) + ], + paint); + } + + @override + bool shouldRepaint(CustomPainter oldDelegate) { + return true; + } +} diff --git a/lib/sotring_algorithms/bubble_sort.dart b/lib/sotring_algorithms/bubble_sort.dart index 23bee66..edc870a 100644 --- a/lib/sotring_algorithms/bubble_sort.dart +++ b/lib/sotring_algorithms/bubble_sort.dart @@ -1,18 +1,22 @@ -import 'dart:async'; -import 'package:sorting_visulalization/main.dart'; - -bubbleSort() async { - for (int i = 0; i < numbers.length; ++i) { - for (int j = 0; j < numbers.length - i - 1; ++j) { - if (numbers[j] > numbers[j + 1]) { - int temp = numbers[j]; - numbers[j] = numbers[j + 1]; - numbers[j + 1] = temp; - } - - await Future.delayed(getDuration(), () {}); - - streamController.add(numbers); - } - } -} +import 'dart:async'; +import 'package:sorting_visulalization/main.dart'; + +bubbleSort() async { + for (int i = 0; i < numbers.length; ++i) { + for (int j = 0; j < numbers.length - i - 1; ++j) { + if (numbers[j] > numbers[j + 1]) { + colors[j] = 1; + colors[j + 1] = 1; + int temp = numbers[j]; + numbers[j] = numbers[j + 1]; + numbers[j + 1] = temp; + } + + await Future.delayed(getDuration(), () {}); + + streamController.add(numbers); + colors[j] = 0; + colors[j + 1] = 0; + } + } +}