Title: How to call widget section from another file in Flutter
Tags: flutter;flutter-layout
Question: i have Method and Widget section defined in main.dart and my code works fine,

i created new file method.dart and used all methods in that file and imported methods in my main file and it worked,

but my issue is how i can call Widget section from another file in main.dart.

main.dart:

```
  how to call following widget from another file:
```

```Widget buttonSection = Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          _buildButtonColumn(color, Icons.call, 'CALL'),
          _buildButtonColumn(color, Icons.near_me, 'ROUTE'),
          _buildButtonColumn(color, Icons.share, 'SHARE'),
        ],
      ),
    );
```

```
  following method can be called from another file:
```

```Column _buildButtonColumn(Color color, IconData icon, String label) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Icon(icon, color: color),
        Container(
          margin: const EdgeInsets.only(top: 8),
          child: Text(
            label,
            style: TextStyle(
              fontSize: 12,
              fontWeight: FontWeight.w400,
              color: color,
            ),
          ),
        ),
      ],
    );
  }
```

```
  Using it in material function:
```

```return MaterialApp(
      title: 'Flutter layout demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter layout demo'),
        ),
        body: ListView(
          children: [
            Image.asset(
              'images/lake.jpg',
              width: 600,
              height: 240,
              fit: BoxFit.cover,
            ),

            buttonSection,

          ],
        ),
      ),
    );
```

Comment: how to use widget of file1.dart in main.dart

Comment: please give one simple example if you can.

Comment: You can call them by using a class and object in file.

Comment: I can't  understood. Can you share all code with proper description?

Here is the accepted answer: To use your buttonSection widget from any file whether it is main.dart file or any other dart file you have to write the buttonSection widget out of any class and globally.

So you can have the access of that particular widget. If you are creating it inside a Stateful or Stateless class then you will not be able to access the buttonSection widget.

So create a dart file and add the following widget out of all class and define globally.

```Widget buttonSection = Container(
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: [
      _buildButtonColumn(color, Icons.call, 'CALL'),
      _buildButtonColumn(color, Icons.near_me, 'ROUTE'),
      _buildButtonColumn(color, Icons.share, 'SHARE'),
    ],
  ),
);
```

Then to access it anywhere import the dart file containing buttonSection widget and you can use it like,

```@override
  Widget build(BuildContext context) {
    return buttonSection;
  }
```

Also, keep in mind that your widget/variable/function name should not start with "_" to globally access it from another dart files.

Comment for this answer: Thankyou all clear but, what do you mean by define it out of all class ?
Comment for this answer: if i just copy and paste your code for buttonsection  in empty file file1.dart  without app body or main function or without stateless widget, will it work if i call it in main.dart? or i need to define everything as well again and again.
Comment for this answer: i want minimal code in file1.dart i want only buttonsection in it. is it possibly
Comment for this answer: Out of all means, you cannot define the widget inside any Stateful or Stateless widget. otherwise its scope will be limited to the respective Stateful or Stateless class and you will not be able to access it from other dart files directly.
Comment for this answer: You can access your buttonSection widget. But, if you are talking about _buildButtonColumn() widget inside it. Then it is not possible to access it. You have to define them.
Comment for this answer: What you can do is define your whole buttonSection widget with its all child in a new file then you can access that whole widget in as many files as  you want.
Comment for this answer: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203980/discussion-between-jay-mungara-and-fayakon).


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:12/10/19', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<flutter><flutter-layout>', 'Title': 'How to call widget section from another file in Flutter', 'ViewCount': 7415, 'id': 59265067, 'user_AboutMe': "<p>I love building things. While hard engineering problems are often intrinsically fun to tackle, I'm most attracted to solving real customer problems with a business justification. I'm looking for a senior individual contributor role where I can take on on collaborative team leadership responsibilites, and gain experience with architecture and project management.</p>\n", 'user_AccountId': 14812273.0, 'user_DisplayName': 'Fayakon', 'user_Reputation': 983.0, 'user_Views': 174.0, 'user_id': 10697521.0}}