Flutter code written after CupertinoDatePicker is executed before it.

290 views
Skip to first unread message

flutter testing

unread,
Sep 1, 2019, 11:43:50 AM9/1/19
to Flutter Development (flutter-dev)

Code written after CupertinoDatePicker is executed before it.. I'm using CupertinoDatePicker, and facing this issue. Below code is an example that I have created to ask the question and find the solution from flutter experts. Any help to solve the issue will be appreciated.

Expected Result:
I/flutter (13818): 1. Date Before : 2019-08-29 00:00:00.000 
I/flutter (13818): 2. on Date Time Changed : 2019-08-30 00:00:00.000
I/flutter (13818): 3. Date After : 2019-08-30 00:00:00.000 
// go to next page

Actual Result:
1. Date Before : 2019-08-29  [output of print statement]
3. Date After : 2019-08-29 [output of print statement]
// goes to next page, After closing the page, it comes back to first page and displays date picker
2. print 'on Date Time Changed' : 2019-08-30 00:00:00.000
2. print  'on Date Time Changed' : 2019-08-31 00:00:00.000

Code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

DateTime selectedDate = DateTime.now();

void main() => runApp(MyApp()); // Run class MyApp

// Context is where the widget is located in the app
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Cupertinio Datepicker'),
        actions: <Widget>[
          IconButton(
            icon: Icon(CupertinoIcons.add),
            tooltip: 'Add Date',
            onPressed: () {
              print('1. Date Before : ${selectedDate} ');
              setState(() {
                _showModalBottomSheet(context);
              });
              print('3. Date After : ${selectedDate} ');
              // Problem: I have to call another page here with the changed date but date remains same and SecondPage is called before _showModalBottomSheet
              //navigateToSecondPage(context, selectedDate);


            },
          ),
        ],
      ),
    );
  }
}

Widget _showModalBottomSheet(context){
  showModalBottomSheet(
      context: context,
      builder: (BuildContext context){
        return Container(
          height: MediaQuery.of(context).copyWith().size.height / 3,
          child: _showCuperTinoDatePicker(),
        );
      }
  );
}


Widget _showCuperTinoDatePicker() {
  return CupertinoDatePicker(
    initialDateTime: selectedDate,
    onDateTimeChanged: (DateTime newdate) {
      selectedDate = newdate;
      print('2. onDateTimeChanged : ${selectedDate}' );
    },
    minimumYear: 2010,
    maximumYear: 2050,
    mode: CupertinoDatePickerMode.date,
  );
}


Rex Bloom

unread,
Sep 2, 2019, 9:17:47 AM9/2/19
to flutter testing, Flutter Development (flutter-dev)
Try this:

_showModalBottomSheet(context).then( (x) {
    print(“3”);
});

You do not need this inside a setState callback either. 

Rex

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/2295bdad-0a8f-4478-b72d-a364ab29eea1%40googlegroups.com.

flutter testing

unread,
Sep 2, 2019, 1:19:56 PM9/2/19
to Flutter Development (flutter-dev)
Thank You Rex. Adding then to the code is giving this error , 'Method then is not defined for the class widget'.
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

flutter testing

unread,
Sep 2, 2019, 2:03:44 PM9/2/19
to Flutter Development (flutter-dev)
After adding future, it started showing 'then'. I'm able to print the value in the correct sequence now. Thanks for your help.
Reply all
Reply to author
Forward
0 new messages