How To Add Duration Picker In Flutter Using Android App

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

A little widget for picking Duration. Heavily inspired from the Material Design time picker widget.

Learn How To Add Duration Picker In Flutter Android App

Step 1
We cannot directly remove the time stamp from Durationpicker() but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code .

dependencies:
  flutter:
    sdk: flutter
  flutter_duration_picker: ^1.0.4

Step 2
After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command. 
flutter pub get

Step 3
 Open your project’s main.dart file and import material.dart and flutter_duration_picker.dart package.
import 'package:flutter_duration_picker/flutter_duration_picker.dart';


Step 4
You Can Add  Duration Picker Code:
floatingActionButton: Builder(
    builder: (BuildContext context) => new FloatingActionButton(
      onPressed: () async {
        Duration resultingDuration = await showDurationPicker(
          context: context,
          initialTime: new Duration(minutes: 30),
        );
        Scaffold.of(context).showSnackBar(new SnackBar(
            content: new Text("Chose duration: $resultingDuration")));
      },
      tooltip: 'Popup Duration Picker',
      child: new Icon(Icons.add),
    ))


Complete Code For Duration Picker In Flutter:
main.dart
import 'package:flutter/material.dart';
import 'package:flutter_duration_picker/flutter_duration_picker.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Duration Picker',
      theme: new ThemeData(
        primarySwatch: Colors.amber,
      ),
      home: new MyHomePage(title: 'Duration Picker Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Duration _duration = Duration(hours: 0, minutes: 0);
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        centerTitle: true,
        title: new Text('Duration Picker'),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Expanded(
                child: DurationPicker(
                  duration: _duration,
                  onChange: (val) {
                    this.setState(() => _duration = val);
                  },
                  snapToMins: 5.0,
                ))
          ],
        ),
      ),
      floatingActionButton: Builder(
          builder: (BuildContext context) => new FloatingActionButton(
            onPressed: () async {
              Duration resultingDuration = await showDurationPicker(
                context: context,
                initialTime: new Duration(minutes: 30),
              );
              Scaffold.of(context).showSnackBar(new SnackBar(
                  content: new Text("Chose duration: $resultingDuration")));
            },
            tooltip: 'Popup Duration Picker',
            child: new Icon(Icons.add),
          )),
    );
  }
}

Related Post