title: Flutter - How to access a function inside a statefulwidget in a file and then use it in a separate file with another stateful class?
body: <p>I am trying to use a function that is defined in file no:1 into file no:2.  I have imported the file 1 in file 2 and want to use some functions that is defined inside the class of file 1. I can access the class itself but not the variables and functions. I want to use those functions in an onpressed but can't.</p>
  
  <p><strong>CODE of FILE 1</strong></p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'dart:async';
  
  
  class TimeDisplay extends StatefulWidget {
    @override
    _TimeDisplayState createState() =&gt; _TimeDisplayState();
  }
  
  class _TimeDisplayState extends State&lt;TimeDisplay&gt; {
    Stopwatch watch = Stopwatch();
    Timer timer;
  
    String elapsedTime= '';
    @override
    Widget build(BuildContext context) {
      return Padding(
              padding: EdgeInsets.only(top: 15.0),
              child: Text(
                elapsedTime,
                textAlign: TextAlign.center,
                style: const TextStyle(fontSize: 80.0, color: Colors.white),
              ),
            );
    }
      startWatch(){
      watch.start();
      setTime();
    }
  
    stopWatch(){
      watch.stop();
      setTime();
    }
  
    resetWatch(){
      watch.reset();
      setTime();
    }
  
    setTime(){
      var timeSoFar = watch.elapsedMilliseconds;
      setState(() {
            elapsedTime = transformMilliseconds(timeSoFar);
          }); 
    }
  
    transformMilliseconds(int milliseconds) {
      int hundreds =(milliseconds / 10).truncate();
      int seconds =(hundreds / 100).truncate();
      int minutes =(seconds / 60).truncate();
  
      String minutesStr = (minutes % 60).toString().padLeft(2, '0');
      String secondsStr = (seconds % 60).toString().padLeft(2, '0');
      String hundredsStr = (hundreds % 100).toString().padLeft(2, '0');
  
      return "$minutesStr:$secondsStr";
    }
  }
  </code></pre>
  
  <p><strong>CODE of FILE 2</strong></p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:getfit/timedisplay.dart';
  import 'dart:async';
  
  
  
  
  
  class ChangingBtn extends StatefulWidget {
    @override
    _ChangingBtnState createState() =&gt; _ChangingBtnState();
  }
  
  class _ChangingBtnState extends State&lt;ChangingBtn&gt; {
      bool playing = false;
  
    @override
    Widget build(BuildContext context) {
      return Column(
        children: &lt;Widget&gt;[
          TimeDisplay(),
          Padding(
            padding: EdgeInsets.only(top: 15.0, bottom: 100.0),
            child: RawMaterialButton(
              shape: CircleBorder(),
              fillColor: Colors.red,
              splashColor: Colors.yellowAccent,
              highlightColor: Colors.orangeAccent.withOpacity(0.5),
              elevation: 10.0,
              highlightElevation: 5.0,
              onPressed: () {
  
                setState(() {
                 playing = !playing;
                 });
                 playing ?  startWatch
                 : stopWatch;
              },
  
              child: Padding(
                padding: EdgeInsets.all(25.0),
  
                child: Icon(
                 playing ?  Icons.pause
                 : Icons.play_arrow,
  
                  color: Colors.white,
                  size: 35.0,
                ),
              ),
            ),
          ),
        ],
      );
    }
  
  }
  </code></pre>
  
  <p>I think I am doing something wrong here, I am not sure</p>
  
  <pre><code>onPressed: () {
  
                setState(() {
                 playing = !playing;
                 });
                 playing ?  startWatch
                 : stopWatch;
              },
  </code></pre>
  
  <p>If anyone could explain it I would really appreciate it.</p>