首页 > 其他分享 >前端学习-flutter学习-006-包管理

前端学习-flutter学习-006-包管理

时间:2024-07-09 23:19:30浏览次数:7  
标签:text number 学习 build context flutter 路由 006

《Flutter实战·第二版》

简介

类似node的npm
配置文件是pubspec.yaml
name:应用或包名称。
description: 应用或包的描述、简介。
version:应用或包的版本号。
dependencies:应用或包依赖的其他包或插件。
dev_dependencies:开发环境依赖的工具包(而不是flutter应用本身依赖的包)。
flutter:flutter相关的配置选项。

name: flutter01
description: "A new Flutter project."
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+1

environment:
  sdk: '>=3.4.3 <4.0.0'

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.6

dev_dependencies:
  flutter_test:
    sdk: flutter

  # The "flutter_lints" package below contains a set of recommended lints to
  # encourage good coding practices. The lint set provided by the package is
  # activated in the `analysis_options.yaml` file located at the root of your
  # package. See that file for information about deactivating specific lint
  # rules and activating additional ones.
  flutter_lints: ^3.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #   - images/a_dot_burr.jpeg
  #   - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

示例(依赖Pub仓库)

在pubspec.yaml中增加,并且点击右上角的Pub get 按钮,会添加新的依赖

dependencies:
  flutter:
    sdk: flutter
  # 新添加的依赖
  english_words: ^4.0.0

在使用时,则需要引入包import 'package:english_words/english_words.dart';

import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

// import 'package:flutter/cupertino.dart';
// void main() {
//   runApp(const MyApp());
// }

void main() => runApp(const MyApp());
// void main() => runApp(CupertinoApp(home: CupertinoTestRoute()));

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      initialRoute:"/", //名为"/"的路由作为应用的home(首页)
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      routes:{
        "new_page":(context) => TipRoute(text:'这个参数是配置路由传过来的'),
        "/":(context) => RouterTestRoute(), //注册首页路由
      },
    );
  }
}

class TipRoute extends StatelessWidget {
  TipRoute({
    Key? key,
    required this.text,  // 接收一个text参数
  }) : super(key: key);
  final String text;

  @override
  Widget build(BuildContext context) {
    //获取路由参数
    var args=ModalRoute.of(context)?.settings.arguments;
    print('旧页面传递参数的方法2:arguments:$args');
    return Scaffold(
      appBar: AppBar(
        title: Text("提示"),
      ),
      body: Padding(
        padding: EdgeInsets.all(18),
        child: Center(
          child: Column(
            children: <Widget>[
              Text(text),
              ElevatedButton(
                onPressed: () => Navigator.pop(context, "这个数据是新页面传递给旧页面的"),
                child: Text("返回"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class RouterTestRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("初始页面"),
      ),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () async {
                // 打开`TipRoute`,并等待返回结果
                // var result = await Navigator.push(
                //   context,
                //   MaterialPageRoute(
                //     builder: (context) {
                //       return TipRoute(
                //         // 路由参数
                //         text: "这个数据是旧页面传递给新页面的",
                //       );
                //     },
                //   ),
                // );
                var result = await Navigator.pushNamed(context, "new_page",  arguments: "hi");
                //输出`TipRoute`路由返回结果
                print("路由返回值: $result");
              },
              child: Text("打开提示页"),
            ),
            RandomWordsWidget(),
          ],
        ),
      ),
    );


  }
}

class RandomWordsWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 生成随机字符串
    final wordPair = WordPair.random();
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Text('这是随机的英文字符串:' + wordPair.toString()),
    );
  }
}

其他依赖方式

  • 依赖本地包 路径可以是相对的,也可以是绝对的。
dependencies:
	pkg1:
        path: ../../code/pkg1
  • 依赖Git:依赖存储在Git仓库中的包。
// 如果在仓库根目录中
dependencies:
  pkg1:
    git:
      url: git://github.com/xxx/pkg1.git
// 如果不在仓库根目录中
dependencies:
  package1:
    git:
      url: git://github.com/flutter/packages.git
      path: packages/package1      

标签:text,number,学习,build,context,flutter,路由,006
From: https://www.cnblogs.com/ayubene/p/18292904

相关文章

  • 前端学习-flutter学习-005-路由管理
    《Flutter实战·第二版》简单示例-跳转页面import'package:flutter/material.dart';import'dart:ui';//import'package:flutter/cupertino.dart';//voidmain(){//runApp(constMyApp());//}voidmain()=>runApp(constMyApp());//v......
  • Django学习第六天
     启动项目命令pythonmanage.pyrunserver取消模态框功能js实现列表数据删除第二种实现思路使用jquery修改模态框标题编辑页面拿到数据库数据显示默认数据功能实现想要去数据库中获取数据时:对象/字典三种不同的数据类型使用Ajax传入数据实现表单编辑,删除,修改......
  • 数据结构——并查集 学习笔记
    数据结构——并查集学习笔记并查集是一种用于管理元素所属集合的数据结构,实现为一个森林。并查集中,每棵树表示一个集合,树中的节点表示对应集合中的元素。其思想是,把集合属性绑定到根节点上,避免多余的处理,因此一般难以分离。普通并查集并查集支持两种操作:合并(Union):合并两个元素......
  • 「学习笔记」数位DP
    虽然叫DP,但是基本所有数位DP题我们都可以用好打好想好理解的记忆化搜索来做。记搜模板有一个大致的记忆化搜索模板,AKALL数位DPintdfs(intlen,boollead,boollimit,...){ if(!len)return1;//len=0,即所有位都搜完 if(~f[len][lead][limit]...)returnf[l......
  • 下降幂学习笔记
    下降幂学习笔记还原精灵还我笔记——来自打完笔记但关电脑前没有保存的某人的呐喊。定义下降幂就是形如\(n^{\underline{m}}\)的式子,表示\[n^{\underline{m}}=\prod_{i=n-m+1}^{n}=\frac{n!}{(n-m)!}\]同理声明一个上升幂\(n^{\overline{m}}\),表示\[n^{\overline{m}}=\pr......
  • 比赛获奖的武林秘籍:05 电子计算机类比赛国奖队伍技术如何分工和学习内容
    比赛获奖的武林秘籍:05电子计算机类比赛国奖队伍技术如何分工和学习内容摘要本文主要介绍了在电子计算机类比赛中技术层面上的团队分工和需要学习的内容,分为了嵌入式硬件、嵌入式软件、视觉图像处理、机械、上位机软件开发和数据分析等六个方向,并结合自身经历给出相关建议。正......
  • 学习老算法,争做老东西
    目录DancingLinksDancingLinks考虑这样一个问题:给定一个\(N\)行\(M\)列的矩阵,矩阵中每个元素要么是\(1\),要么是\(0\)。你需要在矩阵中挑选出若干行,使得对于矩阵的每一列\(j\),在你挑选的这些行中,有且仅有一行的第\(j\)个元素为\(1\)。这类问题统称为精确覆盖问......
  • 设计模式学习(二)工厂模式——抽象工厂模式+注册表
    目录前言使用简单工厂改进使用注册表改进参考文章前言在上一篇文章中我们提到了抽象工厂模式初版代码的一些缺点:①客户端违反开闭原则②提供方违反开闭原则。本文将针对这两点进行讨论使用简单工厂改进对于缺点①,我们可以使用简单工厂的思路来改进抽象工厂的初版代码。对于上......
  • 信创学习笔记(二),信创之CPU芯片架构思维导图
    创作不易只因热爱!!热衷分享,一起成长!“你的鼓励就是我努力付出的动力”各架构,操作系统,指令,代表生产商,服务器使用产品主要供应商......
  • 信创学习笔记(一),信创内容思维导图
    创作不易只因热爱!!热衷分享,一起成长!“你的鼓励就是我努力付出的动力”用一张图归纳学习信创内容信创内容思维导图......