In this lesson we’ll learn how to import widgets we’ve created in other files & use them in our project. We'll also look at how to create dynamic properties in our widgets in order to make them reusable across our application.

We have the CLI generate code:

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(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
brightness: Brightness.dark,
primaryTextTheme: TextTheme(
title: TextStyle(
color: Colors.pinkAccent
)
),
primarySwatch: Colors.deepPurple,
),
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: ,
height: ,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular()
),
child: Text(
"Hello Flutter",
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.w500,
fontSize: 22.0
)
)
)
],
)
)
),
);
}
}

We want to replace the highlighted part with reusable Widget.

import 'package:flutter/material.dart';

class Greeting extends StatelessWidget {
// To get passed in arg
Greeting({
@required this.greeting,
this.color = Colors.green
});
// need to create a variable to hold greeting
final String greeting;
final Color color;
@override
Widget build(BuildContext context) {
return Text(
this.greeting,
style: TextStyle(
color: this.color,
fontSize:
)
);
}
}

Use it:

import 'package:flutter/material.dart';
import 'package:my_flutter_app/Greeting.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
brightness: Brightness.dark,
primaryTextTheme: TextTheme(
title: TextStyle(
color: Colors.pinkAccent
)
),
primarySwatch: Colors.deepPurple,
),
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: ,
height: ,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular()
),
child: Greeting(greeting: "Hey you!", color: Colors.blue)
)
],
)
)
),
);
}
}

[Flutter] Creating, Importing & Using Dynamic Widgets from Other Files in a Flutter Application的更多相关文章

  1. 17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files

    17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files 如果数据库是大的, 复制raw 数据文 ...

  2. Flutter 即学即用系列博客——08 MethodChannel 实现 Flutter 与原生通信

    背景 前面我们讲了很多 Flutter 相关的知识点,但是我们并没有介绍怎样实现 Flutter 与原生的通信. 比如我在 Flutter UI 上面点击了一个按钮,我希望原生做一些处理,那么原生怎么 ...

  3. [转]Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-a ...

  4. Creating a new dynamic form project, business modeling.

    The domain logic is like there are a bunch of objects, as well as a lot of configurations, according ...

  5. [Flutter] Creating & Updating State in a Flutter Application

    To create a Stateful widget: 1. Create a StatefulWidget 2. Create a State class SGreeting extends St ...

  6. Flutter 即学即用系列博客——02 一个纯 Flutter Demo 说明

    前言 上一篇文章我们搭建好了 Flutter 的开发环境. Flutter 即学即用--01 环境搭建 这一篇我们通过 Flutter 的一个 Demo 来了解下 Flutter. 开发系统:MAC ...

  7. 【Flutter 1-1】8个Flutter的优势以及为什么要在下一个项目中尝试Flutter

    首发链接 让我们一起来了解Flutter与其他跨平台框架的优势,以及这些优势在开发流程中的作用. Flutter是什么 Flutter的优势 1. 跨平台使用相同的UI和业务逻辑 2. 节省开发时间 ...

  8. Flutter 异常处理之图片篇

    背景 说到异常处理,你可能直接会认为不就是 try-catch 的事情,至于写一篇文章单独来说明吗? 如果你是这么想的,那么本篇说不定会给你惊喜哦~ 而且本篇聚焦在图片的异常处理. 场景 学以致用,有 ...

  9. Flutter的原理及美团的实践

    导读 Flutter是Google开发的一套全新的跨平台.开源UI框架,支持iOS.Android系统开发,并且是未来新操作系统Fuchsia的默认开发套件.自从2017年5月发布第一个版本以来,目前 ...

随机推荐

  1. [ Python - 8 ] 简单demo练习

    练习1: 利用os模块编写一个能实现dir -l输出的程序(注意:dir /l是windows下命令) #!_*_coding:utf-8_*_ # Author: hkey import os, t ...

  2. docker从零开始网络(四 ) host网络

    使用主机网络 如果host对容器使用网络驱动程序,则该容器的网络堆栈不会与Docker主机隔离.例如,如果您运行绑定到端口80 host的容器并使用网络,则容器的应用程序将在主机IP地址的端口80上可 ...

  3. ubantu启动盘制作

    转载自http://jingyan.baidu.com/article/b24f6c82cf50e086bfe5dae9.html 1 首先打开UltraISO软件,没有的请百度搜索,下载安装,尽量下 ...

  4. ganglia笔记:rrds目录

    因为原来的rrds目录挂载的盘不够用了,所以想修改一下目录,遇到权限的问题. 在一个owner为user的目录下面新建了./ganglia/rrds目录,并且./ganglia及其子文件夹都修改为ga ...

  5. BigDecimal不整除异常

    通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常的 异   常 :java.lang.ArithmeticException: Non-terminatin ...

  6. Dumpzilla工具第615行bug的解决办法

    Dumpzilla工具第615行bug的解决办法   在Dumpzilla使用选项frequency时,会提示SQL语法错误.这是由于其中SQL语句编写错误.需要将615行中: where url l ...

  7. 【spfa】bzoj1295 [SCOI2009]最长距离

    题意:给你一个n*m的点阵.有些点是障碍,求一个欧几里得距离最大的点对(A,B),使得在移走的障碍≤T的情况下,可以从A走到B. 建图,跑n*m次spfa,求出从 每个点 出发到 其他所有点 的 经过 ...

  8. 【离散化】【扫描线】CH Round #59 - OrzCC杯NOIP模拟赛day1 队爷的新书

    //上图绿色扫描线右侧少画了一条扫描线. 很多区间把数轴分成了很多段,看哪个点的(区间覆盖数*该点权值)最大. 显然在某个区间的右端点的答案是最优的. 排序后 用扫描线从左到右扫描,维护每个点的覆盖数 ...

  9. JavaScript学习系列之执行上下文与变量对象篇

    一个热爱技术的菜鸟...用点滴的积累铸就明日的达人 正文 在上一篇文章中讲解了JavaScript内存模型,其中有提到执行上下文与变量对象的概念.对于JavaScript开发者来说,理解执行上下文与变 ...

  10. Swift,类的调用

    1.class或者struct如果没有实例没办法直接调用 (1)设置class后可直接调用 class S{ static var width:Int{ //static代表公有的,都可使用 retu ...