[Flutter] Creating, Importing & Using Dynamic Widgets from Other Files in a Flutter Application

Flutter中创建与使用可复用Widget
本文详细介绍了在Flutter开发中如何创建并使用可复用的Widget,通过实例展示了如何将特定部分替换成可复用的Widget,以及如何传递参数给这些Widget,使得它们能在整个应用中重复使用。

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: 300,
                height: 300,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Colors.black,
                  borderRadius: BorderRadius.circular(500)
                ),
                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: 32
      )
    );
  }
}

 

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: 300,
                height: 300,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Colors.black,
                  borderRadius: BorderRadius.circular(500)
                ),
                child: Greeting(greeting: "Hey you!", color: Colors.blue)
              )
            ],
          )
        )
      ),
    );
  }
}

 

转载于:https://www.cnblogs.com/Answer1215/p/10299612.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值