본문 바로가기

study/flutter

플러터(flutter) BottomSheet에서 DropDown List 만들기

import 'dart:core';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'Dice.dart';

// void main(){
//   runApp(MyAPP());
// }
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Dice game',
      home: LogIn(),
    );
  }
}

class LogIn extends StatefulWidget {
  @override
  _LogInState createState() => _LogInState();
}

class _LogInState extends State<LogIn> {
  TextEditingController _VisitDataTimeEditingController =
      TextEditingController();

  int? _tempPickedTime;

  List<Text> _TexttempPickedTime = [
    Text('선택해주세요'),
    Text('24~01시 오전'),
    Text('01~02시 오전'),
    Text('02~03시 오전'),
    Text('03~04시 오전'),
    Text('04~05시 오전'),
    Text('05~06시 오전'),
    Text('06~07시 오전'),
    Text('07~08시 오전'),
    Text('08~09시 오전'),
    Text('09~10시 오전'),
    Text('10~11시 오전'),
    Text('11~12시 오전'),
    Text('12~13시 오후'),
    Text('13~14시 오후'),
    Text('14~15시 오후'),
    Text('15~16시 오후'),
    Text('16~17시 오후'),
    Text('17~18시 오후'),
    Text('18~19시 오후'),
    Text('19~20시 오후'),
    Text('20~21시 오후'),
    Text('21~22시 오후'),
    Text('22~23시 오후'),
    Text('23~24시 오후'),
    Text('23~24시 오후'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Log ins'),
        backgroundColor: Colors.redAccent,
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.menu),
          onPressed: () {},
        ),
        actions: [
          IconButton(
            onPressed: () {},
            icon: Icon(Icons.search_outlined),
          ),
        ],
      ),
      body: GestureDetector(
        onTap: () {
          HapticFeedback.mediumImpact();
          _VisitselectDate(_TexttempPickedTime, '시간');
        },
        child: Container(
          width: MediaQuery.of(context).size.width,
          padding: const EdgeInsets.only(right: 10, left: 10, top: 10),
          child: TextFormField(
            style: TextStyle(fontSize: 16),
            decoration: InputDecoration(
              contentPadding:
                  new EdgeInsets.symmetric(vertical: 10.0, horizontal: 10.0),
              isDense: true,
              enabled: false,
              hintText: "시간",
              enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.grey),
              ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.red),
              ),
            ),
            controller: _VisitDataTimeEditingController,
          ),
        ),
      ),
      // },
    );
  }

  _VisitselectDate(List<Text> Picked, String WantText) async {
    int? pickedDate = await showModalBottomSheet<int>(
      context: context,
      backgroundColor: ThemeData.light().scaffoldBackgroundColor,
      builder: (context) {
        return Container(
          height: 300,
          child: Column(
            children: <Widget>[
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    CupertinoButton(
                      child: Text('취소'),
                      onPressed: () {
                        Navigator.of(context).pop();
                        FocusScope.of(context).unfocus();
                      },
                    ),
                    CupertinoButton(
                      child: Text('완료'),
                      onPressed: () {
                        HapticFeedback.mediumImpact();
                        Navigator.of(context).pop(_tempPickedTime);
                        FocusScope.of(context).unfocus();
                      },
                    ),
                  ],
                ),
              ),
              Divider(
                height: 0,
                thickness: 1,
              ),
              Expanded(
                child: Container(
                  child: CupertinoPicker(
                    children: Picked,
                    backgroundColor: ThemeData.light().scaffoldBackgroundColor,
                    onSelectedItemChanged: (value) {
                      // setState(() {
                      //   _tempPickedTime = value;
                      // });
                      _tempPickedTime = value;
                    },
                    itemExtent: 25,
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
    print("${pickedDate}");

    if (pickedDate != null) {
      if (WantText == '시간') {
        if (Picked[_tempPickedTime!].data! == '선택해주세요') {
          _VisitDataTimeEditingController.clear();
          return;
        } else
          _VisitDataTimeEditingController.text = Picked[_tempPickedTime!].data!;
      } //시간
    }
  }
}

// void showSnackBar(BuildContext context) {
//   Scaffold.of(context).showSnackBar(snackbar)
// }