flutter 五点一点一:MaterialApp Theme

这篇具有很好参考价值的文章主要介绍了flutter 五点一点一:MaterialApp Theme。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

 factory ThemeData({
 	...
    TargetPlatform? platform,  //目标平台
    ScrollbarThemeData? scrollbarTheme,   //滚动条主题样式
    ...
  }

platform 目标平台

  • 貌似表示的是当前Theme 的目标平台
  • 枚举值
    例如 我将 platform 设置为ios
platform: TargetPlatform.iOS

然后运行到 android模拟器上
点击跳转页面动画 为默认动画
flutter 五点一点一:MaterialApp Theme,flutter

若改成 android

platform: TargetPlatform.android

则动画效果如下
flutter 五点一点一:MaterialApp Theme,flutter

  • 那么问题来了
  • 不同平台 如何显示不同效果?
  • Platform 可获取现在是那个平台 可根据不通的平台设置不同的效果
  • 例如
    把Theme的 platform 去掉 不指定平台
    Android 动画 ios动画不同
class MyPageTransitionsBuilder extends PageTransitionsBuilder {
  @override
  Widget buildTransitions<T>(
    PageRoute<T>? route,
    BuildContext? context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget? child,
  ) {
    // return

    AnimatedWidget? animatedWidget = null;

    if (Platform.isAndroid) {
      animatedWidget = SizeTransition(
        sizeFactor: animation,
        child: SizeTransition(
          sizeFactor: animation,
          child: child,
        ),
      );
    } else {
      animatedWidget = ScaleTransition(
        scale: animation,
        child: RotationTransition(
          turns: secondaryAnimation,
          child: child,
        ),
      );
    }

    return animatedWidget;
  }
}

根据不同平台使用不同Theme

ThemeData? needTheme = null;
    try{
      if(Platform.isAndroid){  //web 调用会报错 
        needTheme = themeRed;
      }else{
        needTheme = themeBlue;
      }
    }catch(e){
      needTheme = themeBlue;
    }
 return MaterialApp(
      theme: needTheme,
      home: A(),
      routes: {
        "/A": (context) => A(),
        "/B": (context) => B(),
        "/C": (context) => C(),
      },
    );

结果
flutter 五点一点一:MaterialApp Theme,flutter
flutter 五点一点一:MaterialApp Theme,flutter

  • 当然 platform 也可以根据不同的平台 来展示不同的页面

scrollbarTheme

  • Scrollbar 的样式
  • ScrollbarThemeData? scrollbarTheme,
//使用scrollbar
class BState extends State<B> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Scrollbar(
          child:ListView(
              primary: true,
              children: List.generate(100, (index) => Text("条目${index}"))
          )
      ),
    );
  }
}
//设置scrollbar样式
scrollbarTheme:ScrollbarThemeData(
            thumbVisibility:MaterialStateProperty.all(true)   // true默认显示scroolbar   false 不显示
        ),

flutter 五点一点一:MaterialApp Theme,flutterflutter 五点一点一:MaterialApp Theme,flutter

 scrollbarTheme:ScrollbarThemeData(
            thumbVisibility:MaterialStateProperty.all(false),
            trackVisibility:MaterialStateProperty.all(true),
        ),
  • trackVisibility:MaterialStateProperty.all(false ), //false 效果
    flutter 五点一点一:MaterialApp Theme,flutter

  • trackVisibility:MaterialStateProperty.all(true), //true效果
    flutter 五点一点一:MaterialApp Theme,flutter

 scrollbarTheme:ScrollbarThemeData(
            thumbVisibility:MaterialStateProperty.all(false),
            thickness: MaterialStateProperty.all(100),
            trackVisibility:MaterialStateProperty.all(false),
        ),
  • thickness: MaterialStateProperty.all(100), 效果
    flutter 五点一点一:MaterialApp Theme,flutter
scrollbarTheme:ScrollbarThemeData(
            thumbVisibility:MaterialStateProperty.all(true),
            thickness: MaterialStateProperty.all(100),
            trackVisibility:MaterialStateProperty.all(false),
            radius:Radius.elliptical(50, 50)
        ),
  • radius:Radius.elliptical(50, 50) 效果
    

flutter 五点一点一:MaterialApp Theme,flutter

  • thumbColor:MaterialStateProperty.all(Colors.yellow),
    flutter 五点一点一:MaterialApp Theme,flutter
 scrollbarTheme:ScrollbarThemeData(
            thumbVisibility:MaterialStateProperty.all(true),
            thickness: MaterialStateProperty.all(100),
            trackVisibility:MaterialStateProperty.all(true),
            radius:Radius.elliptical(50, 50),
            thumbColor:MaterialStateProperty.all(Colors.yellow),
            trackColor:MaterialStateProperty.all(Colors.cyanAccent),
        ),
  • trackColor:MaterialStateProperty.all(Colors.cyanAccent), 轨道颜色
    flutter 五点一点一:MaterialApp Theme,flutter
  •         trackBorderColor:MaterialStateProperty.all(Colors.red),
    

flutter 五点一点一:MaterialApp Theme,flutter

  • crossAxisMargin:20,  没设置之前如上图  设置后如下图
    

flutter 五点一点一:MaterialApp Theme,flutter
mainAxisMargin:50, 未设置前如上图 设置后如下图
flutter 五点一点一:MaterialApp Theme,flutter

  • minThumbLength:200, 未设置前如上图 设置后如下图

flutter 五点一点一:MaterialApp Theme,flutter文章来源地址https://www.toymoban.com/news/detail-803805.html

  • interactive:true, 是否可交互 true可拖动滚动条 false不可以

全部代码

import 'dart:io';

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

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

class MyApp extends StatelessWidget {
  const MyApp();

  @override
  Widget build(BuildContext context) {
    ThemeData themeRed = ThemeData.light().copyWith(
      extensions: <ThemeExtension<ThemeColors>>[ThemeColors(themeType: 0)],
    );

    ThemeData themeGreen = ThemeData.light().copyWith(
      extensions: <ThemeExtension<ThemeColors>>[ThemeColors(themeType: 1)],
    );

    ThemeData themeBlue = ThemeData.light().copyWith(
        extensions: <ThemeExtension<ThemeColors>>[ThemeColors(themeType: 2)],
        inputDecorationTheme: InputDecorationTheme(
          labelStyle: TextStyle(color: Colors.black),
          hintStyle: TextStyle(color: Colors.grey),
          border: UnderlineInputBorder(),
          focusedBorder: UnderlineInputBorder(),
        ),
        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
        pageTransitionsTheme: PageTransitionsTheme(
            builders: <TargetPlatform, PageTransitionsBuilder>{
              TargetPlatform.android: MyPageTransitionsBuilder()
            }),
        scrollbarTheme:ScrollbarThemeData(
            thumbVisibility:MaterialStateProperty.all(true),     //是否默认显示滚动条   false滑动的时候才显示
            thickness: MaterialStateProperty.all(100),   //滚动条宽度
            trackVisibility:MaterialStateProperty.all(true),  //轨道是否显示
            radius:Radius.elliptical(50, 50),   //滚动条弧度
            thumbColor:MaterialStateProperty.all(Colors.yellow),  //滚动条颜色
            trackColor:MaterialStateProperty.all(Colors.cyanAccent),    //轨道颜色
            trackBorderColor:MaterialStateProperty.all(Colors.red),  //轨道边框颜色
            crossAxisMargin:20,   //距离左右高
            mainAxisMargin:50,   //距离上下高
            minThumbLength:200,  //滚动条最小高度
            interactive:true,  //滚动条是否可拖动
          
        ),
    );


    ThemeData? needTheme = null;
    try{
      if(Platform.isAndroid){
        needTheme = themeRed;
      }else{
        needTheme = themeBlue;
      }
    }catch(e){
      needTheme = themeBlue;
    }

    return MaterialApp(
      theme: needTheme,
      home: A(),
      routes: {
        "/A": (context) => A(),
        "/B": (context) => B(),
        "/C": (context) => C(),
      },
    );
  }

  void changeTheme() {}
}

class ThemeColors extends ThemeExtension<ThemeColors> {
  static String main_color = "main_color";
  static String text_color = "text_color";
  static String text_background = "text_background";

  var themeType = 0;

  var themeRed = {
    main_color: Colors.red,
    text_color: const Color(0xFFD26161),
    text_background: const Color(0xFFEAE4E4),
  };

  var themeGreen = {
    main_color: Colors.green,
    text_color: const Color(0xFF6EDC9A),
    text_background: const Color(0xFFEAE4E4),
  };

  var themeBlue = {
    main_color: Colors.blue,
    text_color: const Color(0xFF6F83E7),
    text_background: const Color(0xFFEAE4E4),
  };

  ThemeColors({this.themeType = 0});

  ThemeColors.themeRed(this.themeRed);

  ThemeColors.themeGreen(this.themeGreen);

  ThemeColors.themeBlue(this.themeBlue);

  @override
  ThemeExtension<ThemeColors> copyWith() {
    var result = null;
    switch (this.themeType) {
      case 0:
        result = ThemeColors.themeRed(themeRed);
        break;
      case 1:
        result = ThemeColors.themeGreen(themeGreen);
        break;
      case 2:
        result = ThemeColors.themeBlue(themeBlue);
        break;
    }

    return result;
  }

  @override
  ThemeExtension<ThemeColors> lerp(
      covariant ThemeExtension<ThemeColors>? other, double t) {
    if (other! is ThemeColors) {
      return this;
    }
    var result = null;
    switch (this.themeType) {
      case 0:
        result = ThemeColors.themeRed(themeRed);
        break;
      case 1:
        result = ThemeColors.themeGreen(themeGreen);
        break;
      case 2:
        result = ThemeColors.themeBlue(themeBlue);
        break;
    }

    return result;
  }

  Color getColor(String colorName) {
    var resultMap = null;
    switch (this.themeType) {
      case 0:
        resultMap = themeRed;
        break;
      case 1:
        resultMap = themeGreen;
        break;
      case 2:
        resultMap = themeBlue;
        break;
    }
    return resultMap[colorName];
  }
}

class A extends StatefulWidget {
  A() {
    print("A页面启动!");
  }

  @override
  State<StatefulWidget> createState() => AState();
}

class AState extends State<A> {
  @override
  Widget build(BuildContext context) {
    ThemeColors themeColors =
        Theme.of(context).extension<ThemeColors>() ?? ThemeColors(themeType: 0);

    return Scaffold(
      backgroundColor: themeColors.getColor(ThemeColors.main_color),
      body: Container(
        child: Column(
          children: [
            // TextField(
            //   decoration: InputDecoration(
            //     hintText: "请输入内容"
            //   ),
            // ),
            TextButton(
              onPressed: () {
                Navigator.pushNamed(context, '/B');
              },
              child: Text("B"),
              style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.green),
                  padding: MaterialStateProperty.all(EdgeInsets.all(100))),
            ),
            TextButton(
              onPressed: () {
                Navigator.pushNamed(context, '/C');
              },
              child: Text("C"),
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.red),
                padding: MaterialStateProperty.all(EdgeInsets.all(100)),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class B extends StatefulWidget {
  B() {
    print("B页面启动!");
  }

  @override
  State<StatefulWidget> createState() => BState();
}

class BState extends State<B> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Scrollbar(
          child:ListView(
              primary: true,
              children: List.generate(100, (index) => Text("条目${index}"))
          )
      ),
    );
  }
}

class C extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => CState();
}

class CState extends State<C> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("CCCCCCCCCCCCCCCCCCCCCCCCC"),
      ),
    );
  }
}

class MyPageTransitionsBuilder extends PageTransitionsBuilder {
  @override
  Widget buildTransitions<T>(
    PageRoute<T>? route,
    BuildContext? context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget? child,
  ) {
    // return

    AnimatedWidget? animatedWidget = null;

    if (Platform.isAndroid) {
      animatedWidget = SizeTransition(
        sizeFactor: animation,
        child: SizeTransition(
          sizeFactor: animation,
          child: child,
        ),
      );
    } else {
      animatedWidget = ScaleTransition(
        scale: animation,
        child: RotationTransition(
          turns: secondaryAnimation,
          child: child,
        ),
      );
    }

    return animatedWidget;
  }
}

到了这里,关于flutter 五点一点一:MaterialApp Theme的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 汇川机器人+五点法标定常规托盘码垛

            个人观点,码垛和拆垛本质上是一样,把流水线上的产品按规律整齐堆叠到托盘里叫码垛,从整齐排列有物料的托盘取料然后放到流水线上叫拆垛。取放料操作一般由机器人完成,我习惯按物料流向将设备分为码盘机(码垛)和上料机(拆垛),以下程序实例中的机器人为

    2024年03月27日
    浏览(49)
  • 二阶椭圆型第一边值问题的数值解法(五点差分格式和有限体积法)附matlab代码

    这里我们介绍五点差分格式和有限体积法求椭圆型第一边值问题, 题目: 分别采用矩形网格的五点差分格式和有限体积法求椭圆型第一边值问题, − ∂ 2 u ∂ x 2 − ∂ 2 u ∂ y 2 + a ∂ u ∂ x = 0 , a 0 , -frac{partial^2u}{partial x^2}-frac{partial^2u}{partial y^2}+afrac{partial u}{partial x}

    2023年04月22日
    浏览(33)
  • Android style(样式) 和 theme(主题) 属性

            样式和主题资源都可用于对Android UI组件进行“美化”,只要充分利用好这两个属性资源,我们可以开发出各种风格的应用界面。         style 样式: 一个样式相当于多个格式的集合,其他UI组件通过style属性来指定样式,样式资源文件时放在/res/values/styles.xm

    2023年04月12日
    浏览(33)
  • 如何下载IDEA主题插件Material Theme UI?

    1、打开IDEA插件官网:点击这里,选择IDEA对应的版本下载。我的IDEA版本是2021.3,即我应下载对应的版本6.16.2 2、分别点击IDEA左上角 File - Settings - Plugins - 设置 - install Plugin from Disk… ,从你的电脑里导入刚下载的压缩包Material_Theme_UI-6.16.2 3、( 注意 :路径不能有中文)我的路

    2024年04月26日
    浏览(48)
  • magento2 二次开发如何自定义theme

    2024年02月15日
    浏览(30)
  • MidJourney笔记(9)-daily_theme-docs-describe

    切换 #daily-theme 频道更新的通知 。 但我发现在对话框那里,是没有这个命令的: 但官网是有介绍,不知道是不是版本问题还是这个命令已经无效。 但后来,我发现这个命令是要在Midjourney服务对话框那里才有,在我们后面添加的Mid

    2024年02月04日
    浏览(31)
  • IDEA 插件 Material Theme UI收费后 免费的办法

    使用手动安装的方式 1.在官网找到10之前的版本,下载插件 https://plugins.jetbrains.com/plugin/8006-material-theme-ui/versions 6点10以下的就可以 2.手动在idea进行插件导入 重启idea即可使用主题了

    2024年02月13日
    浏览(50)
  • 博客搭建教程Github+Hexo+hexo-theme-matery主题

    前情提要 写这篇文的目的 记录自己搭建过程,便于以后快速复用 总结经验和自己踩的坑,给其他小伙伴一些参考(由于是搭建后写的,所以没有参考图片) 介绍 初步效果参考我的博客:hermia的个人博客 本博客基于Hexo框架,使用github托管 使用自定义域名: hermiablog.com hexo主题

    2024年02月19日
    浏览(47)
  • R语言【cli】——builtin_theme():内置的CLI主题

    Package  cli  version 3.6.0 此主题始终处于活动状态,并且位于主题堆栈的底部。 参数【dark】 :是否使用黑暗主题。cli.theme_dark选项可用于显式请求暗主题。如果没有设置,或者设置为“auto”,那么cli会尝试检测暗主题,这在最近的RStudio版本和macOS上的term中都有效。 一个命名

    2024年01月25日
    浏览(42)
  • 【Android-JetpackCompose】11、主题设置:Theme、Colors、Typography、Shapes

    首先,从下面的仓库克隆代码: Material Design 定义了一些从语义上命名的颜色: primary 是主要品牌颜色,secondary 用于提供强调色。您可以为形成对比的区域提供颜色更深/更浅的变体。background 和 surface 这两种颜色用于那些容纳在概念上驻留在应用“Surface”的组件的容器。此外

    2024年02月05日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包