Home > Mobile >  How to call class from another dart file
How to call class from another dart file

Time:04-05

Hi I am eager to learn flutter. I am bit confused in here. Im trying to call class from another dart file. This is how my file structure. enter image description here For an example, i only need this in my main.dart.

enter image description here

In app.dart file, I want to call 3 features which are Home_screen, Parking_screen and Payment_screen.

enter image description here

In my homescreen.dart, I want to call all the widgets in my homescreen which are app_bar, button and news.

I didn’t know how to call from (1)app.dart file for 3 features which are Home, Parking and Payment (2)homescreen.dart for all widgets for HomeScreen

I would love if someone can help me and give me an idea ;(

CodePudding user response:

from what i've understood from the question, you want to achieve the following:

  1. want to use app_bar, news, button from "home/widgets" folder in home_screen.dart,

  2. Call homescreen_screen.dart, payment_screen.dart, parking_screen.dart in app.dart

One thing to understand is you cannot call Scaffold() under Scaffold(), what i mean by this is that:

suppose this is app.dart file:

/state building code here/{
return Scaffold(
   appBar : AppBar(),
   body : HomeScreen(),
 );
}

and this be home_screen.dart file:

/state building code here/{
 return Scaffold();
}

This will raise an error. Instead it should be

/state building code here/{
 return Column() or Row() or Container() //etc...
}

(1) Said that app.dart returns home_screen.dart where all widgtes under "home/widgets" are arranged in column,

Let this be your homescreen code:

import '' //import all necessary files including all 3 dart files from "home/widget"

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return HomeScreen();
  }
}

class HomeScreen extends StatefulWidget {
  
  HomeScreen({
    Key? key,
  }) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(height: 150, width:200, color : Colors.grey, child: AppBar_H()), //from "features/home/widget/app_barH.dart"
        Container(height: 150, width:200, color : Colors.grey, child: News()),  //from "features/home/widget/news.dart"
        Container(height: 150, width:200, color : Colors.grey, child: Button()),  //from "features/home/widget/button.dart"
      ]
    );
  }
}

This will create a screen with 3 column returning from app_barH.dart, news.dart and button.dart

As you've already imported the files, just add child of the container to be class from news.dart or app_barH.dart or button.dart

Notice how Column() has a children property and not child, this is as Column can contain multiple widgets in a vertical fashion, while Container(), which has child property can only accept one widget. children is of type list [].

(2) This can be achieved in a similar fashion to that of ans (1)

CodePudding user response:

If, you have the files app.dart and homescreen.dart, as long as app.dart includes an import statement like what you included in your images, you can use the public classes and values from homescreen.dart as if they had been defined in app.dart.
Public is everything that doesn't begin with a _

If for instance you have:

// a.dart
final String someVariable = "some variable";
final String _someOtherVariable = "another variable";

then:

// b.dart
import './a.dart'; 
void someFunction() {
  print(someVariable); // Will work, since it's public from a.dart

  print(_someOtherVariable); // Will fail since the variable is not public.
}

In the above I used a relative importing style. It looks for the file to import from in a different way, but otherwise has the same effect.

For your features, if they are Widget classes, since you have already added an import stement, you should be able to use the classes directly within app.dart

CodePudding user response:

You can write a code like below

import 'package:flutter/material.dart';
 
void main() => runApp(MyApp());
 
class MyClass extends StatelessWidget {
    ...
    void myFunction() { ... }
    static void myStaticFunction() { ... }
} 
 

In your other file import that file like this and use:

import "myclass .dart";

class OtherClass extends StatelessWidget {
    void otherFunction() {
        MyClass myClassObject = MyClass(); //Here you should rather reference your actual widget
        // Now you should be able to call the function like so
        myClassObject.myFunction();

        // Another way would be by using static functions
        MyClass.myStaticFunction();
    }
}

If you are using Statful widgets you can use Global keys

GlobalKey<MyStatefulWidgetState> widgetKey = GlobalKey<MyStatefulWidgetState>();

class MyStatefulWidget extends StatefulWidget {
    @override
    MyStatefulWidgetState createsState() = MyStatefulWidgetState();
}

class MyStatefulWidgetState extends State<MyStatefulWidget> {
    ...
    void someFunction() { ... }
}

You should then be able to do widgetKey.currentState.someFunction();

  • Related