Home > Mobile >  Error: The method 'raisedButton' isn't defined for the class '_PomodoroState
Error: The method 'raisedButton' isn't defined for the class '_PomodoroState

Time:06-06

I'm currently doing a flutter tutorial in android studio and I have this issue, I tried to create a class, tried to become it a variable, but no luck, also I named a file “raised_button.dart” and use the stful widget to see if it works but it doesn't work This is my code:

import 'package:flutter/material.dart';
import 'package:percent_indicator/circular_percent_indicator.dart';
import 'dart:async';
void main() => runApp(MaterialApp(

  debugShowCheckedModeBanner: false,
  home: Pomodoro(),
));

class Pomodoro extends StatefulWidget {
  const Pomodoro({Key? key}) : super(key: key);

  @override
  State<Pomodoro> createState() => _PomodoroState();
}

class _PomodoroState extends State<Pomodoro> {

  double percent = 0;
  static int TimeInMinut = 240;
  int TimeInSec = TimeInMinut * 60;

  late Timer timer;

  _StartTimer(){
    TimeInMinut = 240;
    int Time = TimeInMinut *60;
    double SecPercent = (Time/100);
    timer = Timer.periodic(Duration(seconds:1), (timer) {
      setState((){
        if(Time > 0){
          Time--;
          if(Time % 60 ==0){
            TimeInMinut --;
          }if(Time % SecPercent == 0){
            if(percent <1){
              percent  = 0.01;
            }else{
              percent = 1;
            }
          }
        }else{
          percent = 0;
          TimeInMinut = 240;
          timer.cancel();
        }
      });
    });
  }
  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
          body: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [Colors.orangeAccent, Colors.redAccent],
                begin: FractionalOffset(0.5,1)
              )
            ),
            width: double.infinity,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(top: 25.0),
                  child: Text(
                    "Pomodoro Clock",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 40.0
                    ),
                  ),
                ),
                Expanded(
                    child: CircularPercentIndicator(
                      percent: percent,
                      animation: true,
                      animateFromLastPercent: true,
                      radius: 150.0,
                      lineWidth: 20.0,
                      progressColor: Colors.white,
                      center: Text(
                        "$TimeInMinut",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 80.0
                        ),
                      ),
                    ),
                ),
                SizedBox(height: 10.0,),
                Expanded(
                  child: Container(
                    width: double.infinity,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(topRight: Radius.circular(30.0),topLeft: Radius.circular(30.0))
                    ),
                    child: Padding(
                      padding: EdgeInsets.only(top: 30.0, left: 20.0, right: 20.0),
                      child: Column(
                        children: <Widget>[
                          Expanded(
                              child: Row(
                                children: <Widget>[
                                  Expanded(
                                    child: Column(
                                        children: <Widget>[
                                          Text(
                                            "Focus Time",
                                            style: TextStyle(
                                              fontSize: 20.0,
                                            ),
                                          ),
                                          SizedBox(height: 10.0,),
                                          Text(
                                            "240",
                                            style: TextStyle(
                                              fontSize: 50.0
                                            ),
                                          ),
                                        ]
                                    ),
                                  ),
                                  Expanded(
                                    child: Column(
                                        children: <Widget>[
                                          Text(
                                            "Pause Time",
                                            style: TextStyle(
                                              fontSize: 20.0,
                                            ),
                                          ),
                                          SizedBox(height: 10.0,),
                                          Text(
                                            "20",
                                            style: TextStyle(
                                                fontSize: 50.0
                                            ),
                                          ),
                                        ]
                                    ),
                                  )
                                ],
                              )
                          ),
                          Padding(
                              padding: EdgeInsets.symmetric(vertical: 28.0),
                              child: raisedButton(
                                onPressed: _StartTimer,
                                  color: Colors.redAccent,
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(100.0),
                                ),
                                child: Padding(
                                  padding: EdgeInsets.all(20.0),
                                  child: Text("Start focusing",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 22.0
                                  ),
                                  ),
                                )
                              ),
                          )
                                ],
                              )
                          ),
                      ),
                    ),
              ],
            ),
          ),
        ),
    );
  }
}

and this what is shows on terminal

lib/main.dart:151:38: Error: The method 'raisedButton' isn't defined for the class '_PomodoroState'.
 - '_PomodoroState' is from 'package:pomodoris/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'raisedButton'.
                              child: raisedButton(

What should I do in that particular case? Thank you in advance.

CodePudding user response:

I think for an answer people should see your raisedButton class. If it's a separate class here should be an import with it, but it isn't. And the class name should start with a capital letter

CodePudding user response:

Capitalize the R. It's RaisedButton, not raisedButton.

However, RaisedButton is depricated. Instead of

raisedButton(
   onPressed: _StartTimer,
   color: Colors.redAccent,
   shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.circular(100.0),
   ),
   child: Padding(

you want to use ElevatedButton. You also need to move your styling to the new style property, where you pass the color etc. into ElevatedButton.styleFrom() constructor.

ElevatedButton(
   style: ElevatedButton.styleFrom(
      primary: Colors.red,
      shape: RoundedRectangleBorder(
         borderRadius: BorderRadius.circular(100.0),
      ),
   ),
onPressed: _StartTimer,
child: Padding(

However... This look is also depricated according to the Material Guidelines that Flutter follows. See how to make buttons look nice and modern here: https://material.io/components/buttons All of the names are also the names of the widgets in Flutter. If you get stuck there, feel free to ask again!

  • Related