Created
April 13, 2021 11:19
-
-
Save misterfourtytwo/35056c795b6f7d528ebe60d76267a64c to your computer and use it in GitHub Desktop.
adaptive theme switcher example
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:adaptive_theme/adaptive_theme.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
@override | |
Widget build(BuildContext context) { | |
return AdaptiveTheme( | |
initial: AdaptiveThemeMode.light, | |
light: ThemeData( | |
brightness: Brightness.light, | |
backgroundColor: Colors.white, | |
primaryColor: Colors.black, | |
), | |
dark: ThemeData( | |
brightness: Brightness.dark, | |
backgroundColor: Colors.black, | |
primaryColor: Colors.white, | |
), | |
builder: (light, dark) => MaterialApp( | |
title: 'Flutter Demo', | |
darkTheme: dark, | |
theme: light, | |
home: PageSwitcher(), | |
), | |
); | |
} | |
} | |
class PageSwitcher extends StatefulWidget { | |
PageSwitcher({Key key}) : super(key: key); | |
@override | |
_PageSwitcherState createState() => _PageSwitcherState(); | |
} | |
class _PageSwitcherState extends State<PageSwitcher> { | |
final List<Widget> pages = [Page1(), Page2(), Page3()]; | |
int currentPage = 0; | |
void _setPage(int newPage) { | |
if (currentPage != newPage) | |
setState(() { | |
currentPage = newPage; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: pages[currentPage], | |
bottomNavigationBar: BottomNavigationBar( | |
selectedItemColor: Colors.amber[800], | |
currentIndex: currentPage, | |
onTap: _setPage, | |
backgroundColor: Theme.of(context).backgroundColor, | |
unselectedItemColor: Theme.of(context).primaryColor, | |
items: [ | |
BottomNavigationBarItem( | |
label: 'Page 1', | |
icon: Icon( | |
Icons.bar_chart, | |
), | |
), | |
BottomNavigationBarItem( | |
label: 'Page 2', | |
icon: Icon( | |
Icons.multiple_stop, | |
), | |
), | |
BottomNavigationBarItem( | |
label: 'Page 3', | |
icon: Icon( | |
Icons.attach_money, | |
), | |
), | |
], | |
), | |
); | |
} | |
} | |
class Page1 extends StatelessWidget { | |
const Page1({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return SafeArea( | |
child: Scaffold( | |
backgroundColor: Theme.of(context).backgroundColor, | |
appBar: AppBar( | |
brightness: Theme.of(context).brightness, | |
title: Text( | |
'Page #1', | |
style: TextStyle( | |
color: Theme.of(context).primaryColor, | |
), | |
), | |
backgroundColor: Theme.of(context).backgroundColor, | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Text( | |
'Page #1 content', | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
color: Theme.of(context).primaryColor, | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class Page2 extends StatelessWidget { | |
const Page2({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Theme.of(context).backgroundColor, | |
appBar: AppBar( | |
brightness: Theme.of(context).brightness, | |
title: Text( | |
'Page #2', | |
style: TextStyle( | |
color: Theme.of(context).primaryColor, | |
), | |
), | |
backgroundColor: Theme.of(context).backgroundColor, | |
actions: [ | |
IconButton( | |
icon: Icon( | |
Icons.language, | |
), | |
color: Theme.of(context).primaryColor, | |
onPressed: () {}, | |
), | |
IconButton( | |
icon: Icon(Icons.opacity), | |
color: Theme.of(context).primaryColor, | |
onPressed: () => AdaptiveTheme.of(context).toggleThemeMode(), | |
), | |
], | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Text( | |
'Page #2 content', | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
color: Theme.of(context).primaryColor, | |
), | |
), | |
], | |
), | |
); | |
} | |
} | |
class Page3 extends StatelessWidget { | |
const Page3({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Theme.of(context).backgroundColor, | |
appBar: AppBar( | |
brightness: Theme.of(context).brightness, | |
title: Text( | |
'Page #3', | |
style: TextStyle( | |
color: Theme.of(context).primaryColor, | |
), | |
), | |
backgroundColor: Theme.of(context).backgroundColor, | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Text( | |
'Page #3 content', | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
color: Theme.of(context).primaryColor, | |
), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment