Android Studio 错误:没有为类“_PomodoroState”定义方法“raisedButton”

hm2xizp9  于 2022-12-19  发布在  Android
关注(0)|答案(4)|浏览(220)

我目前正在做一个flutter教程在android工作室,我有这个问题,我试图创建一个类,试图成为一个变量,但没有运气,我也命名了一个文件“raised_button.dart”,并使用stful小部件,看看它是否工作,但它不工作这是我的代码:

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
                                  ),
                                  ),
                                )
                              ),
                          )
                                ],
                              )
                          ),
                      ),
                    ),
              ],
            ),
          ),
        ),
    );
  }
}

这就是终端显示的

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(

在这种特殊情况下我该怎么办?先谢谢你。

ibrsph3r

ibrsph3r1#

大写字母R是RaisedButton不是raisedButton
但是,RaisedButton已过时。

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

你想使用ElevatedButton,你还需要把你的样式转移到new style属性,在这里你把颜色等传递给ElevatedButton.styleFrom()构造函数。

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

然而...这个外观也是根据Flutter所遵循的材料指南来设计的。在这里看看如何让按钮看起来漂亮而现代:https://material.io/components/buttons所有的名称也是Flutter中小部件的名称。如果你在那里卡住了,随时可以再问一次!

aoyhnmkz

aoyhnmkz2#

我想人们应该看看你的raisedButton类,如果它是一个单独的类,这里应该有一个import,但它不是,类名应该以大写字母开头

ddhy6vgd

ddhy6vgd3#

RaisedButton在最新的flutter版本中已弃用。您可以将RaisedButton替换为ElevatedButton

ElevatedButton(
            child: Text('ElevatedButton'))
efzxgjgh

efzxgjgh4#

Flutter新版本不再使用RaisedButton
相反,您可以使用提升按钮

ElevatedButton(
            onPressed: () {
              print('login clicked');
            },
            child: Text(
              'Login',
              style: TextStyle(fontSize: 18.0, fontFamily: "Brand Bold"),
            ))

相关问题