hesham shawky
hesham shawky

Reputation: 1151

Flutter How to force Dark mode on specific screens

I'm building a news app using flutter, the app has 2 theme modes already dark and light debends on phone settings and works really great, but I need on some screens on my app to be dark whatever such as videos section or video page ...etc

I googled this and all the results about the normal theming which I did already.

I don't think there's any code I can put here to help, but if there please let me know!

Upvotes: 1

Views: 2959

Answers (1)

Uroš
Uroš

Reputation: 1658

You can override the current theme at any time simply by placing the desired widget in a Theme class. I don't know if you are using Scaffold or not, but let's say you are then all you would need to do is:

// declare theme data if you don't have it already
final ThemeData specialThemeData = ThemeData(
  brightness: Brightness.dark,
  primaryColor: Colors.yellow[700],
  // and so on...
);
 
 
  @override
  Widget build(BuildContext context) {
    // this the point of interest, return a Theme with desired Theme Data
    return Theme(
      data: specialThemeData,
      child: Scaffold( 
      //...

It doesn't have to be Scaffold, it will work on any widegt.

Here is a fully functional example you can try out yourself:

import 'package:flutter/material.dart';

final ThemeData specialThemeData = ThemeData(
  brightness: Brightness.dark,
  primaryColor: Colors.yellow[700],
  accentColor: Colors.orange[500],

  textTheme: TextTheme(
    headline1: TextStyle(fontSize: 48.0, fontWeight: FontWeight.bold),
    headline6: TextStyle(fontSize: 24.0, fontStyle: FontStyle.italic),
    bodyText2: TextStyle(fontSize: 18.0),
  ),
);

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Home Page default theme'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _goToSpecialPage() {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => MySpecialPage()
        )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Your homepage, using default theme.',),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _goToSpecialPage,
        tooltip: 'Go to special page',
        child: Icon(Icons.navigate_next),
      ),
    );
  }
}

class MySpecialPage extends StatefulWidget {
  MySpecialPage({Key key}) : super(key: key);
  @override
  _MySpecialPageState createState() => _MySpecialPageState();
}

class _MySpecialPageState extends State<MySpecialPage> {
  void _backToHomePage(){
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    // this the point of interest, return a Theme with desired Theme Data
    return Theme(
      data: specialThemeData,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Special theme page'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('Your special page that uses a different theme.',),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _backToHomePage,
          tooltip: 'Go back to home page',
          child: Icon(Icons.navigate_before),
        ),
      ),
    );
  }
}

Upvotes: 7

Related Questions