Search code examples
fluttersharedpreferences

How to enable /disable login page in flutter


Please help me Master.. I have an application which there is a secure login feature using a password. Every time the user opens the application, the user is redirected from MainPage to the LoginPage to enter the password.

I want to make a choice for the user to enable or disable secure login on the SettingsPage. If the user activates secure login, every time he opens the application the user redirected from MainPage to LoginPage. And if the user disabled secure login, the user is immediately redirected from MainPage to the HomePage

Example

if Secure Login Enable :

MainPage -> LoginPage -> HomePage

if Secure Login Disable :

MainPage -> HomePage

MainPage Code

// MainPage
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MainPage',
      home: LoginPage(),
    );
  }
}

LoginPage Code

// LoginPage
class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

HomePage Code

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

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
          child: InkWell(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Settings',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
          ],
        ),
        onTap: () {
          Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => SettingsPage()));
        },
      )),
    );
  }
}

SettingsPage Code

// SettingsPage
class SettingsPage extends StatefulWidget {
  @override
  _SettingsPageState createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Container(
          child: InkWell(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Secure Login enabled/disabled',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
          ],
        ),
        onTap: () {},
      )),
    );
  }
}

Solution

  • You could use Shared Preferences and do this.

    // Button function to enabled/disable
    SharedPreferences prefs = await SharedPreferences.getInstance();
    
    //  Check if it was disabled previously
    final bool disabled = await prefs.getBool('yourKey');
    
    // Set flag depending if it was enable/disabled
    prefs.setBool('yourKey', disabled == null? true : !disabled );  
    

    and before you navigate to LoginPage you should check if he disabled it.

    SharedPreferences prefs = await SharedPreferences.getInstance();
    final bool disabled = await prefs.getBool('yourKey');