flutter_dynamic_theme 1.0.1
flutter_dynamic_theme: ^1.0.1 copied to clipboard
Vous permet de spécifier un certain nombre de thèmes de couleurs parmi lesquels l'utilisateur peut choisir. Persiste le thème sélectionné lors des redémarrages de l'application.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_dynamic_theme/flutter_dynamic_theme.dart';
import 'package:flutter_dynamic_theme/theme_switcher_widgets.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FlutterDynamicTheme(
defaultBrightness: Brightness.light,
data: (Brightness brightness) => ThemeData(
primarySwatch: Colors.indigo,
brightness: brightness,
),
loadBrightnessOnStart: true,
themedWidgetBuilder: (BuildContext context, ThemeData theme) {
return MaterialApp(
title: 'Flutter Demo',
theme: theme,
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
},
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Dynamic Theme'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: FlutterDynamicTheme.of(context).toggleBrightness,
child: const Text('Toggle brightness'),
),
ElevatedButton(
onPressed: changeColor,
child: const Text('Change color'),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: showChooser,
child: const Icon(Icons.add),
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.insert_drive_file),
label: 'Tab 1',
),
BottomNavigationBarItem(
icon: Icon(Icons.show_chart),
label: 'Tab 2',
),
],
),
);
}
void showChooser() {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return BrightnessSwitcherDialog(
onSelectedTheme: (Brightness brightness) {
FlutterDynamicTheme.of(context).setBrightness(brightness);
},
);
},
);
}
void changeColor() {
FlutterDynamicTheme.of(context).setThemeData(
ThemeData(
primaryColor: Theme.of(context).primaryColor == Colors.indigo
? Colors.red
: Colors.indigo,
),
);
}
}
//region [ Default ]
class MyApp2 extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp2> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
// initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
// Future<void> initPlatformState() async {
// String platformVersion;
// // Platform messages may fail, so we use a try/catch PlatformException.
// try {
// platformVersion = await FlutterDynamicTheme.platformVersion;
// } on PlatformException {
// platformVersion = 'Failed to get platform version.';
// }
//
// // If the widget was removed from the tree while the asynchronous platform
// // message was in flight, we want to discard the reply rather than calling
// // setState to update our non-existent appearance.
// if (!mounted) return;
//
// setState(() {
// _platformVersion = platformVersion;
// });
// }
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
),
),
);
}
}
//endregion