2

我的主要文件在下面;

import 'package:flutter/material.dart';
import 'package:ajanda/screens/mainmenu.dart';
Future<void> main() async{
  runApp(MaterialApp(
      debugShowCheckedModeBanner: false, title: "Takvim", home: MainMenu()));
}

主菜单文件在下面;

import 'package:ajanda/blocs/theme.dart';
import 'package:provider/provider.dart';

//const String testDevice = 'Mobile_id';

class MainMenu extends StatelessWidget {
  MainMenu({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<ThemeChanger>(
      builder: (_) => ThemeChanger(ThemeData.dark()),
      child: new MaterialAppWithTheme(),
    );
  }
}

class MaterialAppWithTheme extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MainMenuBody(),
    );
  }
}

class MainMenuBody extends StatefulWidget {
  @override
  _MainMenuBodyState createState() => _MainMenuBodyState();
}

class _MainMenuBodyState extends State<MainMenuBody> {

....

}

我正在尝试制作一个黑暗的主题,但我遇到了 2 个错误,我无法解决如果你能提供帮助我会很高兴的原因。我在构建函数中遇到了这样的错误。参数类型“Widget Function (BuildContext)”不能分配给参数类型“Widget Function (BuildContext, Widget)”。像这个错误的另一个问题:返回类型'ThemeChanger'不是'Widget',正如闭包的上下文所要求的那样。

Theme.dart 文件在下面

import 'package:flutter/material.dart';

class ThemeChanger with ChangeNotifier{

  ThemeData _themeData;

  ThemeChanger(this._themeData);

  getTheme() => _themeData;

  setTheme(ThemeData theme){
    _themeData= theme;

    notifyListeners();
  }

}
4

1 回答 1

1

你需要按照这些步骤

import 'package:flutter/material.dart';

final darkTheme = ThemeData(
 primarySwatch: Colors.grey,
 primaryColor: Colors.black,
 brightness: Brightness.dark,
 backgroundColor: const Color(0xFF212121),
 accentColor: Colors.white,
 accentIconTheme: IconThemeData(color: Colors.black),
 dividerColor: Colors.black12,
);
    
 
final lightTheme = ThemeData(
 primarySwatch: Colors.grey,
primaryColor: Colors.white,
brightness: Brightness.light,
backgroundColor: const Color(0xFFE5E5E5),
accentColor: Colors.black,
accentIconTheme: IconThemeData(color: Colors.white),
dividerColor: Colors.white54,
 );

提供者类

import 'package:flutter/material.dart';

class ThemeNotifier with ChangeNotifier {
  ThemeData _themeData;

  ThemeNotifier(this._themeData);

  getTheme() => _themeData;

  setTheme(ThemeData themeData) async {
    _themeData = themeData;
    notifyListeners();
  }
}

void main() => runApp(
  ChangeNotifierProvider<ThemeNotifier>(
    builder: (_) => ThemeNotifier(darkTheme),
    child: MyApp(),
  ),
);

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
 final themeNotifier = Provider.of<ThemeNotifier>(context);
 return MaterialApp(
   title: 'Chitr',
  theme: themeNotifier.getTheme(),
  home: HomePage(),
);
 }
 }
于 2020-08-15T18:06:06.710 回答