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)
)
],
)
)
),
);
}
}

最新文章

  1. css动画
  2. 未能加载文件或程序集“projectname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。
  3. ibm v3700
  4. JavaScript学习笔记3之 数组 &amp; arguments(参数对象)&amp; 数字和字符串转换 &amp; innerText/innerHTML &amp; 鼠标事件
  5. regsvr32的使用
  6. 利用WCF技术降低系统之间的耦合度
  7. 关于file的上传文件
  8. CSS隐藏元素的几种方法
  9. C#:实现接口中定义的事件
  10. Qt中QObject中的parent参数
  11. 常用的CSS Hack技术集锦
  12. 移动端适配方案以及rem和px之间的转换
  13. Leetcode_141_Linked List Cycle
  14. BZOJ_3669_[Noi2014]魔法森林_LCT
  15. 利用SQL Profiler 追踪数据库操作
  16. UE4游戏开发基础命令
  17. (二)Java工程化--Maven实践
  18. Kafka技术内幕 读书笔记之(四) 新消费者——消费者提交偏移量
  19. 04单例模式Singleton
  20. Phoenix系列:二级索引(1)

热门文章

  1. TCP的三次握手和四次挥手+TCP和UDP的区别
  2. (一)lua基础语法
  3. 《JavaScript模式》精要
  4. JavaScript的for循环语句
  5. python中的迭代器详解
  6. python中deque类详解
  7. 【hdoj_2037】今年暑假不AC
  8. Fedora27安装宝塔linux面板出现/usr/lib/rpm/redhat/redhat-hardened-cc1找不到的错误
  9. React Native - 3 View, Text简介以及onPress &amp; onLongPress事件
  10. thinkphp函数学习(1)——header, get_magic_quotes_gpc, array_map, stripslashes, stripslashes_deep