[flutter] 숫자패드만들기

박망키·2022년 9월 6일
0

Flutter 야금야금 먹기

목록 보기
73/97
post-custom-banner
class SelectNumber extends StatefulWidget {
  SelectNumber({Key? key, this.initNumber = '0'}) : super(key: key);

  late String initNumber;
  @override
  State<SelectNumber> createState() => _SelectNumberState();
}

class _SelectNumberState extends State<SelectNumber> {
  late String SelectNumber;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    SelectNumber = widget.initNumber;
  }

  String getPadvalue(index) {
    if (index < 9) {
      return (index + 1).toString();
    } else if (index == 9) {
      return 'reset';
    } else if (index == 10) {
      return '0';
    } else {
      return '<';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
        child: GestureDetector(
      child: Text(SelectNumber),
      onTap: () {
        showModalBottomSheet(
            barrierColor: Colors.transparent,
            backgroundColor: Colors.transparent,
            context: context,
            builder: (_) {
              return Container(
                  padding: EdgeInsets.all(10),
                  decoration: BoxDecoration(
                      color: Colors.grey,
                      borderRadius: BorderRadius.circular(12)),
                  child: GridView.builder(
                    shrinkWrap: true,
                    itemCount: 12,
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3, //1 개의 행에 보여줄 item 개수
                      childAspectRatio: 1.5 / 1, //item 의 가로 1, 세로 1 의 비율
                      mainAxisSpacing: 10, //수평 Padding
                      crossAxisSpacing: 10, //수직 Padding
                    ),
                    itemBuilder: (BuildContext context, int index) {
                      // return Text(index.toString());
                      return InkWell(
                        onTap: () {
                          switch (index) {
                            case 9:
                              //reset
                              setState(() {
                                SelectNumber = '0';
                              });

                              print('${SelectNumber}');
                              return;
                            case 10:
                              if (SelectNumber == '0') {
                                return;
                              } else {
                                setState(() {
                                  SelectNumber = '${SelectNumber}0';
                                });
                              }
                              print('${SelectNumber}');
                              return;
                            case 11:
                              //remove
                              if (SelectNumber.length == 1) {
                                setState(() {
                                  SelectNumber = '0';
                                });
                              } else {
                                setState(() {
                                  SelectNumber = SelectNumber.substring(
                                      0, SelectNumber.length - 1);
                                });
                              }

                              print('${SelectNumber}');
                              return;

                            default:
                              if (SelectNumber == '0') {
                                setState(() {
                                  SelectNumber = '${index + 1}';
                                });
                              } else {
                                setState(() {
                                  SelectNumber = '$SelectNumber${index + 1}';
                                });
                              }
                              print('$SelectNumber');
                              return;
                          }
                        },
                        child: Ink(
                            child: Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(4)),
                          child: Center(
                              child: Text(getPadvalue(index),
                                  style: TextStyle(
                                      color: Colors.black54,
                                      fontSize: 30,
                                      fontWeight: FontWeight.bold))),
                        )),
                      );
                    },
                  ));
            });
      },
    ));
  }
}

int형 숫자키패드.
double형은 .추가하면 될거같은데 자리수 제한하는건 고민해봐야겠다

profile
무럭무럭 자라는 망키
post-custom-banner

0개의 댓글