Time Picker in Flutter





import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyTimeTutorial(),
    title: "Time Picker Tutorial",
  ));
}

class MyTimeTutorial extends StatefulWidget {
  @override
  _MyFieldTutorialState createState() => _MyFieldTutorialState();
}

class _MyFieldTutorialState extends State<MyTimeTutorial> {
  String selectedtime = "Select Time";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Time Picker Tutorial"),
      ),
      body: Container(
        child: Column(
          children: [
            ListTile(
              title: Text("$selectedtime"),
              trailing: IconButton(
                icon: Icon(
                  Icons.timer,
                  color: Colors.red,
                ),
                onPressed: () {
                  getshowTimePicker().then((value) {
                    if (value != null) {
                      setState(() {
                        selectedtime=value.format(context);
                      });
                    }
                  });
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future getshowTimePicker() async {
    var selected = await showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
    );
    return selected;
  }
}

No comments: