简体   繁体   中英

How pass the date pick by user (via showDatePicker) to new page in flutter without navigator

Tried to use Riverpod didn't work as I don't want to change the UI but use this variable on the second page as DOC id. I don't want to call this function again otherwise I define a custom widget for it. Just need to access the date the user picks on the second page.WITHOUT a navigator as still there are actions that need to be done on the first page.

On a page user picks the date:

 final initalDate = DateTime.now();
    final newDate = await showDatePicker(
        context: context,
        initialDate: initalDate,
        firstDate: DateTime(DateTime.now().year + 0),
        lastDate: DateTime.now());
    if (newDate == null) return;

As this is defined as Doc id in firestore, how can I access this on the second page without navigation?

class SugarPhotoPage extends StatefulWidget {
  const SugarPhotoPage({
    super.key,
  });

  @override
  State<SugarPhotoPage> createState() => _SugarPhotoPageState();
}

class _SugarPhotoPageState extends State<SugarPhotoPage> {
  final Stream<QuerySnapshot> _photoStream = FirebaseFirestore.instance
      .collection('photos')
      .doc(DateFormat('yyyy-MM-dd').format(newDate))
      .collection('Today Photos')
      .snapshots();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      body: StreamBuilder<QuerySnapshot>(
          stream: _photoStream,
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return const Text('Something went wrong');
            }

            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Text("Loading");
            }

            if (snapshot.hasData) {
              return SafeArea(
                child: Center(
                  child: ListView(
                    children: snapshot.data!.docs
                        .map((DocumentSnapshot documentSnapshot) {
                      Map<String, dynamic> data =
                          documentSnapshot.data()! as Map<String, dynamic>;
                      return ListTile(
                        leading: Container(
                          height: 100,
                          width: 100,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: NetworkImage('${data['ImgUrl']}'),
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                      );
                    }).toList(),
                  ),
                ),
              );
            }
            return const Text('Loading');
          }),
    );
  }
}

在此处输入图像描述

Tried to use Riverpod didn't work as I don't want to change the UI but use this variable IN second page as DOC id.

If you don't want to share data using the navigator then you can create a single data source that shares the data between screens.

You can create a singleton class that contains a static variable that can be accessed throughout the app.

1 -> create a singleton class

class AppData {
  AppData._();

  static DateTime? userSelectedData;

}

2 -> update date on user selection

final initalDate = DateTime.now();
final newDate = await showDatePicker(
    context: context,
    initialDate: initalDate,
    firstDate: DateTime(DateTime.now().year + 0),
    lastDate: DateTime.now());
if (newDate == null) {return;}
else{
AppData.userSelectedData = newDate;
}

3-> Now you can access the date wherever you want without context by calling AppData.userSelectedData

if you want to save user selection after app is closed you can introduce Shared Preferences as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM