BirjuVachhani / adaptive_theme

Easiest way to add support for light and dark theme in your flutter app.
https://pub.dev/packages/adaptive_theme
Apache License 2.0
464 stars 37 forks source link

[Question] How to get 'Get ThemeMode At App Start', working? #13

Closed yozachar closed 3 years ago

yozachar commented 3 years ago

Following Get ThemeMode at App Start, my code looks like this:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final savedThemeMode = await AdaptiveTheme.getThemeMode();
  runApp(MyApp(savedThemeMode: savedThemeMode)); // error here!
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AdaptiveTheme(
      light:
          ThemeData(brightness: Brightness.light, primarySwatch: Colors.pink),
      dark:
          ThemeData(brightness: Brightness.dark, primarySwatch: Colors.teal),
      initial: savedThemeMode ?? AdaptiveThemeMode.light,
      builder: (theme, darktheme) => MaterialApp(
        title: 'Schedule',
        theme: theme,
        darkTheme: darktheme,
        home: MyHomePage(
          title: 'Schdeule',
        ),
      )
    );
  }
}

But I get an error saying:

The named parameter 'savedThemeMode' isn't defined.
Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'savedThemeMode'.dart(undefined_named_parameter)

Please pardon my ignorance in Dart and/or Flutter and show me how to resolve this.

BirjuVachhani commented 3 years ago

@joe733 This does not have to do anything with the library. You're missing declaration for savedThemeMode in your MyApp class.

This should fix it:

class MyApp extends StatelessWidget {
  final AdaptiveThemeMode savedThemeMode;

  MyApp({Key key,@required this.savedThemeMode});

  ...
}
yozachar commented 3 years ago

Thanks a lot... still learning :)

Had to change widget_test.dart to

import 'package:adaptive_theme/adaptive_theme.dart';
...

void main() {
  AdaptiveThemeMode savedThemeMode;
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(MyApp(
      savedThemeMode: savedThemeMode,
    ));
...
BirjuVachhani commented 3 years ago

@joe733 You're welcome. Happy to help. 😊

agarnet74 commented 3 years ago

Hi, I have the same issue but I don't understand neither your answer or his change :

import 'package:flutter/material.dart'; import 'package:adaptive_theme/adaptive_theme.dart'; import 'pages/home_page.dart';

void main() async { WidgetsFlutterBinding.ensureInitialized(); final savedThemeMode = await AdaptiveTheme.getThemeMode(); runApp(MyApp(savedThemeMode : savedThemeMode)); }

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return AdaptiveTheme( light: ThemeData( brightness: Brightness.light, primarySwatch: Colors.lightBlue, accentColor: Colors.purple, ), dark: ThemeData( brightness: Brightness.dark, primarySwatch: Colors.lightBlue, accentColor: Colors.purple, ), initial: savedThemeMode ?? AdaptiveThemeMode.light, builder: (theme, darkTheme) => MaterialApp( debugShowCheckedModeBanner: false, title: 'Namka Nav', theme: theme, darkTheme: darkTheme, home: HomePage(), )); } }

issue : The named parameter 'savedThemeMode' isn't defined. Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'savedThemeMode'.

What am I missing ?

Thanks

BirjuVachhani commented 3 years ago

@agarnet74 Compare your code with the example app and you will see what you're missing!

Your MyApp should have this:

class MyApp extends StatelessWidget {

final AdaptiveThemeMode? savedThemeMode;

const MyApp({Key? key, this.savedThemeMode}) : super(key: key);

}
agarnet74 commented 3 years ago

Thanks, it works fine !