How to call class from another dart file

161

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)

Share:
161
rider2501
Author by

rider2501

Updated on January 04, 2023

Comments

  • rider2501
    rider2501 10 months

    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 ;(