How to show different page to free users and different page to paid users

604 Views Asked by At

i am using revenuecat to manage a in-app subscription program. For free users, i want to show a different home page and different home page for premium users. This is my bottom navigation, please help.

So overall, user will sign up using email, navigate to a page where they will make payment and if payment is successfull, we will show them a different page and maintain the status.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:purchases_flutter/offerings_wrapper.dart';
import 'package:purchases_flutter/purchases_flutter.dart';


import 'home.dart';
import 'homefree.dart';


class bottomNavi extends StatefulWidget {
bottomNavi({Key? key}) : super(key: key);


@override
_bottomNaviState createState() => _bottomNaviState();
}

class _bottomNaviState extends State<bottomNavi> {


 int _selectedIndex = 0;

 static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: 
FontWeight.bold);
 final _pages = <Widget>[

 
home() or homeFree(), // based on entitlement status/value
list(),//this is a stateful widget on a separate file
 account(),//this is a stateful widget on a separate file


 ];

  void _onItemTapped(int index) {
  setState(() {
  _selectedIndex = index;
   });
}
@override
Widget build(BuildContext context) {

return Scaffold(

  body: Center(
    child: _pages.elementAt(_selectedIndex),
  ),
  bottomNavigationBar: BottomNavigationBar(
    items: const <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        label: 'home',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.description),
        label: 'todo',

      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.account_box),
        label: 'Account',
      ),
    ],
    currentIndex: _selectedIndex,
    selectedItemColor: Colors.amber[800],
    onTap: _onItemTapped,
    ),
   );
   }
   }

  class RevenueCatProvider extends ChangeNotifier{
  RevenueCatProvider() {
  init();
  }

  Entitlement _entitlement = Entitlement.free;
  Entitlement get entitlement => _entitlement;

  Future init() async {
  Purchases.addPurchaserInfoUpdateListener((purchaserInfo) async {
  updatePurchaseStatus();
  });

  }

 Future updatePurchaseStatus() async {
 final purchaseInfo = await Purchases.getPurchaserInfo();
 final entitments = purchaseInfo.entitlements.active.values.toList();

 _entitlement = entitments.isEmpty ? Entitlement.free : Entitlement.all_charts;

 notifyListeners();


}
}
1

There are 1 best solutions below

2
On

I have two possible solutions:

  1. If you use a own backend server or firestore (Firebase)

You should add an a boolean attribute to user called something like "premium", and use this value to show a premium or free view.

  1. If you don't use a backend server.

You can use the purchases_flutter package, and use a function to the verify if a current user is premium, and save this value in the local storage, and verify this each time that you open the app.

I recommended the first way.