[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月发布第一个版本以来,目前 ...
随机推荐
- 【bzoj4272】筐子放球
看题解会的系列…… 详细解释先坑着,以后补…… #include<bits/stdc++.h> #define N 200005 using namespace std; ,tot=,cn ...
- 【C++】各种成员变量
来自:黄邦勇帅 const 常量对象: 即把对象声明为常量,即 const hyong m,常量对象不能调用可能改变对象的值的函数,因此常量对象只能调用类中的 const 常量函数,因为不是 cons ...
- selenium 3.0与2.0 打开火狐浏览器的区别
3.0的selenium需要引入gecko.driver驱动 ,因为没有在系统环境path中配置相关路径,因此需要特别指出,为了方便使用,建议直接和火狐安装包中的.exe文件放在同一目录下. 2.0的 ...
- PEP 3106 -- Revamping(改进) dict.keys(), .values() and .items()
1. Abstract(摘要) This PEP proposes(建议) to change the .keys(), .values() and .items() methods of the b ...
- 高速备份还原MYSQL数据库
#安装依赖包yum -y update gccyum -y install gcc+ gcc-c++ #安装cd /usr/local/softwaretar -jxvf p7zip_16.02_sr ...
- [libGDX游戏开发教程]使用Libgdx进行游戏开发(5)-关卡加载
在上一章我们介绍了如何管理和利用素材,但是我们注意到,这些素材都是零散的,比如岩石的左部等,这一章,我们将利用这些零件拼合成完整的游戏对象. 回顾最开始的设计类图,注意Level类和所有Level中的 ...
- Spring的自动装配
在Spring中对自定义的引用类型注入时可以实现自动赋值.但是必须依赖set方法: 自动装配功能有两种: <!-- autowire:"byType" --根据class匹 ...
- 【互动问答分享】第5期决胜云计算大数据时代Spark亚太研究院公益大讲堂
Spark亚太研究院100期公益大讲堂 [第5期互动问答分享] Q1:spark怎样支持即席,应该不是spark sql吧,是hive on spark么? Spark1.0 以前支持即席查询的技术是 ...
- [bootstrap] 修改字体
file: variable.less @font-family-sans-serif: "Helvetica Neue", Helvetica, Arial, sans-seri ...
- CentOS下MySQL主从复制,读写分离
1.环境:所有系统都是CentOS5.5 mysql-5.6.31-2.el5,MySQL中都没有数据 主服务器IP为192.168.128.230 从服务器IP为192.168.128.235 代理 ...