Skip to content

Instantly share code, notes, and snippets.

@dhruvilp
Created January 23, 2023 02:56
Show Gist options
  • Save dhruvilp/6719513b0940ee1c3f6dd4cbd6be2571 to your computer and use it in GitHub Desktop.
Save dhruvilp/6719513b0940ee1c3f6dd4cbd6be2571 to your computer and use it in GitHub Desktop.
Flutter Responsive Layout App
import 'package:flutter/material.dart';
var defaultBackgroundColor = Colors.grey[300];
var appBarColor = Colors.grey[900];
var myAppBar = AppBar(
backgroundColor: appBarColor,
title: const Text(' '),
centerTitle: false,
);
var drawerTextColor = TextStyle(
color: Colors.grey[600],
);
var tilePadding = const EdgeInsets.only(left: 8.0, right: 8, top: 8);
var myDrawer = Drawer(
backgroundColor: Colors.grey[300],
elevation: 0,
child: Column(
children: [
const DrawerHeader(
child: Icon(Icons.favorite, size: 64),
),
Padding(
padding: tilePadding,
child: ListTile(
leading: const Icon(Icons.home),
title: Text('Dashboard', style: drawerTextColor),
),
),
],
),
);
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: const ResponsiveLayout(
mobileBody: MobileScaffold(),
tabletBody: TabletScaffold(),
desktopBody: DesktopScaffold(),
),
);
}
}
class ResponsiveLayout extends StatelessWidget {
final Widget mobileBody;
final Widget tabletBody;
final Widget desktopBody;
const ResponsiveLayout({
required this.mobileBody,
required this.tabletBody,
required this.desktopBody,
});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth < 500) {
return mobileBody;
} else if (constraints.maxWidth < 1100) {
return tabletBody;
} else {
return desktopBody;
}
},
);
}
}
class MobileScaffold extends StatefulWidget {
const MobileScaffold({Key? key}) : super(key: key);
@override
State<MobileScaffold> createState() => _MobileScaffoldState();
}
class _MobileScaffoldState extends State<MobileScaffold> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: defaultBackgroundColor,
appBar: myAppBar,
drawer: myDrawer,
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
// first 4 boxes in grid
AspectRatio(
aspectRatio: 1,
child: SizedBox(
width: double.infinity,
child: GridView.builder(
itemCount: 4,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemBuilder: (context, index) {
return MyBox();
},
),
),
),
// list of previous days
Expanded(
child: ListView.builder(
itemCount: 4,
itemBuilder: (context, index) {
return const MyTile();
},
),
),
],
),
),
);
}
}
class TabletScaffold extends StatefulWidget {
const TabletScaffold({Key? key}) : super(key: key);
@override
State<TabletScaffold> createState() => _TabletScaffoldState();
}
class _TabletScaffoldState extends State<TabletScaffold> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: defaultBackgroundColor,
appBar: myAppBar,
drawer: myDrawer,
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
// first 4 boxes in grid
AspectRatio(
aspectRatio: 4,
child: SizedBox(
width: double.infinity,
child: GridView.builder(
itemCount: 4,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4),
itemBuilder: (context, index) {
return MyBox();
},
),
),
),
// list of previous days
Expanded(
child: ListView.builder(
itemCount: 6,
itemBuilder: (context, index) {
return const MyTile();
},
),
),
],
),
),
);
}
}
class DesktopScaffold extends StatefulWidget {
const DesktopScaffold({Key? key}) : super(key: key);
@override
State<DesktopScaffold> createState() => _DesktopScaffoldState();
}
class _DesktopScaffoldState extends State<DesktopScaffold> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: defaultBackgroundColor,
appBar: myAppBar,
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// open drawer
myDrawer,
// first half of page
Expanded(
flex: 2,
child: Column(
children: [
// first 4 boxes in grid
AspectRatio(
aspectRatio: 4,
child: SizedBox(
width: double.infinity,
child: GridView.builder(
itemCount: 4,
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4),
itemBuilder: (context, index) {
return MyBox();
},
),
),
),
// list of previous days
Expanded(
child: ListView.builder(
itemCount: 7,
itemBuilder: (context, index) {
return const MyTile();
},
),
),
],
),
),
// second half of page
Expanded(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 400,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: Colors.grey[400],
),
),
),
// list of stuff
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: Colors.grey[200],
),
),
),
),
],
),
),
],
),
),
);
}
}
class MyBox extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: Colors.grey[400],
),
child: Center(
child: Text(
DateTime.now().second.toString(),
),
),
),
);
}
}
class MyTile extends StatelessWidget {
const MyTile({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 78,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: Colors.grey[200],
),
child: Center(
child: Text(
DateTime.now().second.toString(),
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment