flutter - 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,
),
),
);
})));
}
}
解决方案
问题是你没有使用方法context
给出的build
。像这样更改build
方法的变量名称:
Widget build(BuildContext buildContext) {...}
然后使用
Theme.of(buildContext).textTheme.caption)
推荐阅读
- javascript - 如何使 Service Worker 从 API 缓存数据并在需要时更新缓存
- matrix - 为什么 z3py 求一个 4*4 矩阵的特征值要花这么多时间,却没有给出任何解?
- puppeteer - pyppeteer 等到页面的所有元素都加载完毕
- r - 我希望使用 R 编程与 3 个字段进行统计相关性
- python - Selenium 解析整个文档而不是 webelement
- vb.net - 比较 vb.net 中的两个数据表
- angular - Observable - 过滤和切换属性(角度)
- reactjs - 为什么在状态 React Redux 工具包中未定义数组
- flutter - 如何使用从数组传递的不同数据多次自动创建小部件?
- javascript - 未捕获的类型错误:无法将属性“类型”设置为 null