Last active
July 30, 2019 19:49
-
-
Save filiph/0d1c4c7689a52cdf6d926b24aa241893 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:provider/provider.dart'; | |
| void main() => runApp( | |
| ChangeNotifierProvider<AppStateManager>.value( | |
| value: AppStateManager(), | |
| child: MyApp(), | |
| ), | |
| ); | |
| class AppStateManager extends ChangeNotifier { | |
| int _currentStudentIndex = 0; | |
| int get currentStudentIndex => _currentStudentIndex; | |
| set currentStudentIndex(int index) { | |
| _currentStudentIndex = index; | |
| print('new current student: $index'); | |
| notifyListeners(); | |
| } | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: MyHomePage(title: 'Flutter Demo Home Page'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| MyHomePage({Key key, this.title}) : super(key: key); | |
| final String title; | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class Student { | |
| final String name; | |
| Student.fromString(this.name); | |
| } | |
| Future<List<String>> getStudents() => Future.value(['John', 'Jane', 'Filip']); | |
| class _MyHomePageState extends State<MyHomePage> { | |
| _MyHomePageState() { | |
| print('constructing homepage state'); | |
| getCurrentStudent().then((student) => setState(() { | |
| print('setting current student to ${student.name}'); | |
| _currentStudent = student; | |
| })); | |
| } | |
| Future<Student> getCurrentStudent() async { | |
| List<String> students = await getStudents(); | |
| final AppStateManager stateManager = Provider.of<AppStateManager>(context); | |
| String currentStudent = students[stateManager.currentStudentIndex ?? 0]; | |
| return Student.fromString(currentStudent); | |
| } | |
| Student _currentStudent; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| body: Center( | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| Text( | |
| 'Student: ${_currentStudent?.name}', | |
| ), | |
| ], | |
| ), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () => Navigator.push<void>( | |
| context, | |
| MaterialPageRoute( | |
| builder: (context) => Scaffold( | |
| body: Center( | |
| child: MaterialButton( | |
| onPressed: () { | |
| Provider.of<AppStateManager>(context) | |
| .currentStudentIndex = 2; | |
| Navigator.pop(context); | |
| }, | |
| child: const Text('hi'), | |
| ), | |
| ), | |
| ))), | |
| tooltip: 'Increment', | |
| child: Icon(Icons.add), | |
| ), | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:provider/provider.dart'; | |
| void main() => runApp( | |
| ChangeNotifierProvider<AppStateManager>.value( | |
| value: AppStateManager(), | |
| child: MyApp(), | |
| ), | |
| ); | |
| class AppStateManager extends ChangeNotifier { | |
| int _currentStudentIndex = 0; | |
| int get currentStudentIndex => _currentStudentIndex; | |
| set currentStudentIndex(int index) { | |
| _currentStudentIndex = index; | |
| print('new current student: $index'); | |
| notifyListeners(); | |
| } | |
| } | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: MyHomePage(title: 'Flutter Demo Home Page'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| MyHomePage({Key key, this.title}) : super(key: key); | |
| final String title; | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class Student { | |
| final String name; | |
| Student.fromString(this.name); | |
| } | |
| Future<List<String>> getStudents() => Future.value(['John', 'Jane', 'Filip']); | |
| class _MyHomePageState extends State<MyHomePage> { | |
| Future<Student> getCurrentStudent(int index) async { | |
| List<String> students = await getStudents(); | |
| final AppStateManager stateManager = Provider.of<AppStateManager>(context); | |
| String currentStudent = students[stateManager.currentStudentIndex ?? 0]; | |
| return Student.fromString(currentStudent); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| body: Center( | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| Consumer<AppStateManager>( | |
| builder: (context, manager, child) => FutureBuilder<Student>( | |
| future: getCurrentStudent(manager.currentStudentIndex), | |
| builder: (context, snapshot) { | |
| if (!snapshot.hasData) { | |
| return const CircularProgressIndicator(); | |
| } | |
| return Text('Student: ${snapshot.data.name}'); | |
| }, | |
| ), | |
| ), | |
| ], | |
| ), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () => Navigator.push<void>( | |
| context, | |
| MaterialPageRoute( | |
| builder: (context) => Scaffold( | |
| body: Center( | |
| child: MaterialButton( | |
| onPressed: () { | |
| Provider.of<AppStateManager>(context) | |
| .currentStudentIndex = 2; | |
| Navigator.pop(context); | |
| }, | |
| child: const Text('hi'), | |
| ), | |
| ), | |
| ))), | |
| tooltip: 'Increment', | |
| child: Icon(Icons.add), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment