Commit 8b0b3919 authored by Lila NICKLER's avatar Lila NICKLER

Exercice 6

parent 6e0d510b
...@@ -6,6 +6,7 @@ import 'Exercice 1.dart'; ...@@ -6,6 +6,7 @@ import 'Exercice 1.dart';
import 'Exercice 2.dart'; import 'Exercice 2.dart';
import 'Exercice 4.dart'; import 'Exercice 4.dart';
import 'Exercice 5.dart'; import 'Exercice 5.dart';
import 'Exercice 6.dart';
class DisplayExercice extends StatelessWidget{ class DisplayExercice extends StatelessWidget{
@override @override
...@@ -40,9 +41,17 @@ class DisplayExercice extends StatelessWidget{ ...@@ -40,9 +41,17 @@ class DisplayExercice extends StatelessWidget{
ListTile( ListTile(
leading: Icon(Icons.wb_sunny_outlined), leading: Icon(Icons.wb_sunny_outlined),
title: Text("Exercice 5"), title: Text("Exercice 5"),
subtitle: Text("Affichage d'une tuile a a partir d'une image"), subtitle: Text("Affichage d'une grille modulable de tuile a partir d'un image"),
onTap: (){ onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>ImageGridViewTile())); Navigator.push(context, MaterialPageRoute(builder: (context)=>ImageGridViewTile()));
},)),
Card(child:
ListTile(
leading: Icon(Icons.wb_sunny_outlined),
title: Text("Exercice 6"),
subtitle: Text("Affichage d'une tuile a a partir d'une image"),
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>PositionedTiles()));
},)) },))
], ],
......
...@@ -68,6 +68,18 @@ class _ImageGridViewTileState extends State<ImageGridViewTile> { ...@@ -68,6 +68,18 @@ class _ImageGridViewTileState extends State<ImageGridViewTile> {
return lw; return lw;
} }
Widget creatGridView(List <TileUp> _listTile, int value)
{
return GridView.count(
children: createWidgetList(_listTile, value),
mainAxisSpacing: 2,
crossAxisSpacing: 2,
crossAxisCount: value,
scrollDirection: Axis.vertical,
shrinkWrap: true,
);
}
Widget build(BuildContext context) { Widget build(BuildContext context) {
var mediaQueryData = MediaQuery.of(context); var mediaQueryData = MediaQuery.of(context);
...@@ -86,14 +98,7 @@ class _ImageGridViewTileState extends State<ImageGridViewTile> { ...@@ -86,14 +98,7 @@ class _ImageGridViewTileState extends State<ImageGridViewTile> {
height: 500, height: 500,
width: 400, width: 400,
child:Container( child:Container(
child: GridView.count( child: creatGridView(_listTile,_currentSliderValue)
children: createWidgetList(_listTile, _currentSliderValue),
mainAxisSpacing: 2,
crossAxisSpacing: 2,
crossAxisCount: _currentSliderValue,
scrollDirection: Axis.vertical,
shrinkWrap: true,
),
), ),
), ),
Container( Container(
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:math' as math; import 'dart:math' as math;
// ============== // ==============
// Models // Models
// ============== // ==============
...@@ -9,34 +8,36 @@ math.Random random = new math.Random(); ...@@ -9,34 +8,36 @@ math.Random random = new math.Random();
class Tile { class Tile {
Color color; Color color;
int index;
Tile(this.color, this.index);
Tile(this.color); Tile.randomColor(int index) {
Tile.randomColor() {
this.color = Color.fromARGB( this.color = Color.fromARGB(
255, random.nextInt(255), random.nextInt(255), random.nextInt(255)); 255, random.nextInt(255), random.nextInt(255), random.nextInt(255));
this.index = index;
} }
} }
// ==============
// Widgets
// ==============
class TileWidget extends StatelessWidget { class TileWidget extends StatelessWidget {
final Tile tile; final Tile tile;
TileWidget(this.tile); TileWidget(this.tile);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return this.coloredBox(); return this.coloredBox();
} }
Widget coloredBox() { Widget coloredBox() {
return Container( return Container(
color: tile.color, color: tile.color,
child: Padding( child:Align(
padding: EdgeInsets.all(70.0), alignment: Alignment.center,
)); child: Text(tile.index.toString(), style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold),),
)
);
} }
} }
...@@ -48,25 +49,101 @@ class PositionedTiles extends StatefulWidget { ...@@ -48,25 +49,101 @@ class PositionedTiles extends StatefulWidget {
} }
class PositionedTilesState extends State<PositionedTiles> { class PositionedTilesState extends State<PositionedTiles> {
List<Widget> tiles = int indexSwap = 0;
List<Widget>.generate(2, (index) => TileWidget(Tile.randomColor()));
GridView createGridView(List <Widget> _listTile, int value ){
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: value,
crossAxisSpacing: 2.0,
mainAxisSpacing: 2.0
),
itemCount: value * value,
itemBuilder: (BuildContext context, int index)
{ if (index == indexSwap) {
return Container(
child: TileWidget(new Tile(Colors.white, 0)),
);
}
else
{
return Container (
child: InkWell(
child: _listTile[index],
onTap: ()
{
if (index == indexSwap -1 || index == indexSwap +1 || index == indexSwap -_currentSliderValue || index == indexSwap +_currentSliderValue )
{
swapTiles(index, indexSwap);
}
},
)
);
}
});
}
int _currentSliderValue = 3;
List<Widget> tiles = List<Widget>.generate(10*10, (index) => TileWidget(Tile.randomColor(index)));
@override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var mediaQueryData = MediaQuery.of(context);
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text('Moving Tiles'), title: Text('Exercice 6'),
centerTitle: true,
), ),
body: Row(children: tiles), body: ListView(
floatingActionButton: FloatingActionButton( scrollDirection: Axis.vertical,
child: Icon(Icons.sentiment_very_satisfied), onPressed: swapTiles), shrinkWrap: true,
children: [
SizedBox(
height: 500,
width: 400,
child:Container(
child: createGridView(tiles,_currentSliderValue)
),
),
Container(
width: mediaQueryData.size.width * (7 / 8),
child: Slider(
value: _currentSliderValue.toDouble(),
min: 2,
max: 10,
divisions: 9,
label: _currentSliderValue.round().toString(),
onChanged: (double value) {
setState(() {
indexSwap = 0;
_currentSliderValue = value.toInt();
});
}),)
],)
); );
} }
swapTiles() { swapTiles(int index1, int emptyIndex) {
setState(() { setState(() {
tiles.insert(1, tiles.removeAt(0)); print("Button pressed");
tiles.insert(index1, tiles.removeAt(emptyIndex));
if (index1 == _currentSliderValue+emptyIndex)
{
tiles.insert(emptyIndex, tiles.removeAt(index1-1));
}
if (index1 == emptyIndex -_currentSliderValue)
{
tiles.insert(emptyIndex, tiles.removeAt(index1+1));
}
indexSwap = index1;
}); });
} }
} }
\ No newline at end of file
...@@ -11,7 +11,7 @@ class MyApp extends StatelessWidget { ...@@ -11,7 +11,7 @@ class MyApp extends StatelessWidget {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Flutter Demo',
theme: ThemeData( theme: ThemeData(
primarySwatch: Colors.blue, primarySwatch: Colors.deepPurple,
visualDensity: VisualDensity.adaptivePlatformDensity, visualDensity: VisualDensity.adaptivePlatformDensity,
), ),
home: MyHomePage(title: 'Flutter Demo Home Page'), home: MyHomePage(title: 'Flutter Demo Home Page'),
...@@ -36,7 +36,6 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -36,7 +36,6 @@ class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(widget.title), title: Text(widget.title),
), ),
body: Center( body: Center(
......
...@@ -49,7 +49,7 @@ packages: ...@@ -49,7 +49,7 @@ packages:
name: cupertino_icons name: cupertino_icons
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.0" version: "1.0.2"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
......
...@@ -29,6 +29,7 @@ dependencies: ...@@ -29,6 +29,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0 cupertino_icons: ^1.0.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter
......
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