首页 > 其他分享 >使用 Flutter 构建 Facebook Clone UI

使用 Flutter 构建 Facebook Clone UI

时间:2023-11-08 16:36:26浏览次数:39  
标签:avatarUrl Color Clone children color UI child Facebook Flutter

在本教程中,我将向您展示如何使用 Flutter 构建 Facebook Clone UI,因此这里是源代码。

该项目将需要许多文件,一个用于应用程序,3 个用于某些自定义小部件,因此请确保为每个组件创建一个文件

main.dart

import 'package:bttom_sheet/actionbtn.dart';
import 'package:bttom_sheet/feedbox.dart';
import 'package:bttom_sheet/storytile.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      //i'm going to disable the debug banner
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  //Let's add the color code for our project
  Color bgBlack = Color(0xFF1a1a1a);
  Color mainBlack = Color(0xFF262626);
  Color fbBlue = Color(0xFF2D88FF);
  Color mainGrey = Color(0xFF505050);

  //Here I'm going to import a list of images that we will use for the profile picture and the storys
  List<String> avatarUrl = [
    "https://images.unsplash.com/photo-1518806118471-f28b20a1d79d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=700&q=80",
    "https://images.unsplash.com/photo-1457449940276-e8deed18bfff?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60",

 
    "https://images.unsplash.com/photo-1522075469751-3a6694fb2f61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=80",

 
    "https://images.unsplash.com/photo-1525879000488-bff3b1c387cf?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80",
  ];
  List<String> storyUrl = [
    "https://images.unsplash.com/photo-1600055882386-5d18b02a0d51?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=621&q=80",
    "https://images.unsplash.com/photo-1600174297956-c6d4d9998f14?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80",
    "https://images.unsplash.com/photo-1600008646149-eb8835bd979d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=666&q=80",
    "https://images.unsplash.com/photo-1502920313556-c0bbbcd00403?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=967&q=80",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //let's add the  bg color
      backgroundColor: bgBlack,
      //let's add the app bar
      appBar: AppBar(
        elevation: 0.0,
        backgroundColor: mainBlack,
        title: Text(
          "Facebook",
          style: TextStyle(
            color: fbBlue,
          ),
        ),
        //Now let's add the action button
        actions: [
          IconButton(
            onPressed: () {},
            icon: Icon(Icons.search),
          ),
          IconButton(
            onPressed: () {},
            icon: Icon(Icons.menu),
          ),
        ],
      ),

      //Now let's work on the body
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(8.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              //First of all we need to creat the post editor
              Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  color: mainBlack,
                  borderRadius: BorderRadius.circular(12.0),
                ),
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                      horizontal: 8.0, vertical: 10.0),
                  child: Column(
                    children: [
                      Row(
                        children: [
                          CircleAvatar(
                            radius: 25.0,
                            backgroundImage: NetworkImage(avatarUrl[0]),
                          ),
                          SizedBox(
                            width: 10.0,
                          ),
                          Expanded(
                            child: TextField(
                              style: TextStyle(
                                color: Colors.white,
                              ),
                              decoration: InputDecoration(
                                  contentPadding: EdgeInsets.only(left: 25.0),
                                  hintText: "Post something...",
                                  filled: true,
                                  fillColor: mainGrey,
                                  border: OutlineInputBorder(
                                    borderRadius: BorderRadius.circular(30.0),
                                    borderSide: BorderSide.none,
                                  )),
                            ),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 5.0,
                      ),
                      Divider(
                        color: mainGrey,
                        thickness: 1.5,
                      ),
                      //Now we will create a Row of three button
                      Row(
                        children: [
                          actionButton(
                              Icons.live_tv, "Live", Color(0xFFF23E5C)),
                          actionButton(
                              Icons.image, "Picture", Color(0xFF58C472)),
                          actionButton(Icons.insert_emoticon, "Activity",
                              Color(0xFFF8C03E)),
                        ],
                      )
                    ],
                  ),
                ),
              ),
              //We have finished the Post editor now let's create
              //the story's timeline
              // let's first create a new file for the custom widget
              //now let's buil the container
              SizedBox(
                height: 10.0,
              ),
              Container(
                height: 160.0,
                width: double.infinity,
                child: ListView(
                  scrollDirection: Axis.horizontal,
                  children: [
                    storyTile(avatarUrl[0], storyUrl[0], "Ling chang"),
                    storyTile(avatarUrl[1], storyUrl[1], "Ling chang"),

 
                    storyTile(avatarUrl[2], storyUrl[2], "Ling chang"),
                  ],
                ),
              ),
              SizedBox(
                height: 20.0,
              ),
              //Now let's create the news feed
              //first we will make the custom container of the feed
              //Ok let's test our widget
              feedBox(avatarUrl[0], "Doctor code", "6 min",
                  "I just wrote something", ""),
              feedBox(avatarUrl[1], "Joseph Joestar", "6 min",
                  "It's pretty good I like it", storyUrl[2]),
              feedBox(avatarUrl[2], "Giorno giovana", "Yesterday",
                  "I'm Giorno Giovana and I have a Dream", storyUrl[1]),
            ],
          ),
        ),
      ),
    );
  }
}

actionbtn.dart

//Now i'm going to make a custom button for the different action like the comment button, share ...
import 'package:flutter/material.dart';

//the button wil take 3 parameter : the icon , the action title and the color of the icon
Widget actionButton(IconData icon, String actionTitle, Color iconColor) {
  return Expanded(
    child: FlatButton.icon(
      onPressed: () {},
      icon: Icon(
        icon,
        color: iconColor,
      ),
      label: Text(
        actionTitle,
        style: TextStyle(
          color: Colors.white,
        ),
      ),
    ),
  );
}

storytile.dart

//here we will make our story tile
import 'package:flutter/material.dart';

//the story tile will have 3 parameter
// the avatar url, the story image url and the user name
//I forgot to add some margin between the tiles
Widget storyTile(String avatarUrl, String storyUrl, String userName) {
  return Container(
    margin: EdgeInsets.only(right: 20.0),
    height: 144.0,
    width: 120.0,
    decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12.0),
        image: DecorationImage(
          image: NetworkImage(storyUrl),
          fit: BoxFit.cover,
        )),
    child: Padding(
      padding: EdgeInsets.all(8.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          CircleAvatar(
            backgroundImage: NetworkImage(avatarUrl),
          ),
          Expanded(
            child: Container(),
          ),
          Text(
            userName,
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.w800,
              fontSize: 18.0,
            ),
          )
        ],
      ),
    ),
  );
}

feedbox.dart

import 'package:flutter/material.dart';
import 'actionbtn.dart';

//the feed box will have for parameters :
// the user name , the user avatar, the pub date, the content text and content img
Widget feedBox(String avatarUrl, String userName, String date,
    String contentText, String contentImg) {
  return Container(
    margin: EdgeInsets.only(bottom: 20.0),
    width: double.infinity,
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(12.0),
      color: Color(0xFF262626),
    ),
    child: Padding(
      padding: EdgeInsets.all(8.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              CircleAvatar(
                backgroundImage: NetworkImage(avatarUrl),
                radius: 25.0,
              ),
              SizedBox(
                width: 10.0,
              ),
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      userName,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 18.0,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    SizedBox(
                      height: 5.0,
                    ),
                    Text(
                      date,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16.0,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          SizedBox(
            height: 10.0,
          ),
          if (contentText != "")
            Text(
              contentText,
              style: TextStyle(color: Colors.white, fontSize: 16.0),
            ),
          SizedBox(
            height: 10.0,
          ),
          if (contentImg != "") Image.network(contentImg),
          SizedBox(
            height: 10.0,
          ),
          Divider(
            thickness: 1.5,
            color: Color(0xFF505050),
          ),
          Row(
            children: [
              actionButton(Icons.thumb_up, "Like", Color(0xFF505050)),
              actionButton(Icons.comment, "Reply", Color(0xFF505050)),
              actionButton(Icons.share, "Share", Color(0xFF505050)),
            ],
          )
        ],
      ),
    ),
  );
}

最后

这里也为想要学习Flutter的朋友们准备了两份学习资料《Flutter Dart语言编程入门到精通》《Flutter实战》

  • 第一章 Dart语言基础
  • 第二章 Dart 异步编程
  • 使用 Flutter 构建 Facebook Clone UI_ide

  • 第三章 异步之 Stream 详解
  • 第四章 Dart标准输入输出流
  • 使用 Flutter 构建 Facebook Clone UI_ci_02

  • 第五章 Dart 网络编程
  • 第六章 Flutter 爬虫与服务端
  • 使用 Flutter 构建 Facebook Clone UI_ide_03

  • 第七章 Dart 的服务端开发
  • 第八章 Dart 调用C语言混合编程
  • 第九章 LuaDardo中Dart与Lua的相互调用
  • 使用 Flutter 构建 Facebook Clone UI_ico_04

《Flutter实战:第二版》

  • 第一章:起步
  • 第二章:第一个Flutter应用
  • 第三章:基础组件
  • 第四章:布局类组件
  • 第五章:容器类组件

使用 Flutter 构建 Facebook Clone UI_ide_05

  • 第六章:可滚动组件
  • 第七章:功能型组件
  • 第八章:事件处理与通知
  • 第九章:动画
  • 第十章:自定义组件
  • 使用 Flutter 构建 Facebook Clone UI_ci_06

  • 第十一章:文件操作与网络请求
  • 第十二章:Flutter扩展
  • 第十三章:国际化
  • 第十四章:Flutter核心原理
  • 第十五章:一个完整的Flutter应用
  • 使用 Flutter 构建 Facebook Clone UI_ico_07

标签:avatarUrl,Color,Clone,children,color,UI,child,Facebook,Flutter
From: https://blog.51cto.com/u_16163480/8256191

相关文章

  • 免费为你的PDF创建目录-QuickOutline & Quicker
    有时候我们会遇到没有目录的PDF,但在PDF中其实是存在着目录的,如何把目录页转换成可以导航的目录呢,以下介绍一种快捷的方式,为PDF增加目录1、QuickOutline使用介绍下载链接:https://lqjs.lanzouq.com/iSQoA16omngd浏览文件,选中要添加目录的PDF(或直接拖动);写入目录文本,目录文本图......
  • Burpsuite测试神器使用
    1、Web漏洞扫描是在Web信息搜集的基础上,进行更进一步的自动化的安全评估、漏洞挖掘、渗tou测试2、Web漏洞扫描能让我们进一步挖掘目标服务器潜在的sql注入、Xss跨站脚本攻鸡、CSr跨站请求伪造等漏洞3、Web漏扫虽然能够帮助我们快速的实现自动化漏洞渗tou,但是并不是全能的,也会出现误......
  • 最全面的移动端UI组件设计详解:下篇
    上一期给大家讲解了《最全面的移动端UI组件设计详解:中篇》,主要分享了:基础组件、表单组件和反馈组件3个部分;这次给大家带来:数据展示组件和其他组件详解,希望你在设计APP、小程序、H5页面中,能熟练使用和理解各种的UI组件,今天给大家总结了关于移动端UI组件,希望可以在工作中帮到你。......
  • 使用jadx-gui反编译工具获取签名信息
     1、下载地址: github.com/skylot/jadx/releases 如图:  2、打开jadx-gui方式1:终端执行brewinstalljadx, 执行完毕后,终端再输入命令jadx-gui,即可打开jadx;方式2:解压zip包,在jadx-1.4.7文件夹下找到bin文件夹打开,双击运行jadx-gui,如图: 3、点击“打开......
  • 最全面的移动端 UI组件设计详解:上篇
    作为一名UI设计师,我们经常要进行PC端和移动端的设计任务,上一次给大家分享了:《最全Web端UI组件设计详解》,这次给大家带来移动端UI组件设计详情,尤其在我们APP、小程序、H5页面设计中,我们要使用和熟知各种的UI组件,今天给大家总结了关于移动端UI组件,希望可以在工作中帮到你。什么......
  • Arduino UNO 测试SD卡
    SD卡模块测试方法ArduinoIDE的LibraryManager中搜索‘SD’,安装最新版资料库;依次打开File-Examples-SD-CardInfo示例程序,Upload至开发板;打开串口监视器SerialMonitor,即可看到串口信息输出,如下Cardtype:SD2Clusters:15352BlocksxClu......
  • Unity 编辑器UI 杂记
     用 rootVisualElement方法绘制按钮和用 GUILayout.Button绘制按钮混用的案例usingSystem.Collections;usingSystem.Collections.Generic;usingNUnit.Framework;usingUnityEditor;usingUnityEngine;usingUnityEngine.UIElements;publicclassMyTestPanel:......
  • [-006-]-Python3+Unittest+Selenium Web UI自动化测试之悬浮窗口中的元素点击
     1.分析现状:PPT模板悬浮出现悬浮窗口悬浮窗口中分为4大类:PPT模板,PPT模板页,PPT关系图,PPT图表大类下存在小类点击可跳转但是此页面里还存在PPT模板下的总结汇报等此种情况的元素此情况如果仅用text定位是无法定位到的所以排除了text定位方式2.解决方法:首先我们看下悬浮窗......
  • Web Profile Builder for Web Application Projects
    WebProfileBuilderforWebApplicationProjectsFilescanbedownloadedfromtheWebProfileBuilderprojectpage.IfyouuseWebApplicationProjects,youhaveprobablyrunintotheissueofnotbeingabletoaccesstheProfileatdesigntime.Thankfully......
  • C#多线程与UI响应
    一、概述在使用C#进行应用程序设计时,经常会采用多线程的方式进行一些后台任务的工作。对于不同的应用场景,使用的策略也不尽相同。1.  后台循环任务,少量UI更新:例如批量上传文件,并提供进度。这种情况使用BackgroundWorker组件是非常好的选择。2.  耗时的后......