Last active
November 27, 2023 05:55
-
-
Save flexboni/a85656199413d66e7c126d66de39c1f9 to your computer and use it in GitHub Desktop.
플러터챌린지 0일차 심화문제 - 김구봉
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Challenge', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key}); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
enum BorderType { rectangle, round } | |
const double width = 100; | |
const double height = 100; | |
class _MyHomePageState extends State<MyHomePage> { | |
int _rectangleAcceptedData = 1; | |
int _roundAcceptedData = 1; | |
double borderWidth = 2.0; | |
late BorderType type; | |
final Random random = Random(); | |
void randomType() { | |
type = BorderType.values[random.nextInt(BorderType.values.length)]; | |
} | |
@override | |
void initState() { | |
super.initState(); | |
randomType(); | |
} | |
void resetBorderWidth() { | |
setState(() { | |
borderWidth = 2.0; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Hello Flutter'), | |
leading: IconButton( | |
onPressed: () {}, | |
icon: const Icon(Icons.home), | |
), | |
centerTitle: true, | |
actions: [ | |
IconButton( | |
onPressed: () {}, | |
icon: const Icon(Icons.question_mark_rounded), | |
), | |
], | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
DragTarget<BorderType>( | |
onWillAccept: (data) { | |
if (data == BorderType.rectangle) { | |
setState(() { | |
borderWidth = 3.0; | |
}); | |
return true; | |
} | |
return false; | |
}, | |
onAccept: (data) { | |
if (data == BorderType.rectangle) { | |
setState(() { | |
++_rectangleAcceptedData; | |
}); | |
} | |
resetBorderWidth(); | |
}, | |
onLeave: (data) { | |
resetBorderWidth(); | |
}, | |
builder: ( | |
BuildContext context, | |
List<BorderType?> accepted, | |
List<dynamic> rejected, | |
) { | |
return Container( | |
height: height, | |
width: width, | |
alignment: Alignment.center, | |
decoration: BoxDecoration( | |
border: Border.all( | |
width: | |
type == BorderType.rectangle ? borderWidth : 2.0, | |
color: Colors.black, | |
), | |
borderRadius: const BorderRadius.all( | |
Radius.circular(10), | |
), | |
), | |
child: Text('$_rectangleAcceptedData'), | |
); | |
}, | |
), | |
DragTarget<BorderType>( | |
onWillAccept: (data) { | |
if (data == BorderType.round) { | |
setState(() { | |
borderWidth = 3.0; | |
}); | |
return true; | |
} | |
return false; | |
}, | |
onAccept: (data) { | |
if (data == BorderType.round) { | |
setState(() { | |
++_roundAcceptedData; | |
}); | |
resetBorderWidth(); | |
} | |
}, | |
onLeave: (data) { | |
resetBorderWidth(); | |
}, | |
builder: ( | |
BuildContext context, | |
List<BorderType?> accepted, | |
List<dynamic> rejected, | |
) { | |
return Container( | |
height: height, | |
width: width, | |
alignment: Alignment.center, | |
decoration: BoxDecoration( | |
border: Border.all( | |
width: type == BorderType.round ? borderWidth : 2.0, | |
color: Colors.black, | |
), | |
borderRadius: const BorderRadius.all( | |
Radius.circular(90), | |
), | |
), | |
child: Text('$_roundAcceptedData'), | |
); | |
}, | |
), | |
], | |
), | |
const SizedBox(height: 100), | |
Draggable<BorderType>( | |
data: type, | |
onDragCompleted: () { | |
setState(() { | |
randomType(); | |
}); | |
}, | |
feedback: DraggableWidget(type), | |
childWhenDragging: const SizedBox(height: height, width: width), | |
child: DraggableWidget(type), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class DraggableWidget extends StatelessWidget { | |
const DraggableWidget(this.type, {super.key}); | |
final BorderType type; | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
height: height, | |
width: width, | |
alignment: Alignment.center, | |
decoration: BoxDecoration( | |
border: Border.all(color: Colors.black, width: 2.0), | |
borderRadius: BorderRadius.all( | |
Radius.circular(type == BorderType.rectangle ? 10 : 90), | |
), | |
), | |
child: const Text('Drag Me!'), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment