Starting with our previous examples of using Provider for state management, I want to begin to simulate Logging and Logging out.
In this example, I provide conditional logic to the text shown on the screen depending on the value of foo.
I use a ternary operator on the Text widget built by the Consumer.
testCondition ? trueValue : falseValue
foo.value ? Text("Home") : Text("Please Sign In")
If the text condition is true then return the true value and if not true return the other value.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(ChangeNotifierProvider(
create: (_) => Foo(),
child: MyApp(),
));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//This is no longer needed with Consumer
//var value = Provider.of<Foo>(context).value;
//print(value);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Logged In or Logged Out'),
),
body: Column(
children: [
Center(
child: RaisedButton(
onPressed: () {
//Call the method _goodBye from Foo using provider
Provider.of<Foo>(context, listen: false)._goodBye();
},
child: Text('Login', style: TextStyle(fontSize: 20)),
)),
Center(
child: RaisedButton(
onPressed: () {
//Call the method _goodBye from Foo using provider
Provider.of<Foo>(context, listen: false)._reset();
//print(value);
},
child: Text('Logout', style: TextStyle(fontSize: 20)),
)
),
Consumer<Foo>(
builder: (_, foo, child) =>
//result = testCondition ? trueValue : falseValue
foo.value ? Text("Home") : Text("Please Sign In")
),
],
)),
);
}
}
class Foo extends ChangeNotifier {
//Initial private state of the value
var _value = false;
//Getter
bool get value {
return _value;
}
//Method to Update Value
_goodBye() {
_value = true;
notifyListeners();
}
//Method to Reset
_reset() {
_value = false;
notifyListeners();
}
}
- Log in to post comments