Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
관리 메뉴

nomad-programmer

[Programming/Flutter] TimePicker 위젯 본문

Programming/Flutter

[Programming/Flutter] TimePicker 위젯

scii 2020. 10. 8. 20:48

TimePicker 위젯을 시간을 선택할 때 사용하는 위젯이다.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _selectedTime;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(title: new Text('플러터 테스트')),
      body: Column(
        children: <Widget>[
          RaisedButton(
            child: Text('Time picker'),
            onPressed: (){
              Future<TimeOfDay> selectedTime = showTimePicker(
                context: context,
                initialTime: TimeOfDay.now(),
              );
              // 여기서 사용자가 시간을 선택할 때까지 코드가 블록됨.
              selectedTime.then((timeOfDay){
                setState(() {
                  _selectedTime = '${timeOfDay.hour}:${timeOfDay.minute}';
                });
              });
            },
          ),
          Text('$_selectedTime'),
        ],
      ),
    );
  }
}
Comments