Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
amse
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Matis SPINELLI
amse
Commits
6feb322e
Commit
6feb322e
authored
Feb 07, 2024
by
Timothe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
midi mercredi
parent
285c5ed2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
161 additions
and
127 deletions
+161
-127
main.dart
tp1/lib/main.dart
+160
-126
widget_test.dart
tp1/test/widget_test.dart
+1
-1
No files found.
tp1/lib/main.dart
View file @
6feb322e
import
'package:english_words/english_words.dart'
;
import
'package:flutter/material.dart'
;
import
'package:provider/provider.dart'
;
import
'dart:math'
;
void
main
(
)
{
runApp
(
MyApp
());
}
/// Flutter code sample for [Draggable].
class
MyApp
extends
StatelessWidget
{
const
MyApp
({
super
.
key
});
void
main
(
)
=>
runApp
(
const
TindBookApp
());
class
TindBookApp
extends
StatelessWidget
{
const
TindBookApp
({
super
.
key
});
@override
Widget
build
(
BuildContext
context
)
{
return
ChangeNotifierProvider
(
create:
(
context
)
=>
MyApp
State
(),
create:
(
context
)
=>
TindBook
State
(),
child:
MaterialApp
(
title:
'
Namer App
'
,
title:
'
Tind Book
'
,
theme:
ThemeData
(
useMaterial3:
true
,
colorScheme:
ColorScheme
.
fromSeed
(
seedColor:
Color
.
fromARGB
(
255
,
Random
().
nextInt
(
255
),
Random
().
nextInt
(
255
),
Random
().
nextInt
(
255
))),
colorScheme:
ColorScheme
.
fromSeed
(
seedColor:
Color
.
fromARGB
(
255
,
Random
().
nextInt
(
255
),
Random
().
nextInt
(
255
),
Random
().
nextInt
(
255
))),
),
home:
Builder
(
builder:
(
context
)
{
return
Directionality
(
textDirection:
TextDirection
.
ltr
,
// or TextDirection.rtl
child:
Scaffold
(
appBar:
AppBar
(
title:
const
Text
(
'Draggable Sample'
)),
body:
MyHomePage
(),
),
);
},
),
home:
MyHomePage
(),
),
);
}
}
class
MyAppState
extends
ChangeNotifier
{
var
current
=
WordPair
.
random
();
class
TindBookState
extends
ChangeNotifier
{
void
getNext
(){
current
=
WordPair
.
random
();
notifyListeners
();
}
var
favorites
=
<
WordPair
>[];
}
void
toggleFavorite
()
{
if
(
favorites
.
contains
(
current
))
{
favorites
.
remove
(
current
);
}
else
{
favorites
.
add
(
current
);
}
notifyListeners
();
class
TindBook
extends
StatefulWidget
{
const
TindBook
({
super
.
key
});
@override
State
<
TindBook
>
createState
()
=>
_TindBookApp
();
}
class
_TindBookApp
extends
State
<
TindBook
>
{
bool
acceptedData
=
false
;
double
dragDistance
=
0.0
;
@override
Widget
build
(
BuildContext
context
)
{
return
LayoutBuilder
(
builder:
(
context
,
constraints
)
{
double
containerWidth
=
constraints
.
maxWidth
/
3
;
// Adjust as needed
double
containerHeight
=
constraints
.
maxHeight
;
// Adjust as needed
return
Row
(
mainAxisAlignment:
MainAxisAlignment
.
spaceEvenly
,
children:
<
Widget
>[
DragTarget
<
bool
>(
builder:
(
BuildContext
context
,
List
<
dynamic
>
accepted
,
List
<
dynamic
>
rejected
,
)
{
return
Container
(
height:
containerHeight
,
width:
containerWidth
,
color:
Colors
.
cyan
,
child:
Center
(
child:
Text
(
'Value is updated to:
$acceptedData
'
),
),
);
},
onAccept:
(
bool
data
)
{
setState
(()
{
acceptedData
=
data
;
acceptedData
=
false
;
});
},
),
GestureDetector
(
onHorizontalDragUpdate:
(
details
)
{
setState
(()
{
print
(
'selected:
${details.primaryDelta}
'
);
dragDistance
=
details
.
primaryDelta
!;
});
},
child:
Draggable
<
bool
>(
// Data is the value this Draggable stores.
data:
true
,
feedback:
AnimatedContainer
(
duration:
Duration
(
milliseconds:
4
),
// No animation for feedback
transform:
Matrix4
.
rotationZ
(
dragDistance
*
50
),
child:
Container
(
color:
Colors
.
deepOrange
,
height:
containerHeight
,
width:
containerWidth
,
child:
const
Icon
(
Icons
.
directions_run
),
),
),
childWhenDragging:
Container
(
height:
containerHeight
,
width:
containerWidth
,
color:
Colors
.
pinkAccent
,
child:
const
Center
(
child:
Text
(
'Child When Dragging'
),
),
),
child:
Container
(
height:
containerHeight
,
width:
containerWidth
,
color:
const
Color
.
fromARGB
(
255
,
255
,
89
,
89
),
child:
const
Center
(
child:
Text
(
'Draggable'
),
),
),
),
),
DragTarget
<
bool
>(
builder:
(
BuildContext
context
,
List
<
dynamic
>
accepted
,
List
<
dynamic
>
rejected
,
)
{
return
Container
(
height:
containerHeight
,
width:
containerWidth
,
color:
Colors
.
cyan
,
child:
Center
(
child:
Text
(
'Value is updated to:
$acceptedData
'
),
),
);
},
onAccept:
(
bool
data
)
{
setState
(()
{
acceptedData
=
data
;
});
},
),
],
);
},
);
}
}
// test
class
FavoritePage
extends
StatelessWidget
{
@override
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
body:
Column
(
children:
[
Text
(
'Test'
),
],)
);
}
}
class
MyHomePage
extends
StatefulWidget
{
@override
State
<
MyHomePage
>
createState
()
=>
_MyHomePageState
();
...
...
@@ -62,11 +188,14 @@ class _MyHomePageState extends State<MyHomePage> {
Widget
page
;
switch
(
selectedIndex
)
{
case
0
:
page
=
GeneratorPage
();
page
=
TindBook
();
break
;
case
1
:
page
=
FavoritePage
();
break
;
case
2
:
page
=
Placeholder
();
break
;
default
:
throw
UnimplementedError
(
'no widget for
$selectedIndex
'
);
}
...
...
@@ -85,9 +214,13 @@ class _MyHomePageState extends State<MyHomePage> {
label:
Text
(
'Home'
),
),
NavigationRailDestination
(
icon:
Icon
(
Icons
.
favorit
e
),
label:
Text
(
'
Favorites
'
),
icon:
Icon
(
Icons
.
swip
e
),
label:
Text
(
'
Swipe
'
),
),
NavigationRailDestination
(
icon:
Icon
(
Icons
.
info
),
label:
Text
(
'About'
),
),
],
selectedIndex:
selectedIndex
,
onDestinationSelected:
(
value
)
{
...
...
@@ -108,105 +241,6 @@ class _MyHomePageState extends State<MyHomePage> {
),
);
}
);
}
}
class
GeneratorPage
extends
StatelessWidget
{
@override
Widget
build
(
BuildContext
context
)
{
var
appState
=
context
.
watch
<
MyAppState
>();
var
pair
=
appState
.
current
;
IconData
icon
;
if
(
appState
.
favorites
.
contains
(
pair
))
{
icon
=
Icons
.
favorite
;
}
else
{
icon
=
Icons
.
favorite_border
;
}
return
Center
(
child:
Column
(
mainAxisAlignment:
MainAxisAlignment
.
center
,
children:
[
BigCard
(
pair:
pair
),
SizedBox
(
height:
10
),
Row
(
mainAxisSize:
MainAxisSize
.
min
,
children:
[
ElevatedButton
.
icon
(
onPressed:
()
{
appState
.
toggleFavorite
();
},
icon:
Icon
(
icon
),
label:
Text
(
'Like'
),
),
SizedBox
(
width:
10
),
ElevatedButton
(
onPressed:
()
{
appState
.
getNext
();
},
child:
Text
(
'Next'
),
),
],
),
],
),
);
}
}
class
BigCard
extends
StatelessWidget
{
const
BigCard
({
super
.
key
,
required
this
.
pair
,
});
final
WordPair
pair
;
@override
Widget
build
(
BuildContext
context
)
{
final
theme
=
Theme
.
of
(
context
);
final
style
=
theme
.
textTheme
.
displayMedium
!.
copyWith
(
color:
theme
.
colorScheme
.
onPrimary
,
);
return
Card
(
color:
theme
.
colorScheme
.
primary
,
child:
Padding
(
padding:
const
EdgeInsets
.
all
(
20
),
child:
Text
(
pair
.
asLowerCase
,
style:
style
,
semanticsLabel:
"
${pair.first}
${pair.second}
"
,
),
),
);
}
}
class
FavoritePage
extends
StatelessWidget
{
@override
Widget
build
(
BuildContext
context
)
{
var
appState
=
context
.
watch
<
MyAppState
>();
var
favorites
=
appState
.
favorites
;
return
Scaffold
(
body:
Column
(
children:
[
Text
(
'Favorites'
),
for
(
var
fav
in
favorites
)
Text
(
fav
.
asLowerCase
),
],)
);
}
}
\ No newline at end of file
tp1/test/widget_test.dart
View file @
6feb322e
...
...
@@ -13,7 +13,7 @@ import 'package:tp1/main.dart';
void
main
(
)
{
testWidgets
(
'Counter increments smoke test'
,
(
WidgetTester
tester
)
async
{
// Build our app and trigger a frame.
await
tester
.
pumpWidget
(
const
MyApp
());
await
tester
.
pumpWidget
(
const
TindBook
());
// Verify that our counter starts at 0.
expect
(
find
.
text
(
'0'
),
findsOneWidget
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment