Commit aee7978f authored by Benjamin LEROUX's avatar Benjamin LEROUX

exo6 taille fixe

parent 21a291b9
import 'package:flutter/material.dart';
import 'dart:math' as math;
// ==============
// Models
// ==============
math.Random random = new math.Random();
class Tile {
Color color;
String index;
Tile(this.index,this.color);
}
// ==============
// Widgets
// ==============
class TileWidget extends StatelessWidget {
final Tile tile;
TileWidget(this.tile);
@override
Widget build(BuildContext context) {
return this.coloredBox();
}
Widget coloredBox() {
return Container(
color: tile.color,
child: Align(
child: Text(tile.index.toString(),),
alignment: Alignment.center,
)
);
}
}
int emptyIndex=0;
int size=4;
List<Widget>createTileWidgets(){
List<Widget> tiles = <Widget>[];
int max=size*size;
tiles.add(TileWidget(Tile("Vide",Colors.white)));
for(int i=0;i<max-1;i++){
tiles.add(TileWidget(Tile((i+1).toString(), Colors.grey)));
}
return tiles;
}
String getIndex(TileWidget w){
return w.tile.index;
}
void main() => runApp(new MaterialApp(home: PositionedTiles()));
class PositionedTiles extends StatefulWidget {
@override
State<StatefulWidget> createState() => PositionedTilesState();
}
class PositionedTilesState extends State<PositionedTiles> {
List<Widget> tiles = createTileWidgets();
Widget icone = Icon(Icons.sentiment_very_satisfied);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Moving Tiles'),
centerTitle: true,
),
body: GridView.builder(
padding: const EdgeInsets.all(10),
itemCount: size*size,
gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: size,
crossAxisSpacing: 5.0,
mainAxisSpacing: 5.0,
),
itemBuilder: (BuildContext context, int index) {
return InkWell(
child: tiles[index],
onTap: (){
swapTiles(index,emptyIndex);
},
);
},
),
);
}
swapTiles(int current, int empty) {
setState(() {
icone=Icon(Icons.favorite);
print(current);
if(current==empty+1 && current%size!=0){
emptyIndex=current;
tiles.insert(empty, tiles.removeAt(current));
}else if(current==empty-1 && empty%size!=0){
emptyIndex=current;
tiles.insert(empty, tiles.removeAt(current));
}
else if(current==empty+size){
emptyIndex=current;
tiles.insert(current, tiles.removeAt(empty));
tiles.insert(empty, tiles.removeAt(current-1));
}else if(current==empty-size){
emptyIndex=current;
tiles.insert(current, tiles.removeAt(empty));
tiles.insert(empty, tiles.removeAt(current+1));
}else print("Can't move this");
});
}
}
\ No newline at end of file
...@@ -4,6 +4,7 @@ import 'package:tp2_taquin/Tile.dart'; ...@@ -4,6 +4,7 @@ import 'package:tp2_taquin/Tile.dart';
import 'exo1.dart'; import 'exo1.dart';
import 'exo2.dart'; import 'exo2.dart';
import 'exo6.dart';
void main() { void main() {
runApp(MyApp()); runApp(MyApp());
...@@ -94,6 +95,19 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -94,6 +95,19 @@ class _MyHomePageState extends State<MyHomePage> {
], ],
), ),
), ),
Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
trailing: Icon(Icons.play_arrow),
title: Text('Exercice 6'),
subtitle: Text("Animation des tuiles"),
onTap:() {Navigator.push(context, MaterialPageRoute(builder: (context) =>PositionedTiles()));},
),
],
),
),
], ],
), ),
), ),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment