Flutter: How to use a value across different widget?

I am actually new to programming specifically to dart and flutter. So, I was stuck on a situation where I have created a widget and I need to use the value defined in that widget in another widget. Please kindly assist for the solutions.

import 'package:flutter/material.dart';
    
class DateTimePicker extends StatefulWidget {
  final String title;
  const DateTimePicker({Key key, this.title}) : super(key: key);
  @override
  _DateTimePickerState createState() => _DateTimePickerState();
}

class _DateTimePickerState extends State<DateTimePicker> {
  DateTime selectedDate;

  Future<Null> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2000),
        lastDate: DateTime(2050));
    if (picked != null) {
      setState(() {
        selectedDate = picked;
      });
    }
  }

  @override
  void initState() {
    selectedDate = DateTime.now();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(5.0),
        border: Border.all(color: Colors.grey[400]),
      ),
      child: ListTile(
        onTap: () => _selectDate(context),
        leading: Icon(Icons.calendar_today, color: Colors.grey[400]),
        title: Text(
            '${widget.title}\n${selectedDate.year}-${selectedDate.month}-${selectedDate.day}'),
      ),
    );
  }
}


Read more here: https://stackoverflow.com/questions/65002496/flutter-how-to-use-a-value-across-different-widget

Content Attribution

This content was originally published by Amrit Sharma at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: