首页 > 解决方案 > Theme.of(context) 未传递给 simpleDialog

问题描述

我在我的应用程序中使用了 showDialog 函数。themeData 没有从我的父小部件传递到我的 showDialog。谁能告诉我为什么它不起作用?我必须指定主题在其他任何地方都有效,但不在我的 showDialog 中。我正在使用提供者将主题相应地更改为深色和浅色。无论如何,在 showDialog 正下方的代码中它可以工作。

Widget build(BuildContext context) {
Cart cartProvider = Provider.of<Cart>(context);
    Future<Future> showDialogFunction(BuildContext context) {
      // showing basket cart
      return showDialog(
        context: context,
        builder: (BuildContext context) {
          Widget cartDialog = BackdropFilter(
              filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
              child: SimpleDialog(
                backgroundColor: Colors.transparent,
                children: [
                  Consumer<Cart>(builder: (context, cart, child) {
                    return Container(
                      height: 1.sh,
                      width: 0.8.sw,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              IconButton(
                                icon: const Icon(Icons.close),
                                color: mainSecondaryColor,
                                onPressed: () {
                                  Navigator.pop(context);
                                },
                              ),
                              Text('Cosul meu',
                                  style: TextStyle(color: Theme.of(context).colorScheme.primary)),

我的 main.dart 文件:

class FinerApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (_) => ThemeNotifier(),
        child: ScreenUtilInit(
            designSize: Size(360, 690),
            allowFontScaling: true,
            builder: () => Consumer<ThemeNotifier>(
                    builder: (context, ThemeNotifier notifier, child) {
                  return Provider<AuthBase>(
                    create: (context) => Auth(),
                    child: MaterialApp(
                      debugShowCheckedModeBanner: false,
                      title: 'Flutter_ScreenUtil',
                      home: MaterialApp(
                        debugShowCheckedModeBanner: false,
                        theme: notifier.darkTheme
                            ? CustomTheme.darkTheme
                            : CustomTheme.lightTheme,
                        home: LandingPage(),
                        routes: routes,
                      ),
                    ),
                  );
                })));
  }
}

标签: flutter

解决方案


问题是你没有使用方法context给出的build。像这样更改build方法的变量名称:

Widget build(BuildContext buildContext) {...}

然后使用

Theme.of(buildContext).textTheme.caption)

推荐阅读