Ui Design-Flutter Lab - Week 6
Ui Design-Flutter Lab - Week 6
Lab Program 6
Explanation:
This CustomButton class creates a reusable button that takes three
parameters:
o text: The label on the button.
o onPressed: The callback function triggered when the button is
pressed.
o color: Optional, background color of the button (defaults to blue).
You can use this button in your main screen or any other widgets by
importing this file.
Use the custom button:
In any of your other Dart files, import custom_button.dart and use the
button like this:
import 'package:your_project_name/custom_button.dart'; if we keep in
the other package
OR
import 'package:flutter/material.dart';
CustomButton({
required this.text,
required this.onPressed,
this.color = Colors.blue,
});
@override
return ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
),
child: Text(
text,
),
);
To create a custom text field in Flutter, you can create a separate Dart file for it.
Explanation:
import 'package:flutter/material.dart';
CustomTextField({
required this.hintText,
required this.controller,
this.obscureText = false,
});
@override
return TextField(
controller: controller,
obscureText: obscureText,
decoration: InputDecoration(
hintText: hintText,
border: OutlineInputBorder(),
filled: true,
fillColor: Colors.white,
),
);
You can also create a separate Dart file for the CustomContainer widget to keep
your code modular and reusable.
Explanation:
import 'package:flutter/material.dart';
CustomContainer({
required this.color,
required this.child,
this.height,
this.width,
this.onTap,
});
@override
return GestureDetector(
onTap: onTap,
child: Container(
height: height,
width: width,
constraints: BoxConstraints(
minHeight: 50,
),
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(10),
),
child: child,
),
);
Now Prepare your main.dart by importing the above three (3) dart files
1. Create a new Dart file named main.dart in the lib folder or /Update the
exisisting named main.dart in your lib folder
2. Add the following code to that file:
Explanation:
CustomTextField: A text field where users can input text, and the value is
passed to the _controller.
CustomButton: The submit button that displays the input text when
pressed.
CustomContainer: A container that displays a message and shows a
snackbar when tapped.
Stateful Widget: Allows updating the UI when the button is pressed and
text is entered.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
@override
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
),
body: Padding(
child: Column(
children: [
CustomTextField(
),
SizedBox(height: 20),
CustomButton(
text: 'Submit',
onPressed: () {
setState(() {
_displayText = _controller.text;
});
},
),
SizedBox(height: 20),
Text(
),
SizedBox(height: 20),
CustomContainer(
color: Colors.grey[200]!,
height: 100,
width: double.infinity,
child: Center(
child: Text(
),
),
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(
);
},
),
],
),
),
),
);
Output:
Create a New project and copy the 3 user defined dart files (1. 2. 3. ) into
lib folder and modify your main.dart with following code:
Key Differences
Theme Enhancements:
Previous main.dart file: The theme only set the primarySwatch to Colors.blue.
New main.dart file: The theme includes:
1. brightness: Set to Brightness.light, defining the overall theme brightness.
2. Text Theme:
Introduces headlineLarge for the app bar title.
Defines bodyLarge for general text styling.
Adds labelLarge for button text styling.
3. Elevated Button Theme: Configures styles for elevated buttons throughout
the app, including padding and background color.
4. Input Decoration Theme: Standardizes input field appearance with rounded
borders and filled backgrounds.
Text Styling:
1. Previous main.dart file: The app bar and body text used inline TextStyle
definitions, which resulted in inconsistent styling.
2. New main.dart file: Uses Theme.of(context).textTheme for consistent styling
across the app. The app bar title and other texts pull from the defined text
theme, improving consistency.
Button Color:
1. Previous main.dart file: The button color was implicitly set within the
CustomButton widget and defaults to the primary theme color.
2. New main.dart file: The CustomButton uses a custom color (Colors.green)
directly in the CustomButton constructor, which allows for more flexibility.
Program: main.dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
@override
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.light,
textTheme: TextTheme(
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
),
),
inputDecorationTheme: InputDecorationTheme(
filled: true,
fillColor: Colors.grey[200],
),
),
home: Scaffold(
appBar: AppBar(
),
body: Padding(
child: Column(
children: [
CustomTextField(
),
SizedBox(height: 20),
CustomButton(
text: 'Submit',
onPressed: () {
setState(() {
_displayText = _controller.text;
});
},
),
SizedBox(height: 20),
Text(
style: Theme.of(context).textTheme.bodyLarge,
),
SizedBox(height: 20),
CustomContainer(
color: Colors.grey[300]!,
height: 100,
width: double.infinity,
child: Center(
child: Text(
style: Theme.of(context).textTheme.bodyLarge,
),
),
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(
);
},
),
],
),
),
), ); } }
Output: