0% found this document useful (0 votes)
18 views

Flutter_Widget_Example_Code

This document contains a complete Flutter application code that demonstrates various widgets and their functionalities. It includes features such as buttons, text fields, dialogs, and gesture detection, allowing users to interact with the UI and see real-time updates. Additionally, it showcases the use of a Provider pattern for state management with a simple example.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Flutter_Widget_Example_Code

This document contains a complete Flutter application code that demonstrates various widgets and their functionalities. It includes features such as buttons, text fields, dialogs, and gesture detection, allowing users to interact with the UI and see real-time updates. Additionally, it showcases the use of a Provider pattern for state management with a simple example.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Flutter Widget Practice - Full Code

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Widget Practice',
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {


@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


String message = "Hello, Flutter!";
bool showToast = false;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Widgets UI"),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
showToast = !showToast;
});
},
child: Icon(Icons.add),
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Text(
message,
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {
setState(() {
message = "Elevated Button Clicked!";
});
},
child: Text("Elevated"),
),
OutlinedButton(
onPressed: () {
setState(() {
message = "Outlined Button Clicked!";
});
},
child: Text("Outlined"),
),
TextButton(
onPressed: () {
setState(() {
message = "Text Button Clicked!";
});
},
child: Text("Text"),
),
IconButton(
onPressed: () {
setState(() {
message = "Icon Button Clicked!";
});
},
icon: Icon(Icons.thumb_up),
),
],
),
Container(
margin: EdgeInsets.symmetric(vertical: 16),
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.blue[50],
borderRadius: BorderRadius.circular(8),
),
child: TextField(
decoration: InputDecoration(
labelText: "Enter something",
border: OutlineInputBorder(),
),
onChanged: (value) {
setState(() {
message = value;
});
},
),
),
Card(
elevation: 4,
child: ListTile(
leading: Icon(Icons.person),
title: Text("List Tile Title"),
subtitle: Text("With Card and Icon"),
),
),
Image.network(
'https://via.placeholder.com/150',
height: 100,
),
GestureDetector(
onTap: () {
setState(() {
message = "Gesture Detected!";
});
},
child: Container(
margin: EdgeInsets.only(top: 10),
padding: EdgeInsets.all(12),
color: Colors.orange,
child: Text("Tap me"),
),
),
ExpansionTile(
title: Text("Tap to Expand (ExpansionTile)"),
children: [
ListTile(
title: Text("More Widgets Inside"),
subtitle: Text("This simulates Expanded usage."),
)
],
),
Builder(builder: (context) {
return ElevatedButton(
child: Text("Show Dialog"),
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Dialog Box"),
content: Text("This is a dialog message."),
actions: [
TextButton(
child: Text("Close"),
onPressed: () => Navigator.pop(context),
)
],
),
);
},
);
}),
if (showToast)
Container(
margin: EdgeInsets.symmetric(vertical: 10),
padding: EdgeInsets.all(10),
color: Colors.green[200],
child: Text("This is a custom toast!"),
),
ProviderExample(),
],
),
),
),
);
}
}

class ProviderExample extends StatelessWidget {


final String data = "Provided Data";

@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(top: 20),
padding: EdgeInsets.all(10),
color: Colors.purple[100],
child: Text("Provider Example: $data"),
);
}
}

You might also like