SHARE
TWEET

Untitled

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