Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended Dark/Light or custom Theme implementation in Flutter Cupertino

Tags:

flutter

I’m currently implementing an iOS style app in flutter using the CupertinoApp class and Cupertino widgets. Now I would like to implement a different Theme for the application (like feedly did), so that the user can switch between both during runtime.

Unlike a MaterialApp, CupertinoApp has no property to set the theme.

MaterialApp with theme property:

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

CupertinoApp without theme property:

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      title: 'Flutter Demo',
      // theme: ThemeData(
      //   primarySwatch: Colors.blue,
      // ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

It looks like sb. is working on a CupertinoTheme which actually seems to be a Theme for a MaterialApp and not for a CupertinoApp.

What's the recommended way to define themes and change them during runtime in a Cupertino style app (e.g. Dark and Light Theme)?

like image 624
Daniel Avatar asked Jan 04 '19 13:01

Daniel


People also ask

Should I use material or Cupertino flutter?

The Material design language was created for any platform, not just Android. When you write a Material app in Flutter, it has the Material look and feel on all devices, even iOS. If you want your app to look like a standard iOS-styled app, then you would use the Cupertino library.

What is Cupertino design in flutter?

An application that uses Cupertino design. A convenience widget that wraps a number of widgets that are commonly required for an iOS-design targeting application. It builds upon a WidgetsApp by iOS specific defaulting such as fonts and scrolling physics.


1 Answers

First of all CupertinoThemeData is an alternative for ThemeData:

import 'package:flutter/cupertino.dart';

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

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
    return CupertinoApp(
      title: 'Flutter Demo',
      theme: CupertinoThemeData(),
      home: MyHomePage(),
     );
    }
  }

CupertinoThemeData has a 'raw' property which contains these properties: Brightness, primaryColor, primaryContrastingColor, textTheme and so on. Would you want to choose either dark or light? that's too easy!!

CupertinoThemeData.raw(Brightness.light)
CupertinoThemeData.raw(Brightness.dark)

but if you want to change it during runtime you should write a function:

CupertinoThemeData generalTheme(Brightness brightness) {
  CupertinoThemeData _basicCupertinoTheme = CupertinoThemeData.raw(
    brightness,
    ...
}

As you see, you can send an argument to function but don't forget to use setState method. For more info check out CupertinoThemeData.raw constructor

like image 103
Saeed Avatar answered Nov 20 '22 20:11

Saeed