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. win端git连接私服仓库+上传本地项目+从服务器下载文件到win

    win端git连接私服仓库: 1.win端 检查c:/Users/用户/.ssh/目录下是否有config文件(!!!没有任何后缀名).如果没有则新建config文件,然后修改添加如下内容: Host ...

  2. vue+axios下载文件的实现

    HTML代码: <el-button size="medium" @click="download">下载表格</el-button> ...

  3. 浏览器内核测试 v0.99

    操作系统:  Windows 7 浏览器内核: WebKit 内核(Chrome) 内核版本是: WebKit 537.36( Chrome 49.0.2593.0 S) 完整代码: Mozilla/ ...

  4. 【C++】指针和new相关

    看黄邦勇帅的笔记. 指针和new之前觉得已经掌握的很好了,可是看了资料还是get到了新知识.记录一下. 1.指针只支持 4 种算术运算符:++,――,+,-.指针只能与整数加减.指针运算的原则是:每当 ...

  5. zabbix通过snmp监控vmware vpshere5.5

    https://www.iyunv.com/thread-516343-1-1.html

  6. SnowNLP:一个处理中文文本的 Python 类库[转]

    SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和Te ...

  7. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  8. JDK/Java里的设计模式

    JDK/Java里的设计模式

  9. luogu P2134 百日旅行

    题目链接 luogu P2134 百日旅行 题解 dp方程好想吧 优化有些玄学惹 不会证.... 不过我会三分和贪心 \滑稽 但还是写dp吧 代码 #include<cstdio> #in ...

  10. [HDU5739]Fantasia(圆方树DP)

    题意:给一张无向点带有权无向图.定义连通图的权值为图中各点权的乘积,图的权值为其包含的各连通图的权和.设z_i为删除i点后图的权值,求$S = (\sum\limits_{i=1}^{n}i\cdot ...