Skip to content

Instantly share code, notes, and snippets.

@danielschmitz
Created April 25, 2021 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielschmitz/5d3ac38bd4f598c48be66a256671daeb to your computer and use it in GitHub Desktop.
Save danielschmitz/5d3ac38bd4f598c48be66a256671daeb to your computer and use it in GitHub Desktop.
Flutter Example - Gerenciando estado da app - Alterando tema para dark
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