...............................................................................................................................................................................................................................
Example: #1
First, we will see the basic example of TabBar, Three things are important while creating a tab bar
1. Create a TabController.
2. Create the tabs.
3. Create content for each tab.
Code Here
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Home(),
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text("Default tab bar!"),
bottom: TabBar(
tabs: [
Tab(text: "Home", icon: Icon(Icons.home)),
Tab(text: "Setting", icon: Icon(Icons.add_shopping_cart)),
],
),
),
),
);
}
}
Happy Coding :)
...............................................................................................................................................................................................................................
Example: #2
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Padding(
padding: const EdgeInsets.symmetric(vertical: 30.0, horizontal: 30.0),
child: Column(
children:<Widget>[
Container(
width: 100,
height: 100,color: Colors.blue,
),
Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(20)
),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: TabBar(
indicator: BoxDecoration(
color: Colors.pink[800],
borderRadius: BorderRadius.circular(20)
),
tabs:[
Tab(text:"Tab1"),
Tab(text: "Tab2",),
]),
),
),
SizedBox(
height: 300,
child: TabBarView(
children: [
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(20)
),
child: Center(child: Text('Hello')),
),
Container(
height: 100,
width: 100,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(20)
),
child: Center(child: Text('Hello')),
),
],
),
)
],
),
),
),
);
}
}
Happy Coding :)
...............................................................................................................................................................................................................................
Example: #3 []
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var lists = List<String>.generate(10, (index) => "List : $index");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter SnackBar Demo'),
),
body: ListView.builder(
itemCount: lists.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(lists[index]),
trailing: Container(
width: 60,
child: FlatButton(
child: Icon(
Icons.delete,
color: Colors.grey,
),
onPressed: () {
showSnackBar(context, index);
},
),
),
);
},
),
);
}
void showSnackBar(BuildContext context, int index) {
var deletedRecord = lists[index];
setState(() {
lists.removeAt(index);
});
SnackBar snackBar = SnackBar(
content: Text('Deleted $deletedRecord'),
action: SnackBarAction(
label: "UNDO",
onPressed: () {
setState(() {
lists.insert(index, deletedRecord);
});
},
),
);
Scaffold.of(context).showSnackBar(snackBar);
}
}
Happy Coding :)
...............................................................................................................................................................................................................................
Example: #4 []
Happy Coding :)
No comments:
Post a Comment