春天前销销售 - 保存一切。所有视频。所有书籍。 现在50%的折扣。

建立您的移动发展技能并保存!通过终极书籍和视频订阅,继续前进。作为春季前销销售的一部分,仅为149美元/年。

首页 扑波教程

开奖结果3d扑扑应用:入门

了解如何通过样式窗口小部件,创建动态开奖结果3d,并在可用开奖结果3d之间切换来使应用程序脱颖而出。

4.2/5 5个评分

版本

  • Dart 2.10,Flutter 1.22,Android Studio 4.2

扑腾的一个巨大特征是它使您可以为您提供屏幕上每个像素的绝对控制。这允许开发人员在没有妥协的情况下实现设计师的愿景。

如今,香草小部件看起来有点沉闷。自定义窗口小部件为整个UI提供层次结构,方向性和结构 - 帮助用户获取和参与。

在本教程中,您将将样式应用于小部件,集中应用开奖结果3d并在不同开奖结果3d之间动态切换。

在此过程中,您将学习如何:

  • 样式特定小部件。
  • 为您的应用程序使用整体开奖结果3d。
  • 在光线和黑暗开奖结果3d之间切换。

现在,现在是时候用美丽的开奖结果3d向你的项目装修!

笔记:本教程假设您熟悉扑腾的基础知识。如果你是新的颤动,请检查这一点 教程和这种更深入的 浮动UI小部件 tutorial.

入门

点击下载Starter项目 下载材料 按钮在教程的顶部或底部。然后,在Android Studio打开Starter项目,您将在那里找到 骑士与白天,用于值班骑士的活动记录应用程序。

建立和运行。您将看到以下屏幕:

vanilla小部件样式

在这一点 首页 屏幕,你会看到你作为骑士的不同活动的计数,已经记录。按下与其中一个活动相对应的按钮递增活动计数。

即使该应用程序有效,它看起来很平淡。您将使用不同的方法为小部件提供一些个性,使您的应用程序从生态系统中的所有其他人脱颖而出。 :]

在开始编码之前,请花点时间审查一些关于造型和开奖结果3d的背景以及它们为什么重要。

什么是开奖结果3d?

开奖结果3d是在您的应用中使用一组颜色,字体,形状和设计样式的过程。这是一种在一个地方集中所有风格决策的方法。

由于大多数小部件共享类似的样式,因此您不想单独使用窗口小部件。相反,您应该在一个地方定义样式,然后重用它们。这将保持Codebase Dry(不要重复自己),可读且易于维护。

这些是在开发应用程序时要记住的最佳实践。如果这现在没有意义,请不要担心!您将从地面上的开奖结果3d应用程序,快速实现这种方式组织风格的好处。

在飘飘上的开奖结果3d

Most visual widgets in Flutter have a style property whose type varies depending on the widget. Examples include TextStyle for the Text widget or ButtonStyle for the Button widget. Specifying a style will only affect that specific widget.

The idiomatic approach to styling components in Flutter is to add Theme widgets to the widget hierarchy. The higher level Material and Cupertino libraries even provide their own baked in themes to match the design languages they are implementing.

The Theme widget automatically applies its style to all descendant widgets. It takes a ThemeData argument, which holds the actual definition for the colors and font styles. If you look into the Flutter source code, you’ll even notice that a Theme widget just uses an InheritedWidget under the hood to distribute the ThemeData throughout the widget tree.

在颤动中施加开奖结果3d非常简单。你会看到接下来怎么做!

笔记: Descendant widgets can obtain the current theme’s ThemeData using Theme.of. This is useful for making one-off variations based on the inherited style using copyWith to overwrite attributes.

造型小部件

您将学习的第一件事是如何彼此独立地进行特定的小部件。

打开 lib / home / home_page.dart and go to the build method. There are three plain RaisedButton widgets. Your first task is to add shape and color attributes to style the buttons.

To start, replace the build method with the following:

@override
Widget build(BuildContext context) {
  final totalActivities = _joustCounter + _breakCounter + _patrolCounter;
  return Scaffold(
    appBar: CustomAppBar(
      title: 'Knight and Day',
    ),
    body: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        BarChart(
          joustCounter: _joustCounter,
          breakCounter: _breakCounter,
          patrolCounter: _patrolCounter,
        ),
        const SizedBox(
          height: 32.0,
        ),
        Text('You\'ve done $totalActivities activities in total'),
        const SizedBox(
          height: 32.0,
        ),
        RaisedButton(
          child: const Text('Joust'),
          onPressed: () => setState(() { _joustCounter++; }),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18.0)),
          color: CustomColors.lightPurple,
        ),
        RaisedButton(
          child: const Text('Take break'),
          onPressed: () => setState(() { _breakCounter++; }),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18.0)),
          color: CustomColors.lightPurple,
        ),
        RaisedButton(
          child: const Text('Patrol'),
          onPressed: () => setState(() { _patrolCounter++; }),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18.0)),
          color: CustomColors.lightPurple,
        ),
      ],
    ),
  );
}

Now all of the RaisedButton widgets have a shape attributes to make them into rounded rectangles and they’ve been given a light purple color

建立并运行,您会看到您的新花式按钮。

风格按钮

笔记: You’ve probably noticed that you’re using a class called CustomColors from lib / theme / colors.dart. Following the DRY principle, this is a class that simply holds static values for the different colors you’ll use in this tutorial. If you need to change a color for any reason, instead of going through your entire codebase and changing each individual value, you can open CustomColors and simply change it there.

提供整体开奖结果3d

You’ve seen how easy it’s to adjust the styling of a couple of widgets. When the number of widgets you want to style grows, however, it can become cumbersome to update all of those widgets when you want to change your apps style. Ideally, you want to have a single place that defines styles for your whole app. Luckily, you can achieve this by setting a Theme for your MaterialApp!.

创建一个新文件 lib /开奖结果3d,说出它 custom_theme.dart. 并将以下代码添加到空文件:

import 'package:flutter/material.dart';

import 'colors.dart';

class CustomTheme {
  static ThemeData get lightTheme { //1
    return ThemeData( //2
      primaryColor: CustomColors.purple,
      scaffoldBackgroundColor: Colors.white,
      fontFamily: 'Montserrat', //3
      buttonTheme: ButtonThemeData( // 4
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18.0)),
        buttonColor: CustomColors.lightPurple,
      )
    );
  }
}

您可能已经有大多数代码的挂起。打破它:

  1. 您正在提供一个全球可访问的静态吸气厂。你稍后会用它。
  2. You’re building your actual custom ThemeData here. Notice the number of attributes you’ll override — this is just a handful of the possibilities.
  3. 您还定义了默认情况下的字体系列。
  4. 在这里,您可以定义按钮样式,类似于您以前所做的内容 lib / home / home_page.dart.

现在,唯一剩下的事情是应用开奖结果3d。打开 lib / main.dart. 文件并用以下内容替换文件的内容:

import 'package:flutter/material.dart';
import 'package:knight_and_day/home/home_page.dart';
import 'package:knight_and_day/theme/custom_theme.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Knight and Day',
      home: HomePage(),
      theme: CustomTheme.lightTheme,
    );
  }
}

The only thing that’s actually changed is you’re using the theme attribute of MaterialApp and supplying the custom theme you created in the previous step.

构建并运行,您将看到字体系列在全球化。

光线开奖结果3d

Now that you’ve setup a global theme for your apps colors, text, and button styles you’ll want to remove the one off styles you added to the RaisedButtons earlier. Revert the lib / home / home_page.dart build 方法回到其原始代码:

@override
Widget build(BuildContext context) {
  final totalActivities = _joustCounter + _breakCounter + _patrolCounter;
  return Scaffold(
    appBar: CustomAppBar(
      title: 'Knight and Day',
    ),
    body: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        BarChart(
          joustCounter: _joustCounter,
          breakCounter: _breakCounter,
          patrolCounter: _patrolCounter,
        ),
        const SizedBox(
          height: 32.0,
        ),
        Text('You\'ve done $totalActivities activities in total'),
        const SizedBox(
          height: 32.0,
        ),
        RaisedButton(
          child: const Text('Joust'),
          onPressed: () => setState(() { _joustCounter++; }),
        ),
        RaisedButton(
          child: const Text('Take break'),
          onPressed: () => setState(() { _breakCounter++; }),
        ),
        RaisedButton(
          child: const Text('Patrol'),
          onPressed: () => setState(() { _patrolCounter++; }),
        ),
      ],
    ),
  );
}

Build and run again, and you’ll notice that the buttons are still styled even though you removed the styles from each RaisedButton. They’re now inheriting the button theme from the theme you added to MaterialApp earlier on.

做得好!您现在有一个坚实的基础,用于开奖结果3d为您的应用程序。

开奖结果3d文本小部件

Text widgets are special because ThemeData gives you multiple text styles to choose from. To see this in action, change the text widget that reads “你完全完成了X活动” in lib / home / home_page.dart to the following:

Text(
  'You\'ve done $totalActivities activities in total',
  style: Theme.of(context).textTheme.headline6,
),

建立和跑步,你现在有一个很好的舞台来看看。 :]

文字风格

Feel free to explore the different text styles contained in the TextTheme object. You’ll see text styles broken down into headline, subtitle, and body groupings. You can use these groupings to apply consistent text styles throughout your applications. This can help you create a more consistent looking application with very little effort.

创造一个黑暗的开奖结果3d

如今,大多数操作系统和应用程序都有一个在低光环境中使用的黑暗开奖结果3d或简单地为您的应用程序提供风格扭曲。您还希望为您的应用程序添加此伟大的应用程序。

要这样做,打开 lib / theme / custom_theme.dart 并在下面添加如下 灯塔米:

static ThemeData get darkTheme {
  return ThemeData(
    primaryColor: CustomColors.darkGrey,
    scaffoldBackgroundColor: Colors.black,
    fontFamily: 'Montserrat',
    textTheme: ThemeData.dark().textTheme,
    buttonTheme: ButtonThemeData(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18.0)),
      buttonColor: CustomColors.lightPurple,
    )
  );
}

This block may look complex, but it’s basically the same structure that you used for 灯塔米. The difference is that the colors and styles you’re using make more sense as a dark theme.

在行动中看到这一点,开放 lib / main.dart. and replace the theme attribute with your new dark theme:

return MaterialApp(
  title: 'Knight and Day',
  home: HomePage(),
  theme: CustomTheme.darkTheme,
);

Here, you’re simply passing CustomTheme.darkTheme instead of CustomTheme.lightTheme.

构建并运行您的应用程序,看看您的UI如何获得美丽的改装! :]

黑暗开奖结果3d

这是一个很好的升级,但如果用户可以通过单击按钮可以更改开奖结果3d更好?你会在下一节解决这个问题。

在开奖结果3d之间切换

要在开奖结果3d之间切换,您需要跟踪当前开奖结果3d。您将通过创建开奖结果3d的全局实例并在您需要时应用它来完成此操作。首先创建 config.dart.lib /开奖结果3d 并添加以下内容:

import 'package:knight_and_day/theme/custom_theme.dart';

CustomTheme currentTheme = CustomTheme();

这将是您在整个应用程序中使用的自定义开奖结果3d的实例。

接下来,开放 lib / theme / custom_theme.dart and add the following code to the top of CustomTheme:

static bool _isDarkTheme = true;
ThemeMode get currentTheme => _isDarkTheme ? ThemeMode.dark : ThemeMode.light;

void toggleTheme() {
  _isDarkTheme = !_isDarkTheme;
  notifyListeners();
}

Two main things are happening here. First, you declared a private variable that tracks whether the current theme is dark and you map _isDarkTheme to its corresponding ThemeMode.

Second, you create a method that toggles _isDarkTheme and then notifies anyone who’s listening. To be able to call notifyListeners, you need to implement ChangeNotifier. Do this by changing CustomTheme‘s signature to the following:

class CustomTheme with ChangeNotifier {
...
}

现在,开放 lib / main.dart. 并用以下代码替换其内容。记得导入 lib / theme / config.dart:

import 'package:flutter/material.dart';
import 'package:knight_and_day/home/home_page.dart';
import 'package:knight_and_day/theme/config.dart';
import 'package:knight_and_day/theme/custom_theme.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key key}): super(key: key);
  //1
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
  @override
  void initState() {
    super.initState();
    currentTheme.addListener(() {
      //2
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Knight and Day',
      home: HomePage(),
      theme: CustomTheme.lightTheme, //3
      darkTheme: CustomTheme.darkTheme, //4
      themeMode: currentTheme.currentTheme, //5
    );
  }
}

这是发生的事情的细分:

  1. You convert MyApp from a stateless to a stateful widget.
  2. Since you need ChangeNotifier to let you know when the theme changes, you subscribe to it in 在 itState and listen to changes from currentTheme. You need to trigger a widget rebuild, so you call setState with nothing particular inside the closure.
  3. 默认开奖结果3d。
  4. 黑暗的开奖结果3d。
  5. Based on this attribute, MaterialApp knows which theme to apply: either theme or darkTheme.

使所有这项工作的最后一步是通过按App Bar上的操作按钮来切换开奖结果3d。打开 lib / custom_app_bar.dart. and replace IconButton with the following:

IconButton(
  icon: const Icon(Icons.brightness_4),
  onPressed: () => currentTheme.toggleTheme(),
)

Here you added onPressed, a function that toggles between the different themes.

记得导入 lib / theme / config.dart:

import 'package:knight_and_day/theme/config.dart';

现在,您可以构建和运行。按App Bar上的操作按钮,您将会看到开奖结果3d如何动态更改。做得好!

动态开奖结果3d

然后去哪儿?

您可以使用此项目的最终版本 下载材料 本教程顶部或底部的按钮。

祝贺您的应用程序与这些美丽的开奖结果3d一起发光!现在,您可以利用扑动中的开奖结果3d系统将您的应用提升到新的水平。在尝试与用户互动时,伟大的设计和用户体验是关键。

如果你想深入潮流的扑腾世界,请退房 你的第一个浮动应用程序。另一个伟大的资源是我们令人敬畏的书, 扑腾学徒.

如果您有任何疑问,评论或想要为您的应用展示伟大的开奖结果3d选项,请随时加入下面的讨论!

平均评级

4.2/5

为此内容添加评级

5 ratings

更像这样的

贡献者

注释