Week 3@flutter UI
Week 3@flutter UI
EXPERIMENT-3
AIM:
a) Design a responsive UI that adapts to different screen sizes.
DESCRIPTION:
Responsive
Typically, a responsive app has had its layout tuned for the available screen size. Often this means (for
example), re-laying out the UI if the user resizes the window, or changes the device’s orientation. This is
especially necessary when the same app can run on a variety of devices, from a watch, phone, tablet, to a
laptop or desktop computer.
CODE:
void main() {
runApp(const Sunflower());
}
@override
State<StatefulWidget> createState() {
return _SunflowerState();
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
brightness: Brightness.dark,
appBarTheme: const AppBarTheme(elevation: 2),
),
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Sunflower'),
),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: SunflowerWidget(seeds),
),
const SizedBox(height: 20),
Text('Showing ${seeds.round()} seeds'),
SizedBox(
width: 300,
child: Slider(
min: 1,
max: maxSeeds.toDouble(),
value: seeds.toDouble(),
onChanged: (val) {
setState(() => seeds = val.round());
},
),
),
const SizedBox(height: 20),
],
),
),
),
);
}
}
class SunflowerWidget extends StatelessWidget {
static const tau = math.pi * 2;
static const scaleFactor = 1 / 40;
static const size = 600.0;
static final phi = (math.sqrt(5) + 1) / 2;
static final rng = math.Random();
@override
Widget build(BuildContext context) {
final seedWidgets = <Widget>[];
seedWidgets.add(AnimatedAlign(
key: ValueKey(i),
duration: Duration(milliseconds: rng.nextInt(500) + 250),
curve: Curves.easeInOut,
alignment: Alignment(r * math.cos(theta), -1 * r * math.sin(theta)),
child: const Dot(true),
));
}
seedWidgets.add(AnimatedAlign(
key: ValueKey(j),
duration: Duration(milliseconds: rng.nextInt(500) + 250),
curve: Curves.easeInOut,
alignment: Alignment(x, y),
child: const Dot(false),
));
}
return FittedBox(
fit: BoxFit.contain,
child: SizedBox(
height: size,
width: size,
child: Stack(children: seedWidgets),
),
);
}
}
@override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
color: lit ? Colors.orange : Colors.grey.shade700,
borderRadius: BorderRadius.circular(radius),
),
child: const SizedBox(
height: size,
width: size,
),
);
}
}
OUTPUT:
b) Implement media queries and breakpoints for responsiveness.
What is MediaQuery Class?
The MediaQuery class in Flutter is a crucial tool for creating responsive layouts. It provides access to
the media query data from the BuildContext context. This data is used to determine the physical
characteristics of the device's screen size, such as screen width, screen height, device pixel ratio, and
more.
import 'orientation.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
//home: const Media(),
home: const Oriten(),
);
}
}
Media.dart:
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
@override
State<Media> createState() => _MediaState();
}
return Scaffold(
appBar: AppBar(
title: const Text('Media'),
),
body: SingleChildScrollView(
child: Column(
children: [
Text(
"Hello, this is a text",
style: GoogleFonts.rubikIso(
fontSize: isPortrait ? 20 : 16,
color: Colors.green[300],
),
),
// Add your widgets here to try.
],
),
),
);
}
}
orientation.dart:
import 'package:flutter/material.dart';
@override
State<Oriten> createState() => _OritenState();
}