1

I am using awesome_notification package for the notifications. Actually, I have a custom bottom navigation bar and I am using screen[selectedIndex] to change the content of the body inside the scaffold. I guess that is what causing all the problem. Please suggest me a way out.

Whenever I am trying to navigate from my notification I am getting this error.

The code:

// ignore_for_file: use_key_in_widget_constructors

import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:flutter/material.dart';
import 'package:notiminder/pages/done.dart';
import 'package:notiminder/pages/history.dart';
import 'package:notiminder/pages/reminder.dart';

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

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    AwesomeNotifications().isNotificationAllowed().then((isAllowed) {
      if (!isAllowed) {
        showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: const Text(
              'Allow Notifications',
            ),
            content: const Text(
              'Notiminder app would like to send you notifications',
            ),
            actions: [
              TextButton(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: const Text("Don't Allow"),
              ),
              TextButton(
                onPressed: () => AwesomeNotifications()
                    .requestPermissionToSendNotifications()
                    .then(
                      (_) => Navigator.pop(context),
                    ),
                child: const Text(
                  'Allow',
                  style: TextStyle(
                    fontStyle: FontStyle.italic,
                    color: Colors.teal,
                  ),
                ),
              ),
            ],
          ),
        );
      }
    });
    AwesomeNotifications().createdStream.listen((notification) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text('Reminder created for you'),
        ),
      );
    });
    AwesomeNotifications().actionStream.listen((event) {
      Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(
          builder: (_) => HomePage(),
        ),
        (route) => route.settings.name == screen[1].toString(),
        // (route) => route.isFirst,
      );
    });
  }

  int selectedIndex = 0;
  final screen = [
    ReminderPage(),
    DonePage(),
    HistoryPage(),
  ];

  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;
    return GestureDetector(
      onTap: () {
        final FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus) {
          currentFocus.unfocus();
        }
      },
      child: SafeArea(
        child: Scaffold(
          // resizeToAvoidBottomInset: false,
          appBar: AppBar(
            title: const Text('Notiminder'),
            centerTitle: true,
          ),
          body: screen[selectedIndex],
          bottomNavigationBar: Row(
            children: [
              SizedBox(
                width: size.width,
                height: 80,
                child: Stack(
                  children: [
                    CustomPaint(
                      size: Size(size.width, 80),
                      painter: BNBCustomPainter(),
                    ),
                    Center(
                      heightFactor: 0.6,
                      child: FloatingActionButton(
                        onPressed: () {
                          setState(() {
                            selectedIndex = 0;
                          });
                        },
                        child: const Icon(Icons.notifications, size: 30),
                      ),
                    ),
                    SizedBox(
                      width: size.width,
                      height: 80,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          buttomBarItems(
                            Icons.done_outline_rounded,
                            1,
                          ),
                          Container(
                            width: size.width * 0.20,
                          ),
                          buttomBarItems(
                            Icons.history_rounded,
                            2,
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

Specifically the notification route:

    AwesomeNotifications().actionStream.listen((event) {
      Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(
          builder: (_) => HomePage(),
        ),
        (route) => route.settings.name == screen[1].toString(),
        // (route) => route.isFirst,
      );
    });
  }

 
MNBWorld
  • 529
  • 1
  • 5
  • 25

0 Answers0