Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type for items #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 57 additions & 51 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import 'package:flutter/material.dart';
import 'package:cool_dropdown/cool_dropdown.dart';
import 'package:cool_dropdown/drop_down_params.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

void main() {
runApp(MyApp());
}

class FruitQuantity {
final String name;
final int quantity;

FruitQuantity({this.name, this.quantity});
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

List dropdownItemList = [];

List<String> pokemons = [
'pikachu',
'charmander',
Expand All @@ -23,60 +29,60 @@ List<String> pokemons = [
'psyduck',
'meowth'
];
List<String> fruits = [
'apple',
'banana',
'grapes',
'lemon',
'melon',
'orange',
'pineapple',
'strawberry',
'watermelon',

List<DropDownParams<FruitQuantity>> dropdownItemList = [];

List<FruitQuantity> fruits = [
FruitQuantity(name: 'apple', quantity: 12),
FruitQuantity(name: 'banana', quantity: 4),
FruitQuantity(name: 'grapes', quantity: 2),
FruitQuantity(name: 'lemon', quantity: 8),
FruitQuantity(name: 'melon', quantity: 2),
FruitQuantity(name: 'orange', quantity: 7),
FruitQuantity(name: 'pineapple', quantity: 1),
FruitQuantity(name: 'strawberry', quantity: 4),
FruitQuantity(name: 'watermelon', quantity: 2),
];

class _MyAppState extends State<MyApp> {
List<Map> pokemonsMap = [];
List<DropDownParams<String>> pokemonsDropdownList = [];
@override
void initState() {
for (var i = 0; i < pokemons.length; i++) {
pokemonsMap.add({
'label': '${pokemons[i]}',
'value': '${pokemons[i]}',
'icon': Container(
height: 25,
width: 25,
child: SvgPicture.asset(
'assets/${pokemons[i]}.svg',
),
),
});
}
for (var i = 0; i < fruits.length; i++) {
dropdownItemList.add(
{
'label': fruits[i] == 'melon' ? 'melon sugar high' : '${fruits[i]}',
// 'label': '${fruits[i]}',
'value': '${fruits[i]}',
'icon': Container(
key: UniqueKey(),
height: 20,
width: 20,
pokemonsDropdownList = pokemons.map((pokemon) => DropDownParams(
label: pokemon,
value: pokemon,
icon: Container(
height: 25,
width: 25,
child: SvgPicture.asset(
'assets/${fruits[i]}.svg',
'assets/$pokemon.svg',
),
),
'selectedIcon': Container(
key: UniqueKey(),
width: 20,
height: 20,
child: SvgPicture.asset(
'assets/${fruits[i]}.svg',
color: Color(0xFF6FCC76),
),
));

for (var i = 0; i < fruits.length; i++) {
dropdownItemList.add(DropDownParams<FruitQuantity>(
label: fruits[i].name == 'melon' ? 'melon sugar high' : fruits[i].name,
// 'label': '${fruits[i]}',
value: fruits[i],
icon: Container(
key: UniqueKey(),
height: 20,
width: 20,
child: SvgPicture.asset(
'assets/${fruits[i]}.svg',
),
},
);
),
selectedIcon: Container(
key: UniqueKey(),
width: 20,
height: 20,
child: SvgPicture.asset(
'assets/${fruits[i]}.svg',
color: Color(0xFF6FCC76),
),
),
));
}
super.initState();
}
Expand All @@ -96,7 +102,7 @@ class _MyAppState extends State<MyApp> {
height: 100,
),
Center(
child: CoolDropdown(
child: CoolDropdown<FruitQuantity>(
dropdownList: dropdownItemList,
onChange: (selectedItem) {
print(selectedItem);
Expand Down Expand Up @@ -147,8 +153,8 @@ class _MyAppState extends State<MyApp> {
height: 200,
),
Center(
child: CoolDropdown(
dropdownList: pokemonsMap,
child: CoolDropdown<String>(
dropdownList: pokemonsDropdownList,
dropdownItemPadding: EdgeInsets.zero,
onChange: (dropdownItem) {},
resultHeight: 50,
Expand Down
12 changes: 8 additions & 4 deletions example/lib/other_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:cool_dropdown/cool_dropdown.dart';
import 'package:cool_dropdown/drop_down_params.dart';
import 'package:flutter/material.dart';

class OtherScreen extends StatefulWidget {
const OtherScreen({Key key}) : super(key: key);
Expand All @@ -14,9 +15,12 @@ class _OtherScreenState extends State<OtherScreen> {
return Scaffold(
body: Center(
child: Container(
child: CoolDropdown(dropdownList: [
{'label': 'apple', 'value': 'apple'}
], onChange: (_) {}),
child: CoolDropdown<String>(dropdownList: [
DropDownParams(
label: 'Apple',
value: 'apple',
),
], onChange: (_) => print(_.value)),
),
),
);
Expand Down
132 changes: 63 additions & 69 deletions lib/cool_dropdown.dart
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
library cool_dropdown;

import 'package:flutter/material.dart';
import 'package:cool_dropdown/drop_down_body.dart';
import 'package:cool_dropdown/drop_down_params.dart';
import 'package:cool_dropdown/utils/animation_util.dart';
import 'package:cool_dropdown/utils/extension_util.dart';
import 'package:cool_dropdown/drop_down_body.dart';
import 'package:flutter/material.dart';

class CoolDropdown extends StatefulWidget {
List dropdownList;
Function onChange;
Function? onOpen;
String placeholder;
late Map defaultValue;
bool isTriangle;
bool isAnimation;
bool isResultIconLabel;
bool isResultLabel;
bool isDropdownLabel; // late
bool resultIconRotation;
late Widget resultIcon;
double resultIconRotationValue;
class CoolDropdown<ValueType> extends StatefulWidget {
final List<DropDownParams<ValueType>> dropdownList;
final void Function(DropDownParams<ValueType>) onChange;
final void Function(bool)? onOpen;
final String placeholder;
final DropDownParams<ValueType>? defaultValue;
final bool isTriangle;
final bool isAnimation;
final bool isResultIconLabel;
final bool isResultLabel;
final bool isDropdownLabel; // late
final bool resultIconRotation;
late final Widget resultIcon;
final double resultIconRotationValue;

// size
double resultWidth;
double resultHeight;
double? dropdownWidth; // late
double dropdownHeight; // late
double dropdownItemHeight;
double triangleWidth;
double triangleHeight;
double iconSize;
final double resultWidth;
final double resultHeight;
final double? dropdownWidth; // late
final double dropdownHeight; // late
final double dropdownItemHeight;
final double triangleWidth;
final double triangleHeight;
final double iconSize;

// align
Alignment resultAlign;
String dropdownAlign; // late
Alignment dropdownItemAlign;
String triangleAlign;
double triangleLeft;
bool dropdownItemReverse;
bool resultReverse;
MainAxisAlignment resultMainAxis;
MainAxisAlignment dropdownItemMainAxis;
final Alignment resultAlign;
final String dropdownAlign; // late
final Alignment dropdownItemAlign;
final String triangleAlign;
final double triangleLeft;
final bool dropdownItemReverse;
final bool resultReverse;
final MainAxisAlignment resultMainAxis;
final MainAxisAlignment dropdownItemMainAxis;

// padding
EdgeInsets resultPadding;
EdgeInsets dropdownItemPadding;
EdgeInsets dropdownPadding; // late
EdgeInsets selectedItemPadding;
final EdgeInsets resultPadding;
final EdgeInsets dropdownItemPadding;
final EdgeInsets dropdownPadding; // late
final EdgeInsets selectedItemPadding;

// style
late BoxDecoration resultBD;
late BoxDecoration dropdownBD; // late
late BoxDecoration selectedItemBD;
late TextStyle selectedItemTS;
late TextStyle unselectedItemTS;
late TextStyle resultTS;
late TextStyle placeholderTS;
late final BoxDecoration resultBD;
late final BoxDecoration dropdownBD; // late
late final BoxDecoration selectedItemBD;
late final TextStyle selectedItemTS;
late final TextStyle unselectedItemTS;
late final TextStyle resultTS;
late final TextStyle placeholderTS;

// gap
double gap;
double labelIconGap;
double dropdownItemGap;
double dropdownItemTopGap;
double dropdownItemBottomGap;
double resultIconLeftGap;
final double gap;
final double labelIconGap;
final double dropdownItemGap;
final double dropdownItemTopGap;
final double dropdownItemBottomGap;
final double resultIconLeftGap;

CoolDropdown({
required this.dropdownList,
Expand Down Expand Up @@ -111,23 +112,16 @@ class CoolDropdown extends StatefulWidget {
this.resultIconRotationValue = 0.5,
this.isDropdownLabel = true,
this.iconSize = 10,
defaultValue,
this.defaultValue,
}) {
// 기본값 셋팅
if (defaultValue != null) {
print('.. $defaultValue');
this.defaultValue = defaultValue;
} else {
this.defaultValue = {};
}
// label unique 체크
for (var i = 0; i < dropdownList.length; i++) {
if (dropdownList[i]['label'] == null) {
if (dropdownList[i].label.isEmpty) {
throw '"label" must be initialized.';
}
for (var j = 0; j < dropdownList.length; j++) {
if (i != j) {
if (dropdownList[i]['label'] == dropdownList[j]['label']) {
if (dropdownList[i].label == dropdownList[j].label) {
throw 'label is duplicated. Labels have to be unique.';
}
}
Expand Down Expand Up @@ -195,16 +189,16 @@ class CoolDropdown extends StatefulWidget {
}

@override
_CoolDropdownState createState() => _CoolDropdownState();
_CoolDropdownState createState() => _CoolDropdownState<ValueType>();
}

class _CoolDropdownState extends State<CoolDropdown>
class _CoolDropdownState<ValueType> extends State<CoolDropdown<ValueType>>
with TickerProviderStateMixin {
GlobalKey<DropdownBodyState> dropdownBodyChild = GlobalKey();
GlobalKey inputKey = GlobalKey();
Offset triangleOffset = Offset(0, 0);
late OverlayEntry _overlayEntry;
late Map selectedItem;
DropDownParams<ValueType>? selectedItem;
late AnimationController rotationController;
late AnimationController sizeController;
late Animation<double> textWidth;
Expand Down Expand Up @@ -232,7 +226,7 @@ class _CoolDropdownState extends State<CoolDropdown>

OverlayEntry _createOverlayEntry() {
return OverlayEntry(
builder: (BuildContext context) => DropdownBody(
builder: (BuildContext context) => DropdownBody<ValueType>(
key: dropdownBodyChild,
inputKey: inputKey,
onChange: widget.onChange,
Expand Down Expand Up @@ -371,15 +365,15 @@ class _CoolDropdownState extends State<CoolDropdown>
axisAlignment: -1,
child: Row(
mainAxisAlignment: widget.resultMainAxis,
children: [
children: <Widget>[
if (widget.isResultLabel)
Flexible(
child: Container(
child: Text(
selectedItem['label'] ??
selectedItem?.label ??
widget.placeholder,
overflow: TextOverflow.ellipsis,
style: selectedItem['label'] != null
style: selectedItem?.label != null
? widget.resultTS
: widget.placeholderTS,
),
Expand All @@ -389,8 +383,8 @@ class _CoolDropdownState extends State<CoolDropdown>
SizedBox(
width: widget.labelIconGap,
),
if (selectedItem['icon'] != null)
selectedItem['icon'] as Widget,
if (selectedItem?.icon != null)
selectedItem!.icon!,
].isReverse(widget.dropdownItemReverse),
),
),
Expand Down
Loading