Open In App

Flutter – Creating Time Picker Using Material Design

Time Picker is a UI component that controls or lets the selection of time in either 24-hours format or AM/PM format. Mainly is job is to ease out the process of time selection and to ensure that the user is picking or selecting a valid time in the application. This can be used in the applications such as alarm, stop-watch or in any other scenario where time selection is required.  In native android time picker is available in two modes, the first one is the clock, where the user can select the time from the clock itself first selecting the house and then the minutes. And another mode is the spinner mode where the user can manually type in the time or scroll through the spinner. Flutter makes it very easy to implement both of these as it out of the box gives us both modes without any additional efforts. So in this article, we are going to see how to implement a time picker in flutter.

We will be using VS Code IDE for this project, you can also use Android Studio or any other IDE. The only package that will be used in this app is material.dart.  Some of the concepts that will be used while developing this app are listed below:



Step 1: The first step is going to be simple.




import 'package:flutter/material.dart';
  
// Main function which starts the app
void main() {
  runApp(MyApp());
}

Step 2:  In this step, we have performed three main things.






class MyApp extends StatelessWidget {
    
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          
        // This is the theme of your application.
        primarySwatch: Colors.green,
      ),
      home: MyHomePage(title: 'GeeksForGeeks'),
    );
  }
}

Step 3: In step three we have the following things:




class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  
  // This widget is the home of this app which is Stateful
  final String title;
  
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

Step 4: In this step, we build the logic for the time selection.




class _MyHomePageState extends State<MyHomePage> {
  TimeOfDay _time = TimeOfDay.now();
  TimeOfDay picked;
  
  Future<Null> selectTime(BuildContext context) async {
    picked = await showTimePicker(
      context: context,
      initialTime: _time,
    );
    setState(() {
      _time = picked;
  
      print(picked);
    });
  }
    
  ....

Step 5: In this step, we have finalized the app by building the UI for the logic, that was curated in the previous step.




....
    
  @override
  Widget build(BuildContext context) {
    
    // This method is rerun every time setState is called.    
    return Scaffold(
      appBar: AppBar(
          
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
          
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            IconButton(
              iconSize: 80,
              icon: Icon(
                Icons.alarm,
                size: 80,
              ),
              onPressed: () {
                selectTime(context);
              },
            ),
            SizedBox(
              height: 60,
            ),
            Text('$_time', style: TextStyle(fontSize: 40)),
          ],
        ),
      ), 
    );
  }
}

Complete Code:




import 'package:flutter/material.dart';
  
// Main function which starts the app
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
    
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
          
        // This is the theme of your application
        primarySwatch: Colors.green,
      ),
      home: MyHomePage(title: 'GeeksForGeeks'),
    );
  }
}
  
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  
  // This widget is the home page of your application.
  final String title;
  
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
  
class _MyHomePageState extends State<MyHomePage> {
  TimeOfDay _time = TimeOfDay.now();
  TimeOfDay picked;
  
  Future<Null> selectTime(BuildContext context) async {
    picked = await showTimePicker(
      context: context,
      initialTime: _time,
    );
    setState(() {
      _time = picked;
  
      print(picked);
    });
  }
  
  @override
  Widget build(BuildContext context) {
      
    // This method is rerun every time setState is called.
    return Scaffold(
      appBar: AppBar(
          
        // Here we take the value from the 
        // MyHomePage object that was created by
        // the App.build method, and use it
        // to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
          
        // Center is a layout widget. It takes
        // a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            IconButton(
              iconSize: 80,
              icon: Icon(
                Icons.alarm,
                size: 80,
              ),
              onPressed: () {
                selectTime(context);
              },
            ),
            SizedBox(
              height: 60,
            ),
            Text('$_time', style: TextStyle(fontSize: 40)),
          ],
        ), // Column
      ), // Center
    ); // Scaffold
  }
}

Output:


Article Tags :