SHARE
TWEET

Untitled

a guest Jan 21st, 2020 70 in 331 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3.  
  4. void main() => runApp(MyApp());
  5.  
  6. class MyApp extends StatelessWidget {
  7.   @override
  8.   Widget build(BuildContext context) {
  9.     return MaterialApp(
  10.       title: 'Flutter Demo',
  11.       theme: ThemeData(
  12.         primarySwatch: Colors.blue,
  13.       ),
  14.       home: ChangeNotifierProvider<UserModel>(
  15.         create: (_) => UserModel(),
  16.         child: Consumer<UserModel>(
  17.           builder: (buildContext, model, _) {
  18.             if (model.username == null) {
  19.               return LoginPage();
  20.             } else {
  21.               return HomePage();
  22.             }
  23.           },
  24.         ),
  25.       ),
  26.     );
  27.   }
  28. }
  29.  
  30. class HomePage extends StatelessWidget {
  31.   @override
  32.   Widget build(BuildContext context) {
  33.     final provider = Provider.of<UserModel>(context, listen: false);
  34.  
  35.     return Scaffold(
  36.       appBar: AppBar(
  37.         title: Text('Homepage'),
  38.       ),
  39.       body: Container(
  40.         child: Center(
  41.           child: Column(
  42.             children: <Widget>[
  43.               SizedBox(
  44.                 height: 32.0,
  45.               ),
  46.               Text('Logged in as ' + provider.username),
  47.               FlatButton(
  48.                 color: Color(0xFFDDDDDD),
  49.                 child: Text('Logout'),
  50.                 onPressed: () {
  51.                   provider.update(null);
  52.                 },
  53.               ),
  54.             ],
  55.           ),
  56.         ),
  57.       ),
  58.     );
  59.   }
  60. }
  61.  
  62. class LoginPage extends StatelessWidget {
  63.   @override
  64.   Widget build(BuildContext context) {
  65.     final provider = Provider.of<UserModel>(context, listen: false);
  66.  
  67.     return Scaffold(
  68.       appBar: AppBar(
  69.         title: Text('Login'),
  70.       ),
  71.       body: Center(
  72.         child: Column(
  73.           children: <Widget>[
  74.             SizedBox(
  75.               height: 32.0,
  76.             ),
  77.             Text('Please login'),
  78.             FlatButton(
  79.               color: Color(0xFFDDDDDD),
  80.               child: Text('Login'),
  81.               onPressed: () {
  82.                 provider.update('newUser');
  83.               },
  84.             ),
  85.           ],
  86.         ),
  87.       ),
  88.     );
  89.   }
  90. }
  91.  
  92. class UserModel extends ChangeNotifier {
  93.   String username;
  94.  
  95.   void update(String newValue) {
  96.     username = newValue;
  97.  
  98.     notifyListeners();
  99.   }
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top