How to import a local widget in Flutter

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

So far, we quite a basic look at Flutter, and how everything works. We even made a cool sample Todo list application in Flutter.
However, we placed all our code in the lib/main.dart file until now.
You might be wondering, cool, but surely that gets a bit unmanageable. And this was the same thing I was wondering.
So let's look at how we can move some code to another file and import that into our main file.
Note: this guide does not tell you the best practices about folder structures, just an introduction to how it works.
Let's start simple and create a small widget that we'll try and embed in our basic app.
We'll work on the basic Flutter app. You can download this code on GitHub to get started with.
Alright, now let's make a new folder in our lib called screens and place a file called home.dart inside this folder.

In this file, we'll place the code of our basic widget like so:
import 'package:flutter/material.dart';
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
),
TextButton(
onPressed: _incrementCounter,
child: const Text('Add number'),
),
]),
),
);
}
}
And in our main.dart file, we can remove all that code, so it looks like this.
import 'package:flutter/material.dart';
void main() async {
runApp(
MaterialApp(debugShowCheckedModeBanner: false, home: MyApp()),
);
}
However, your editor will be saying it doesn't know what MyApp is, and it's right! So let's fix that by importing our new Home Screen!
import 'package:flutter_app/screens/home.dart';
Ok, that makes sense, but what is this package:flutter_app thing about?
And the package tells Flutter what package to load something from.
The flutter_app refers to our application. If you are wondering where we set this variable, open up the pubspec.yaml and check out the name variable!
name: flutter_app
Cool right! Our little package. And with this knowledge, we can now move certain widgets and screens into their files to create more structure in our application.
You can also download my example code from GitHub if you're unsure about how it should work.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter