diff --git a/eight_queens/lib/main.dart b/eight_queens/lib/main.dart index 5ffb160..5a39a4d 100644 --- a/eight_queens/lib/main.dart +++ b/eight_queens/lib/main.dart @@ -66,7 +66,7 @@ class _MyHomePageState extends State { }); await pr.show(); await QueenResolver().solve(_boardSize); - await pr.dismiss(); + pr.dismiss(); Navigator.push(context, MaterialPageRoute(builder: (context) => ResultsPage(title: "Results"))); } diff --git a/eight_queens/lib/queens.dart b/eight_queens/lib/queens.dart index cc4fb94..6da518c 100644 --- a/eight_queens/lib/queens.dart +++ b/eight_queens/lib/queens.dart @@ -3,7 +3,7 @@ * Solved using algorithm found in: https://www.geeksforgeeks.org/n-queen-problem-using-branch-and-bound/ */ -import "dart:io"; +import 'package:shared_preferences/shared_preferences.dart'; class QueenResolver { @@ -58,7 +58,7 @@ class QueenResolver { return res; } - void _storeSolution(List> board) { + _storeSolution(List> board) async { for(int i = 0; i < board.length; i++) { for(int x = 0; x < board[i].length ; x++) { if(board[i][x] == null) { @@ -68,15 +68,23 @@ class QueenResolver { } boards.add(board); + await _saveToLocalStorage(); } + Future _saveToLocalStorage() async { + SharedPreferences.setMockInitialValues({}); + final prefs = await SharedPreferences.getInstance(); + prefs.setString('result', boards.toString()); + } + + + Future solve(int n) async { // Create the initial queens board List> board = new List.generate(n, (_) => new List(n)); if(await _solveQueens(board,0,n) == false) { - print("found solutions: "+boards.length.toString()); return false; } return true; diff --git a/eight_queens/lib/results.dart b/eight_queens/lib/results.dart index 65b3fca..0b30432 100644 --- a/eight_queens/lib/results.dart +++ b/eight_queens/lib/results.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'dart:convert'; class ResultsPage extends StatefulWidget { ResultsPage({Key key, this.title}) : super(key: key); @@ -10,6 +12,23 @@ class ResultsPage extends StatefulWidget { } class _ResultsPageState extends State { + var boards; + + Future retrieveEightQueensResults() async { + final prefs = await SharedPreferences.getInstance(); + String boardsString = await prefs.get('result'); + setState(() { + boards = jsonDecode(boardsString); + }); + } + + @override + void initState() { + retrieveEightQueensResults().then((value) { + print('async done'); + }); + super.initState(); + } @override Widget build(BuildContext context) { @@ -17,14 +36,12 @@ class _ResultsPageState extends State { appBar: AppBar( title: Text(widget.title), ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text("results") - ], - ), - ), + body: new ListView.builder( + itemCount: boards.length, + itemBuilder: (BuildContext context, int index) { + return new Text(boards[index].toString()); + }, + ) ); } } \ No newline at end of file diff --git a/eight_queens/pubspec.yaml b/eight_queens/pubspec.yaml index 1ca16e6..216c2f7 100644 --- a/eight_queens/pubspec.yaml +++ b/eight_queens/pubspec.yaml @@ -19,6 +19,7 @@ environment: dependencies: flutter: sdk: flutter + shared_preferences : ^0.5.6 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.