I am new to flutter and facing issues while trying to incorporate a button on the Appbar that changes the theme of the entire app.
This is the main.dart file code.
import 'package:flutter/material.dart';
import 'package:qrpay_app/routes/Routes.dart';
import 'module/splash/splash_view.dart';
import 'module/buy/buy_list_view.dart';
import 'module/sell/sell_list_view.dart';
import 'module/shop/shop_list_view.dart';
import 'module/account/account_list_view.dart';
import 'module/contact/contact_list_view.dart';
import 'module/help/help_list_view.dart';
import 'module/receipts/receipts_list_view.dart';
import 'module/about us/about_us_view.dart';
import 'package:qrpay_app/Themes/appThemes.dart';
import 'module/qr/qr_view.dart';
void main() {
runApp(new MaterialApp(
title: 'QRPAY Touch-Free',
theme: new ThemeData(primarySwatch: Colors.red),
home: SplashPage(),
routes: {
Routes.splash: (context) => SplashPage(),
Routes.buy: (context) => BuyPage(),
Routes.sell: (context) => SellPage(),
Routes.shop: (context) => ShopPage(),
Routes.qr: (context) => QRPage(),
Routes.account: (context) => AccountPage(),
Routes.contact: (context) => ContactPage(),
Routes.help: (context) => HelpPage(),
Routes.receipts: (context) => ReceiptsPage(),
Routes.about_us: (context) => AboutUsPage(),
},
));
}
This is the appThemes.dart code:
import 'package:flutter/material.dart';
class AppThemes {
// Simple constructor
AppThemes._();
static final ThemeData highContrast = ThemeData(
primarySwatch: Colors.black,
primaryTextTheme: TextTheme(
headline6: TextStyle(color: Colors.white),
),
scaffoldBackgroundColor: Colors.white,
appBarTheme: AppBarTheme(
color: Colors.black,
iconTheme: IconThemeData(
color: Colors.white,
),
elevation: 30.0,
),
);
static final ThemeData normalContrast = ThemeData(
scaffoldBackgroundColor: Colors.white,
appBarTheme: AppBarTheme(
color: Colors.red,
iconTheme: IconThemeData(
color: Colors.white,
),
),
);
}
I have routed the home to splash_view.dart. This is the appThemes.dart code:
import 'package:flutter/material.dart';
import 'package:qrpay_app/widget/drawer.dart';
import 'package:qrpay_app/Themes/appThemes.dart';
class SplashPage extends StatefulWidget {
static const String routeName = '/splash';
@override
_SplashPageState createState() => _SplashPageState();
}
class _SplashPageState extends State<SplashPage> {
void changeContrast() {
print("High contrast button clicked\nChanging app contrast\n");
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("QRPAY Touch-Free"),
elevation: 30.0,
actions: <Widget>[
ElevatedButton(
onPressed: () {
changeContrast();
},
child: Text('High Contrast')),
],
),
drawer: AppDrawer(),
body: new Center(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(
child: new Image.asset(
'res/images/qplogo.png',
width: MediaQuery.of(context).size.width * .9,
fit: BoxFit.cover,
),
),
],
),
),
);
}
}
I have added an elevated button that needs to change the theme of the entire app. Please help with mapping the functionality based on my dart files.
Also, I am confused about how to add the change in state
I tried working out with 'provider' but I am getting confused.
Please someone help me out. Also, please let me know if you require anything else to better understand my problem.
I appreciate you all taking out the time and helping me out.
Thank You.
You can read an article about Flutter Stream here: https://medium.com/@ayushpguptaapg/using-streams-in-flutter-62fed41662e4
Add a boolean value to track current theme
Then when your switch is on, just simply call:
And then listen for changes to update entire UI: