ayham95 / Custom-navigator

A flutter package that handles navigation deep in the widget tree
MIT License
80 stars 33 forks source link

CustomScaffold : How to use Navigator.of(context).pushNamed() ? #14

Closed zgramming closed 4 years ago

zgramming commented 4 years ago

previous , i want say thank's to make great plugin . I have problem navigate from Page-Testing1 to Detail-Page-Testing1 , I already read the documentation but not found about using pushNamed.
I get error like this after i press button to navigate from Page-Testing1 to Detail-Page-Testing1

Log Error


════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
Could not find a generator for route RouteSettings("/detail-page-testing1", null) in the _CustomNavigatorState.

Generators for routes are searched for in the following order:
 1. For the "/" route, the "home" property, if non-null, is used.
 2. Otherwise, the "routes" table is used, if it has an entry for the route.
 3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
 4. Finally if all else fails onUnknownRoute is called.
Unfortunately, onUnknownRoute was not set.
When the exception was thrown, this was the stack
#0      _CustomNavigatorState._onUnknownRoute.<anonymous closure> 
package:custom_navigator/custom_navigator.dart:158
#1      _CustomNavigatorState._onUnknownRoute 
package:custom_navigator/custom_navigator.dart:170
#2      NavigatorState._routeNamed 
package:flutter/…/widgets/navigator.dart:1644
#3      NavigatorState.pushNamed 
package:flutter/…/widgets/navigator.dart:1683
#4      PageTesting1.build.<anonymous closure> 
package:klenger_burger/screens/homescreen.dart:124
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#4f977
    debugOwner: GestureDetector
    state: possible
    won arena
    finalPosition: Offset(177.3, 238.7)
    finalLocalPosition: Offset(82.3, 18.0)
    button: 1
    sent tap down
════════════════════════════════════════════════════════════════════════════════

Custom


class HomeScreenCustom extends StatefulWidget {
  final List<BottomNavigationBarItem> itemMenuBottomNavigation;
  final List<Widget> screenBottomNavigation;
  final Function(int) onTapItemBottomNavigation;
  HomeScreenCustom({
    @required this.onTapItemBottomNavigation,
    @required this.screenBottomNavigation,
    @required this.itemMenuBottomNavigation,
  });
  @override
  _HomeScreenCustomState createState() => _HomeScreenCustomState();
}

class _HomeScreenCustomState extends State<HomeScreenCustom> {
  @override
  Widget build(BuildContext context) {
    return CustomScaffold(
      scaffold: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          items: widget.itemMenuBottomNavigation,
        ),
      ),

      // Children are the pages that will be shown by every click
      // They should placed in order such as
      // `page 0` will be presented when `item 0` in the [BottomNavigationBar] clicked.
      children: widget.screenBottomNavigation,

      // Called when one of the [items] is tapped.
      onItemTap: widget.onTapItemBottomNavigation,
    );
  }

  // final _items = [
  //   BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('home')),
  //   BottomNavigationBarItem(icon: Icon(Icons.event), title: Text('events')),
  //   BottomNavigationBarItem(
  //       icon: Icon(Icons.save_alt), title: Text('downloads')),
  // ];
}

HomeScreenCustom

 @override
  Widget build(BuildContext context) {
    return HomeScreenCustom(
      itemMenuBottomNavigation: [
        BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('home')),
        BottomNavigationBarItem(icon: Icon(Icons.event), title: Text('events')),
      ],
      screenBottomNavigation: <Widget>[
        PageTesting1(),
        PageTesting2(),
      ],
      onTapItemBottomNavigation: (index) {},
    );
  }
}

class PageTesting1 extends StatelessWidget {
  static const routeNamed = "/page-testing1";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: FlatButton(
          onPressed: () =>
              Navigator.of(context).pushNamed(DetailPageTesting1.routeNamed),
          child: Text('Detail Testing 1'),
        ),
      ),
    );
  }
}

class DetailPageTesting1 extends StatelessWidget {
  static const routeNamed = "/detail-page-testing1";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('Detail Testing 1'),
      ),
    );
  }
}

class PageTesting2 extends StatelessWidget {
  static const routeNamed = "/page-testing2";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: FlatButton(
          onPressed: () =>
              Navigator.of(context).pushNamed(DetailPageTesting2.routeNamed),
          child: Text('Detail Testing 2'),
        ),
      ),
    );
  }
}

class DetailPageTesting2 extends StatelessWidget {
  static const routeNamed = "/detail-page-testing2";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('Detail Testing 2'),
      ),
    );
  }
}

If i make mistake or i miss something in documentation, please correct me.

Thank's

ayham95 commented 4 years ago

ok.. I don't think that named routes are supported but i'll make sure to check and fix the problem

zgramming commented 4 years ago

thank's for your fast response , i hope you can adding supported pushNamed to give a different choise routed.

shyam1s15 commented 4 years ago

previous , i want say thank's to make great plugin . I have problem navigate from Page-Testing1 to Detail-Page-Testing1 , I already read the documentation but not found about using pushNamed.

I get error like this after i press button to navigate from Page-Testing1 to Detail-Page-Testing1

Log Error


════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
Could not find a generator for route RouteSettings("/detail-page-testing1", null) in the _CustomNavigatorState.

Generators for routes are searched for in the following order:
 1. For the "/" route, the "home" property, if non-null, is used.
 2. Otherwise, the "routes" table is used, if it has an entry for the route.
 3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
 4. Finally if all else fails onUnknownRoute is called.
Unfortunately, onUnknownRoute was not set.
When the exception was thrown, this was the stack
#0      _CustomNavigatorState._onUnknownRoute.<anonymous closure> 
package:custom_navigator/custom_navigator.dart:158
#1      _CustomNavigatorState._onUnknownRoute 
package:custom_navigator/custom_navigator.dart:170
#2      NavigatorState._routeNamed 
package:flutter/…/widgets/navigator.dart:1644
#3      NavigatorState.pushNamed 
package:flutter/…/widgets/navigator.dart:1683
#4      PageTesting1.build.<anonymous closure> 
package:klenger_burger/screens/homescreen.dart:124
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#4f977
    debugOwner: GestureDetector
    state: possible
    won arena
    finalPosition: Offset(177.3, 238.7)
    finalLocalPosition: Offset(82.3, 18.0)
    button: 1
    sent tap down
════════════════════════════════════════════════════════════════════════════════

Custom


class HomeScreenCustom extends StatefulWidget {
  final List<BottomNavigationBarItem> itemMenuBottomNavigation;
  final List<Widget> screenBottomNavigation;
  final Function(int) onTapItemBottomNavigation;
  HomeScreenCustom({
    @required this.onTapItemBottomNavigation,
    @required this.screenBottomNavigation,
    @required this.itemMenuBottomNavigation,
  });
  @override
  _HomeScreenCustomState createState() => _HomeScreenCustomState();
}

class _HomeScreenCustomState extends State<HomeScreenCustom> {
  @override
  Widget build(BuildContext context) {
    return CustomScaffold(
      scaffold: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          items: widget.itemMenuBottomNavigation,
        ),
      ),

      // Children are the pages that will be shown by every click
      // They should placed in order such as
      // `page 0` will be presented when `item 0` in the [BottomNavigationBar] clicked.
      children: widget.screenBottomNavigation,

      // Called when one of the [items] is tapped.
      onItemTap: widget.onTapItemBottomNavigation,
    );
  }

  // final _items = [
  //   BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('home')),
  //   BottomNavigationBarItem(icon: Icon(Icons.event), title: Text('events')),
  //   BottomNavigationBarItem(
  //       icon: Icon(Icons.save_alt), title: Text('downloads')),
  // ];
}

HomeScreenCustom

 @override
  Widget build(BuildContext context) {
    return HomeScreenCustom(
      itemMenuBottomNavigation: [
        BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('home')),
        BottomNavigationBarItem(icon: Icon(Icons.event), title: Text('events')),
      ],
      screenBottomNavigation: <Widget>[
        PageTesting1(),
        PageTesting2(),
      ],
      onTapItemBottomNavigation: (index) {},
    );
  }
}

class PageTesting1 extends StatelessWidget {
  static const routeNamed = "/page-testing1";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: FlatButton(
          onPressed: () =>
              Navigator.of(context).pushNamed(DetailPageTesting1.routeNamed),
          child: Text('Detail Testing 1'),
        ),
      ),
    );
  }
}

class DetailPageTesting1 extends StatelessWidget {
  static const routeNamed = "/detail-page-testing1";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('Detail Testing 1'),
      ),
    );
  }
}

class PageTesting2 extends StatelessWidget {
  static const routeNamed = "/page-testing2";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: FlatButton(
          onPressed: () =>
              Navigator.of(context).pushNamed(DetailPageTesting2.routeNamed),
          child: Text('Detail Testing 2'),
        ),
      ),
    );
  }
}

class DetailPageTesting2 extends StatelessWidget {
  static const routeNamed = "/detail-page-testing2";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('Detail Testing 2'),
      ),
    );
  }
}

If i make mistake or i miss something in documentation, please correct me.

Thank's

Oh Hey there, may be i have found a quick fix for your solution which i have used in my app

try passing context your screen

example:

Home(context, some_other_data)

Store it in your Home

Home(this._saveContext) : assert( _saveContext!=null )

Use it for your next screen

Navigator.of(widget._saveContext).pushName('secondScreen')

please correct me if there is an issue