Created
April 25, 2021 13:52
-
-
Save danielschmitz/5d3ac38bd4f598c48be66a256671daeb to your computer and use it in GitHub Desktop.
Flutter Example - Gerenciando estado da app - Alterando tema para dark
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/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedBuilder( | |
builder: (context, child) { | |
return MaterialApp( | |
title: 'Welcome to Flutter', | |
theme: ThemeData( | |
primarySwatch: Colors.lightBlue, brightness: isDarkTheme()), | |
home: AppWidget()); | |
}, | |
animation: AppController.instance, | |
); | |
} | |
Brightness isDarkTheme() { | |
return AppController.instance.isDarkTheme | |
? Brightness.dark | |
: Brightness.light; | |
} | |
} | |
class AppWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Exemplo"), | |
), | |
body: Center( | |
child: Switch( | |
onChanged: (value) => {AppController.instance.changeTheme()}, | |
value: AppController.instance.isDarkTheme, | |
), | |
)); | |
} | |
} | |
class AppController extends ChangeNotifier { | |
static AppController instance = AppController(); | |
bool isDarkTheme = false; | |
changeTheme() { | |
isDarkTheme = !isDarkTheme; | |
notifyListeners(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment