0% found this document useful (0 votes)
97 views3 pages

Flutter Stateful Widget Examples

This document contains 3 examples of using stateful widgets in Flutter. The first example creates a list view to display cards with text and icons. The second example creates a simple button that prints a message when pressed. The third example expands on this by adding a counter variable that is incremented and displayed on each button press using the setState method.

Uploaded by

Alisha malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views3 pages

Flutter Stateful Widget Examples

This document contains 3 examples of using stateful widgets in Flutter. The first example creates a list view to display cards with text and icons. The second example creates a simple button that prints a message when pressed. The third example expands on this by adding a counter variable that is incremented and displayed on each button press using the setState method.

Uploaded by

Alisha malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//Basic structure of stateful widget

import 'package:flutter/[Link]';

void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget {
List<String> titlelist = ["Irtaza","Danish","Halfbludd","shikka","Abdullah",];
List<String> subtitlelist = ["2K20-BSCS-153","2K20-BSCS-160","2K20-BSCS-
104","2K20-BSCS-110","2K20-BSCS-158",];
List<Icon> iconlist = [
Icon([Link]),
Icon([Link]),
Icon(Icons.abc_rounded),
Icon(Icons.ac_unit_sharp),
Icon(Icons.zoom_in),
Icon(Icons.zoom_in_rounded),
Icon([Link]),
];

@override
Widget build(BuildContext context){
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Theory 5'),
backgroundColor: [Link](255, 225, 21, 7),
),
body:
[Link]>0 ?
[Link](
itemCount: [Link],
itemBuilder:(context,index){
return Card(
margin: [Link](8),
elevation: 15,
child: ListTile(
title: Text(titlelist[index]),
subtitle: Text(subtitlelist[index]),
leading: iconlist[index],
trailing: Icon([Link]),
),
);
}
)
: Center(
child: Text('List is empty'),),
),
);
}
}
----------------------------------------------------------------
//To Create a simple button in stateful widget
import 'package:flutter/[Link]';

void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context){
return MaterialApp(
home:HomeScreen(),);
}
}
class HomeScreen extends StatefulWidget{
@override
State<StatefulWidget> createState(){
return HomeClass();
}

}
class HomeClass extends State<HomeScreen>{
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text('Stateful widget'),
backgroundColor: [Link](255, 0, 3, 2),
),
body: Center(
child: Column(children: [
Text('Click the following button :)'),
ElevatedButton(
onPressed: () {
print('Button is pressed.');
},
child: Text('click me'),
),
],)
),
);
}
}
----------------------------------------------------------------
//button that click and shows number
import 'package:flutter/[Link]';

void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context){
return MaterialApp(
home:HomeScreen(),);
}
}
class HomeScreen extends StatefulWidget{
@override
State<StatefulWidget> createState(){
return HomeClass();
}

}
class HomeClass extends State<HomeScreen>{
int i=0;
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text('Stateful widget'),
backgroundColor: [Link](255, 0, 3, 2),
),
body: Center(
child: Column(children: [
Text(
[Link](),
style: TextStyle(fontSize: 40),),
Text('Click the following button :)'),
ElevatedButton(
onPressed: () {
setState((){
i = i + 1;
});

print('Button is pressed $i');


}
,child: Text('click me'),
)
],)
),
);
}
}

You might also like