

Reusable Widgets & Reusable Custom AppBars in Flutter.
source link: https://www.coderzheaven.com/2020/07/17/reusable-widgets-reusable-custom-appbars-in-flutter/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Reusable Widgets & Reusable Custom AppBars in Flutter.
We will be talking about how to reuse widgets and create custom reusable AppBars in this article.
It’s so easy in flutter. Composing widgets and making it a more powerful and reusable one is so much easy in flutter.
Watch Video Tutorial
Let’s start at one of the wrong ways that beginners used to do.
Let’s say I want some TextFormField in my application and I want it to be of similar look and feel. So there is a tendency of writing methods like this below. Assume that you have a Utils class where we add all the utility methods…
import
'package:flutter/material.dart'
;
class
Utils {
//
static
getTF(TextEditingController controller) {
return
TextFormField(
controller: controller,
);
}
}
and now to add in the UI….
body: Container(
child: Column(
children: [
Utils.getTF(_emailController),
...
Well, this definitely works…, but do you see that it doesn’t look like a widget. This is one of the common mistakes that beginners do…
You can customize this function by sending more parameters and thereby customizing the ‘TextFormField’ widget…still this is the wrong way.
Flutter is more powerful to easily handle this..
Let’s see the proper way to do this…
Simply create a dart file with a proper name, such as app_textfield.dart…
then create a class named “AppTextFormField”, this is going to extend a StatelessWidget. This is very important. It is always a good practice to create reusable widgets as StatelessWidgets.
So our AppTextFormField class will look like this.
import
'package:flutter/material.dart'
;
class
AppTextFormField
extends
StatelessWidget {
//
AppTextFormField({
this
.controller,
this
.hintText,
this
.helpText,
this
.prefixIcon,
this
.suffixIcon,
this
.isPassword,
this
.enabled,
this
.readOnly,
this
.borderColor,
});
final
TextEditingController controller;
final
String hintText;
final
String helpText;
final
IconData prefixIcon;
final
IconData suffixIcon;
final
bool isPassword;
final
bool enabled;
final
bool readOnly;
final
Color borderColor;
@override
Widget build(BuildContext context) {
return
Container(
child: TextFormField(
controller: controller,
readOnly:
null
== readOnly ?
false
:
true
,
obscureText:
null
== isPassword ?
false
:
true
,
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.greenAccent,
width:
1.0
,
),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.greenAccent,
width:
1.0
,
),
),
border: OutlineInputBorder(
borderSide: BorderSide(
color:
null
== borderColor ? Colors.teal : borderColor,
width:
1.0
,
),
),
hintText:
null
== hintText ?
''
: hintText,
helperText:
null
== helpText ?
''
: helpText,
prefixIcon:
null
== prefixIcon ?
null
: Icon(prefixIcon),
suffix:
null
== suffixIcon ?
null
: Icon(suffixIcon),
enabled:
null
== enabled ?
true
:
false
,
),
),
);
}
}
Here we are setting all parameters as optional. So we are checking if some are null or not and taking action accordingly.
For eg: if ‘prefixIcon’ is null or not sent as parameter then we will set it to null, then it will disappear from the TextFormField, as simple as it is…
Then in our UI…
Container(
child: Column(
children: [
AppTextFormField(
controller: _emailController,
helpText:
'Email'
,
hintText:
'Email'
,
prefixIcon: Icons.email,
),
AppTextFormField(
controller: _passwordController,
helpText:
'Password'
,
hintText:
'Password'
,
isPassword:
true
,
prefixIcon: Icons.lock_open,
),
....
So now, it looks like some widgets are. being added to the Widget tree.
Reusable/Custom AppBar
Creating a reusable AppBar is little different. AppBar extends a PreferredSizeWidget. Yes, it needs a size…
So let’s see in that case how can we create a reusable AppBar.
So Create a new file app_bar.dart and I am gonna name by class IAppBar.
Here, the important thing is we need to supply a height or we will set it to default ‘kToolbarHeight’ from Flutter which is 56.0.
This is how the the whole class will look like…
import
'package:flutter/material.dart'
;
class
IAppBar
extends
PreferredSize {
//
final
Widget child;
final
double
height;
final
Color color;
IAppBar({
@required
this
.child,
this
.color,
this
.height = kToolbarHeight,
});
@override
Size get preferredSize => Size.fromHeight(height);
@override
Widget build(BuildContext context) {
return
Container(
height: preferredSize.height,
color: color ?? Colors.red,
alignment: Alignment.center,
child: child,
);
}
}
Now using this is as simple as below
return
Scaffold(
appBar: IAppBar(
height:
100
,
color: Colors.green,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
'HOME'
,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize:
20
,
),
),
SizedBox(
height:
20
,
),
],
),
),
body: Container(
//
),
);
The child property can be any widget you send. Here I am sending a Column widget.
That’s it!!!
Source code
- Youtube Downloader
- Youtube Videos
- Column
- Containers
- Video Tutorials
Recommend
-
78
Flutter is fast becoming one of the most popular frameworks for developing cross-platform mobile apps. Most Android and iOS developers today are starting to agree that it is a faster and...
-
58
README.md Flutter 菜鸟手册 帮助开发者快速上手 Flutter 内部测试中,1.0 正式版将于 2月 20日 发布。 ...
-
44
Flutter is a cross-platform language that works on both Android and iOS. However, Flutter draws all of its widgets itself and doesn’t use any resources from the platform it’s running on. This means that it will dra...
-
15
These are a few of the things I wish someone had shown me when I first started learning Flutter. Source:
-
41
Flutter Widgets 之 ListWheelScrollView ...
-
21
Understand Flutter's UI Approach 4:25 Get a brief overview of how flutter approa...
-
11
Creating Custom Reusable Widgets in FlutterGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupShapeGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGr...
-
3
Shadow DOM Support & reusable component objectsFebruary 22, 2019 · 7 min readShadow DOM is one of the key browser features that make up web components. Web components are a really great way to build reusable eleme...
-
6
Creating Custom Reusable Widgets in Flutter Group Group Group
-
7
MATLAB Online x GitHub で Open & Reusable な研究を » MATLAB ユーザーコミュニティー ※この投稿は 2...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK