FLUTTER LAB 3,4
FLUTTER LAB 3,4
FLUTTER LAB 3,4
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
return Scaffold(
appBar: AppBar(title: Text("Tablet Layout")),
body: Padding(
padding: EdgeInsets.symmetric(horizontal: screenWidth *
0.05),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Tablet View",
style: TextStyle(fontSize: screenWidth * 0.04),
),
SizedBox(height: 20),
ElevatedButton(onPressed: () {}, child: Text("Button")),
],
),
),
),
);
}
}
class DesktopLayout extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Desktop Layout")),
body: Row(
children: [
Expanded(
child: Padding(
padding: EdgeInsets.all(32.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Desktop View",
style: TextStyle(
fontSize: MediaQuery.of(context).size.width * 0.03,
),
),
SizedBox(height: 20),
ElevatedButton(onPressed: () {}, child:
Text("Button")),
],
),
),
),
Expanded(
child: Container(
color: Colors.blueAccent,
child: Center(
child: Text("Additional Desktop Content"),
),
),
),
],
),
);
}
}
4.A. SETUP NAVIGATION BETWEEN TWO DIFFERNET
SCREENS USING NAVIGATION
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
// First Screen
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// Navigate to the Second Screen
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>
SecondScreen()),
);
},
child: Text('Go to Second Screen'),
),
),
);
}
}
// Second Screen
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Screen'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// Go back to the First Screen
Navigator.pop(context);
},
child: Text('Back to First Screen'),
),
),
);
}
}
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Navigation with Named Routes',
// Define named routes
routes: {
'/': (context) => FirstScreen(), // Default route
'/second': (context) => SecondScreen(),
},
initialRoute: '/’, // Set the initial screen (first screen)
);
}
}
// First Screen
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// Navigate to the Second Screen using the named route
Navigator.pushNamed(context, '/second');
},
child: Text('Go to Second Screen'),
),
),
);
}
}
// Second Screen
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Screen'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// Navigate back to the First Screen using Navigator.pop()
Navigator.pop(context);
},
child: Text('Back to First Screen'),
),
),
);
}
}