navigate to other pages in flutter code example

Example 1: go to another page flutter

onPressed: () {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => SecondRoute()),
  );
}

Example 2: navigate between pages flutter

class Categories extends StatelessWidget {
  void showCategoryProductPage(BuildContext ctx) {
     Navigator.of(ctx).push(MaterialPageRoute(builder: (_) {
      return CategoryProducts();
     }));
  }
  
  
  Widget build(BuildContext context) {
	return Scaffold(
      body: GridView(
        children: [
          InkWell(
            onTap: () => showCategoryProductPage(context),
            child: Container(
              child: Text(
                'Fist Category',
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class CategoryProducts extends StatelessWidget {
  
  Widget build(BuildContext context) {
	 return Scaffold(
      appBar: AppBar(
        title: const Text('Meal App'),
      ),
      body: Text('Category Product Page'),
    );
  }
}

Tags:

Dart Example