[Flutter] Creating, Importing & Using Dynamic Widgets from Other Files in a Flutter Application
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的更多相关文章
- 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 数据文 ...
- Flutter 即学即用系列博客——08 MethodChannel 实现 Flutter 与原生通信
背景 前面我们讲了很多 Flutter 相关的知识点,但是我们并没有介绍怎样实现 Flutter 与原生的通信. 比如我在 Flutter UI 上面点击了一个按钮,我希望原生做一些处理,那么原生怎么 ...
- [转]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 ...
- 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 ...
- [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 ...
- Flutter 即学即用系列博客——02 一个纯 Flutter Demo 说明
前言 上一篇文章我们搭建好了 Flutter 的开发环境. Flutter 即学即用--01 环境搭建 这一篇我们通过 Flutter 的一个 Demo 来了解下 Flutter. 开发系统:MAC ...
- 【Flutter 1-1】8个Flutter的优势以及为什么要在下一个项目中尝试Flutter
首发链接 让我们一起来了解Flutter与其他跨平台框架的优势,以及这些优势在开发流程中的作用. Flutter是什么 Flutter的优势 1. 跨平台使用相同的UI和业务逻辑 2. 节省开发时间 ...
- Flutter 异常处理之图片篇
背景 说到异常处理,你可能直接会认为不就是 try-catch 的事情,至于写一篇文章单独来说明吗? 如果你是这么想的,那么本篇说不定会给你惊喜哦~ 而且本篇聚焦在图片的异常处理. 场景 学以致用,有 ...
- Flutter的原理及美团的实践
导读 Flutter是Google开发的一套全新的跨平台.开源UI框架,支持iOS.Android系统开发,并且是未来新操作系统Fuchsia的默认开发套件.自从2017年5月发布第一个版本以来,目前 ...
随机推荐
- 6.flume实战(三)※
需求:将A服务器上的日志实时采集到B服务器上面去 大致原理: 技术选型: exec source + memory channel + avro sink avro source + memory c ...
- [ 总结 ] vsftpd 虚拟用户配置
需求:在不更改目录权限的前提下,ftp用户对目录里的文件拥有所有权. [root@server2 ~]# yum install vsftpd -y [root@server2 ~]# cd /etc ...
- Centos下yum update与yum upgrade的区别
转载来源于:http://www.cnblogs.com/EasonJim/p/9026357.html 说明:生产环境对软件版本和内核版本要求非常精确,别没事有事随便的进行yum update操作! ...
- JavaScript变量、数据类型、函数
#转载请留言联系 说在前面: JavaScript 是一种弱类型语言,javascript的变量类型由它的值来决定. JavaScript语句的末尾用 ; 结束.空格没有特殊意义. 1.JavaScr ...
- 在表达式和脚本中将bean实例暴露出来
默认情况下,在activiti.cfg.xml中的所有bean和Spring配置文件中的所有bean都可以用于表达式和脚本.如果要限制配置文件中的bean的可见性,可以在流程引擎配置文件中配置一个名为 ...
- [libGDX游戏开发教程]使用libGDX进行游戏开发(12)-Action动画
前文章节列表: 使用libGDX进行游戏开发(11)-高级编程技巧 使用libGDX进行游戏开发(10)-音乐音效不求人,程序员也可以DIY 使用libGDX进行游戏开发(9)-场景过渡 ...
- 山东省第八届省赛 A:Return of the Nim(尼姆+威佐夫)
Problem Description Sherlock and Watson are playing the following modified version of Nim game: Ther ...
- HDU 6362.oval-and-rectangle-数学期望、微积分 (2018 Multi-University Training Contest 6 1001)
2018 Multi-University Training Contest 6 6362.oval-and-rectangle 题意就是椭圆里画内接矩形,问你矩形周长的期望. 比赛的时候推了公式,但 ...
- Python的异步编程[0] -> 协程[0] -> 协程和 async / await
协程 / Coroutine 目录 生产者消费者模型 从生成器到异步协程– async/await 协程是在一个线程执行过程中可以在一个子程序的预定或者随机位置中断,然后转而执行别的子程序,在适当的时 ...
- 洛谷——P1009 阶乘之和
P1009 阶乘之和 题目描述 用高精度计算出S=1!+2!+3!+…+n!(n≤50) 其中“!”表示阶乘,例如:5!=5*4*3*2*1. 输入输出格式 输入格式: 一个正整数N. 输出格式: 一 ...