Home > Blockchain >  How to pass the value from other dart file?
How to pass the value from other dart file?

Time:07-27

I would like to pass the value from timePicker.dart to showTime.dart,and I had tried global variable but the TextFormField controller in showTime.dart only got the initalized value.

//global.dart 
String getTime='';

//timePicker.dart
  Dialog(
        child:TextField(
          controller: _conTime
          ..text = DateTimes.periodTime(time: _selectedTime),
           readOnly: true,
            onTap: () {
            DateTimes.timePicker(
            context: context,
            time: _selectedTime,
            dateTime: (time) {
              setState(() {
                selectedTime = time;
                getTime=_selectedTime;
               });
              });
             },
            )
        )
//showTime.dart
TextFormField(
     readOnly: true,
     controller: showTimeController,
     decoration:InputDecoration(
     suffixIcon: IconButton(
        icon: Icon(Icons.open_in_new_rounded),
        onPressed: () async{
            showDialog(
            anchorPoint:Offset.infinite, 
            context: context,
            builder: (_) => timePicker(getTime),
            );
       showTimeController.text=getTime;
       },
     ),
   ),
  ),
 )

    

   

CodePudding user response:

You can use event bus to pass value. https://pub.dev/packages/event_bus

CodePudding user response:

showTimePicker returns a nullable value, you can await and receive data from it.

showMyTimerPicker() async {
    final gotTime = await showTimePicker(
      context: context,
      initialTime: TimeOfDay(hour: 9, minute: 0),
    );
  }

You can find more about showTimePicker

  Future<TimeOfDay?> showMyTimerPicker() async {
    final gotTime = await showTimePicker(
      context: context,
      initialTime: TimeOfDay(hour: 9, minute: 0),
    );
    // if you need more here
    return gotTime;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(onPressed: () async {
        final data = await showMyTimerPicker();
        print(data);
      }),
    );
  }
  • Related