پخته‌ترین و کاربردی‌ترین محصولات آموزشی سون‌لرن را با آفرهای ویژه تهیه کنید🔥
۰ ثانیه
۰ دقیقه
۰ ساعت
۱ hamid delbari
خطا در widget.toggleThemeMode
جامعه فلاتر ایجاد شده در ۲۹ بهمن ۱۴۰۱

سلام

برنامه در هنگام اجرای این خط به مشکل می‌خوره:

    onTap: widget.toggleThemeMode,
@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(
        title: const Text("top title"),
        actions: const [
          SizedBox(width: 4),
          Icon(Icons.more),
          InkWell(
                onTap: widget.toggleThemeMode,
            child: Padding(
              padding: EdgeInsets.fromLTRB(10, 0, 20, 0),
              child: Icon(CupertinoIcons.ellipsis_vertical),
            ),
          ),
        ],
      ),

خطاش اینه:

lib/main.dart:143:26: Error: Undefined name 'widget'.

         onTap: widget.toggleThemeMode,

             ^^^^^^

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
void main() {
  runApp(const MyApp());
}
class MyApp extends StatefulWidget {
  const MyApp({super.key});
  @override
  State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
  // This widget is the root of your application.
  ThemeMode _themeMode = ThemeMode.dark;
  @override
  Widget build(BuildContext context) {
    Color surfaceColor = Color(0x0dffffff);
    Color primaryColor = Colors.pink.shade400;
    return MaterialApp(
      title: 'Flutter Demo',
      theme: _themeMode == ThemeMode.dark
          ? MyAppThemeConfig.dark().getTheme()
          : MyAppThemeConfig.light().getTheme(),
      home: MyHomePage(toggleThemeMode: (){
        setState(() {
          if(_themeMode ==ThemeMode.dark)
            _themeMode = ThemeMode.light;
          else
            _themeMode = ThemeMode.dark;
        });
      },),
    );
  }
}
class MyAppThemeConfig {
  final Color primaryColor;
  final Color secondaryTextColor;
  final Color surfaceColor;
  final Color backgroundColor;
  final Color appBarColor;
  final Brightness brightness;
  final Color primaryTextColor;
  MyAppThemeConfig.dark():
        primaryColor = Colors.white,
        primaryTextColor = Colors.white,
        secondaryTextColor=Colors.white70,
        surfaceColor=Color(0x0dffffff),
        backgroundColor=Color.fromARGB(255, 30, 30, 30),
        appBarColor=Colors.black,
        brightness=Brightness.dark;
  MyAppThemeConfig.light():primaryColor = Colors.grey.shade900,
        primaryTextColor = Colors.white,
        secondaryTextColor=Colors.grey.shade900.withOpacity(0.8),
        surfaceColor=Color(0x0d000000),
        backgroundColor=Colors.white,
        appBarColor=Color.fromARGB(255, 235, 235, 235),
        brightness=Brightness.light;
  ThemeData getTheme(){
    return ThemeData(
      // This is the theme of your application.
      //
      // Try running your application with "flutter run". You'll see the
      // application has a blue toolbar. Then, without quitting the app, try
      // changing the primarySwatch below to Colors.green and then invoke
      // "hot reload" (press "r" in the console where you ran "flutter run",
      // or simply save your changes to "hot reload" in a Flutter IDE).
      // Notice that the counter didn't reset back to zero; the application
      // is not restarted.
      primarySwatch: Colors.blue,
      primaryColor: primaryColor,
      brightness: brightness,
      elevatedButtonTheme: ElevatedButtonThemeData(
        style: ButtonStyle(
          backgroundColor: MaterialStatePropertyAll(primaryColor),
        ),
      ),
      dividerTheme: DividerThemeData(indent: 0, endIndent: 0),
      dividerColor: surfaceColor,
      inputDecorationTheme: InputDecorationTheme(
        border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: BorderSide.none),
        filled: true,
        fillColor: surfaceColor,
      ),
      scaffoldBackgroundColor: backgroundColor,
      appBarTheme: AppBarTheme(backgroundColor: appBarColor, foregroundColor: primaryTextColor ),
      textTheme: GoogleFonts.latoTextTheme(TextTheme(
          bodyText2: TextStyle(fontSize: 16,color:primaryColor,),
          bodyText1: TextStyle(
              fontSize: 13, color: secondaryTextColor),
          headline6: TextStyle(fontWeight: FontWeight.w700,color:primaryColor),
          subtitle1: TextStyle(fontSize: 16, fontWeight: FontWeight.bold,color:primaryColor))),
    );
  }
}
class MyHomePage extends StatefulWidget {
  final Function() toggleThemeMode;
  const MyHomePage({Key? key, required this.toggleThemeMode}) : super(key : key);
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
enum _SkillType {
  php,
  python,
  vb,
  javaScript,
}
class _MyHomePageState extends State<MyHomePage> {
  _SkillType _skill = _SkillType.php;
  void updateSelectedSkill(_SkillType skillType) {
    setState(() {
      this._skill = skillType;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("ddd ddddjjjjjdd"),
          actions: const [
            SizedBox(width: 4),
            Icon(Icons.more),
            InkWell(
                //  onTap: widget.toggleThemeMode,
              child: Padding(
                padding: EdgeInsets.fromLTRB(10, 0, 20, 0),
                child: Icon(CupertinoIcons.ellipsis_vertical),
              ),
            ),
          ],
        ),
        body: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.all(32),
                child: Row(
                  children: [
                    ClipRRect(
                        borderRadius: BorderRadius.circular(10),
                        child: Image.asset('assets/images/php.png',
                            width: 60, height: 60)),
                    SizedBox(width: 12),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            'hamid delbari',
                            style: Theme.of(context).textTheme.subtitle1,
                          ),
                          Text('programmer',
                              style: Theme.of(context).textTheme.bodyText2),
                          Row(
                            children: [
                              Icon(CupertinoIcons.location_solid,
                                  size: 15,
                                  color: Theme.of(context)
                                      .textTheme
                                      .bodyText1!
                                      .color),
                              SizedBox(width: 3),
                              Text('34 years',
                                  style: Theme.of(context).textTheme.caption),
                            ],
                          )
                        ],
                      ),
                    ),
                    Icon(CupertinoIcons.heart,
                        color: Theme.of(context).primaryColor),
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(32, 0, 16, 16),
                child: Text(
                  'hi. im hamid delbari, programming for 15 years. php, pythn, java expert ',
                  style: Theme.of(context).textTheme.bodyText1,
                ),
              ),
              Divider(),
              Padding(
                padding: const EdgeInsets.fromLTRB(18, 0, 18, 12),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text('Skills',
                        style: Theme.of(context)
                            .textTheme
                            .bodyText2!
                            .copyWith(fontWeight: FontWeight.w900)),
                    SizedBox(width: 2),
                    Icon(CupertinoIcons.chevron_down, size: 12),
                  ],
                ),
              ),
              Center(
                child: Wrap(
                  direction: Axis.horizontal,
                  spacing: 8,
                  runSpacing: 8,
                  children: [
                    Skill(
                      type: _SkillType.php,
                      title: 'php',
                      imagePath: 'assets/images/php.png',
                      shadowColor: Colors.red,
                      isActive: _skill == _SkillType.php,
                      onTap: () {
                        updateSelectedSkill(_SkillType.php);
                      },
                    ),
                    Skill(
                      type: _SkillType.python,
                      title: 'python',
                      imagePath: 'assets/images/python.png',
                      shadowColor: Colors.yellow,
                      isActive: _skill == _SkillType.python,
                      onTap: () {
                        updateSelectedSkill(_SkillType.python);
                      },
                    ),
                    Skill(
                      type: _SkillType.vb,
                      title: 'vb',
                      imagePath: 'assets/images/visual-basic.png',
                      shadowColor: Colors.deepPurple,
                      isActive: _skill == _SkillType.vb,
                      onTap: () {
                        updateSelectedSkill(_SkillType.vb);
                      },
                    ),
                    Skill(
                      type: _SkillType.javaScript,
                      title: 'java script',
                      imagePath: 'assets/images/java-script.png',
                      shadowColor: Colors.black,
                      isActive: _skill == _SkillType.javaScript,
                      onTap: () {
                        updateSelectedSkill(_SkillType.javaScript);
                      },
                    ),
                    Skill(
                      type: _SkillType.javaScript,
                      title: 'java script',
                      imagePath: 'assets/images/java-script.png',
                      shadowColor: Colors.black,
                      isActive: _skill == _SkillType.javaScript,
                      onTap: () {
                        updateSelectedSkill(_SkillType.javaScript);
                      },
                    ),
                  ],
                ),
              ),
              Divider(),
              Padding(
                padding: const EdgeInsets.fromLTRB(32, 16, 32, 16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('Personal Information',
                        style: Theme.of(context)
                            .textTheme
                            .bodyText2!
                            .copyWith(fontWeight: FontWeight.w900)),
                    SizedBox(
                      height: 12,
                    ),
                    TextField(
                      decoration: InputDecoration(
                        labelText: "Email",
                        prefixIcon: Icon(CupertinoIcons.at),
                      ),
                    ),
                    SizedBox(
                      height: 12,
                    ),
                    TextField(
                      decoration: InputDecoration(
                        labelText: "Password",
                        prefixIcon: Icon(CupertinoIcons.lock),
                      ),
                    ),
                    SizedBox(height: 6),
                    SizedBox(
                        width: double.infinity,
                        height: 48,
                        child: ElevatedButton(
                            onPressed: () {}, child: Text('Save')))
                  ],
                ),
              ),
            ],
          ),
        ));
  }
}
class Skill extends StatelessWidget {
  final _SkillType type;
  final String title;
  final String imagePath;
  final Color shadowColor;
  final bool isActive;
  final Function() onTap;
  const Skill({
    super.key,
    required this.type,
    required this.title,
    required this.imagePath,
    required this.shadowColor,
    required this.isActive,
    required this.onTap,
  });
  @override
  Widget build(BuildContext context) {
    final BorderRadius defaultBorderRadius = BorderRadius.circular(20);
    return InkWell(
      borderRadius: defaultBorderRadius,
      onTap: onTap,
      child: Container(
        width: 110,
        height: 110,
        decoration: isActive
            ? BoxDecoration(
          color: Theme.of(context).dividerColor,
          borderRadius: defaultBorderRadius,
        )
            : null,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
                decoration: isActive
                    ? BoxDecoration(boxShadow: [
                  BoxShadow(
                    color: shadowColor.withOpacity(0.5),
                    blurRadius: 40,
                  )
                ])
                    : null,
                child: Image.asset(imagePath, width: 60, height: 60)),
            const SizedBox(
              height: 8,
            ),
            Text(title),
          ],
        ),
      ),
    );
  }
}

ممنون میشم بگید مشکل چیه؟

سلام. جلوی action کلمه ی const رو بردارید

یلدا محصلی ۲۹ بهمن ۱۴۰۱، ۲۱:۰۳