Last active
July 26, 2020 06:13
-
-
Save manofi21/f34fc47aed859515212a52279bbf7cff to your computer and use it in GitHub Desktop.
This file contains 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
- provider: ^3.0.0+1 | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_grocery_shopping/application_color.dart'; | |
import 'package:geolocator/geolocator.dart'; | |
import 'package:provider/provider.dart'; | |
class ProvdersApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
// tambah widget provider // | |
return ChangeNotifierProvider<ApplicationColor>( | |
create: (context) => ApplicationColor(), | |
child: Scaffold( | |
appBar: AppBar( | |
backgroundColor: Colors.black, | |
// Pasang widget Consumer pada setiap widget yang akan berubah ketika ChangeNotifier ter trigger // | |
title: Consumer<ApplicationColor>( | |
builder: (context, applicationColor, _) => Text( | |
"Provider State Management", | |
style: TextStyle(color: applicationColor.color)), | |
), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Consumer<ApplicationColor>( | |
builder: (context, applicationColor, _) => AnimatedContainer( | |
margin: EdgeInsets.all(5), | |
width: 100, | |
height: 100, | |
color: applicationColor.color, | |
duration: Duration(milliseconds: 500), | |
), | |
), | |
// pasang juga pada swich widget yang akan melakukan perubahan nilai pada provider. | |
switch adalah widget yang hanya menerima nilai true false. | |
maka di kelas ApplicationColor nilai yang di deklarasi boolean// | |
Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Container(margin: EdgeInsets.all(5), child: Text("AB")), | |
Consumer<ApplicationColor>( | |
builder: (context, applicationColor, _) => | |
Switch(value: applicationColor.isLightBlue, onChanged: (newValue) { | |
applicationColor.isLightBlue = newValue; | |
})), | |
Container(margin: EdgeInsets.all(5), child: Text("LB")), | |
], | |
) | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
This file contains 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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
// tambah syntax "with ChangeNotifier" untuk dapat di masukkan ke widget provider | |
kelas ini yang akan menjadi pengganti setState // | |
class ApplicationColor with ChangeNotifier{ | |
// nilai default yang dideklarasi. | |
untuk menentukan nilai pada Switch nanti // | |
bool _isLightBlue = true; | |
// membuat get dan set // | |
bool get isLightBlue => _isLightBlue; | |
set isLightBlue(bool value){ | |
_isLightBlue = value; | |
notifyListeners(); | |
} | |
// get color/warna dengan if statement // | |
Color get color => (_isLightBlue) ? Colors.lightBlue : Colors.amber; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment