Skip to content

Instantly share code, notes, and snippets.

@ChangJoo-Park
Created January 24, 2020 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChangJoo-Park/ff66b6c2282206c161de9662c18a7ecb to your computer and use it in GitHub Desktop.
Save ChangJoo-Park/ff66b6c2282206c161de9662c18a7ecb to your computer and use it in GitHub Desktop.
Flutter facebook newsfeed refactoring with method (anti-pattern)
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: FacebookNewsfeed(),
),
),
);
}
}
class FacebookNewsfeed extends StatelessWidget {
@override
Widget build(BuildContext context) {
double screenHeight = MediaQuery.of(context).size.height;
return Container(
color: Colors.white,
child: SafeArea(
child: Stack(
children: <Widget>[
Column(
children: <Widget>[
// AppBar
_buildAppBar(screenHeight, context),
// Header
_buildHeaderTabs(screenHeight),
// Newsfeed
_buildNewsfeedItems(screenHeight)
],
),
// BottomAction
_buildBottomActions(context, screenHeight)
],
),
),
);
}
Expanded _buildNewsfeedItems(double screenHeight) {
return Expanded(
child: Container(
padding: const EdgeInsets.all(16.0),
color: Color.fromARGB(255, 242, 242, 242),
child: ListView(
children: <Widget>[
Container(
color: Colors.white,
margin: const EdgeInsets.only(bottom: 16.0),
child: Column(
children: <Widget>[
Container(
height: screenHeight / 12,
padding: const EdgeInsets.all(8),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
width: 32.0,
height: 32.0,
child: Placeholder(),
),
SizedBox(width: 8.0),
SizedBox(
width: 64.0,
height: 32.0,
child: Placeholder(),
),
Spacer(),
SizedBox(
width: 32.0,
height: 32.0,
child: Placeholder(),
),
],
),
),
Container(
padding: const EdgeInsets.all(8),
height: screenHeight / 3,
child: Placeholder(),
),
Container(
padding: const EdgeInsets.all(8),
height: screenHeight / 12,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0, horizontal: 16.0),
child: Placeholder(),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0, horizontal: 16.0),
child: Placeholder(),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0, horizontal: 16.0),
child: Placeholder(),
),
),
],
),
),
],
),
),
Container(
color: Colors.white,
margin: const EdgeInsets.only(bottom: 16.0),
child: Column(
children: <Widget>[
Container(
height: screenHeight / 12,
padding: const EdgeInsets.all(8),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
width: 32.0,
height: 32.0,
child: Placeholder(),
),
SizedBox(width: 8.0),
SizedBox(
width: 64.0,
height: 32.0,
child: Placeholder(),
),
Spacer(),
SizedBox(
width: 32.0,
height: 32.0,
child: Placeholder(),
),
],
),
),
Container(
padding: const EdgeInsets.all(8),
height: screenHeight / 3,
child: Placeholder(),
),
Container(
padding: const EdgeInsets.all(8),
height: screenHeight / 12,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0, horizontal: 16.0),
child: Placeholder(),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0, horizontal: 16.0),
child: Placeholder(),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0, horizontal: 16.0),
child: Placeholder(),
),
),
],
),
),
],
),
)
],
),
),
);
}
Container _buildHeaderTabs(double screenHeight) {
return Container(
height: screenHeight / 12,
child: Row(
children: <Widget>[
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 4.0,
color: const Color.fromARGB(255, 59, 89, 152),
),
),
),
padding:
const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Placeholder(
color: Color.fromARGB(255, 59, 89, 152),
strokeWidth: 4.0,
),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 4.0,
color: Colors.white,
),
),
),
padding:
const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Placeholder(
strokeWidth: 4.0,
),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 4.0,
color: Colors.white,
),
),
),
padding:
const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Placeholder(
strokeWidth: 4.0,
),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 4.0,
color: Colors.white,
),
),
),
padding:
const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Placeholder(
strokeWidth: 4.0,
),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 4.0,
color: Colors.white,
),
),
),
padding:
const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Placeholder(
strokeWidth: 4.0,
),
),
),
],
),
);
}
Container _buildAppBar(double screenHeight, BuildContext context) {
return Container(
color: Color.fromARGB(255, 59, 89, 152),
padding: EdgeInsets.symmetric(horizontal: 16.0),
height: screenHeight / 12,
child: Row(
children: <Widget>[
SizedBox(
width: 32.0,
height: 32.0,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
Navigator.of(context).pop();
},
child: Placeholder(color: Colors.white)),
),
SizedBox(width: 16.0),
SizedBox(
width: 128.0,
height: 32.0,
child: Placeholder(color: Colors.white)),
Spacer(),
SizedBox(
width: 32.0,
height: 32.0,
child: Placeholder(color: Colors.white)),
SizedBox(width: 16.0),
SizedBox(
width: 32.0,
height: 32.0,
child: Placeholder(color: Colors.white)),
],
),
);
}
Positioned _buildBottomActions(BuildContext context, double screenHeight) {
return Positioned(
bottom: 0,
left: 0,
width: MediaQuery.of(context).size.width,
child: Container(
color: Color.fromARGB(225, 59, 89, 152),
height: screenHeight / 12,
child: Row(
children: <Widget>[
Expanded(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Placeholder(color: Colors.white),
),
),
Expanded(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Placeholder(color: Colors.white),
),
),
Expanded(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Placeholder(color: Colors.white),
),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment