flutter生成源代码_手把手教你用Spuernova生成Flutter代码

前言

周末得空,逛了dribbble,发现了好多好看的设计,馋的不行。相信每个前端都有这样一个梦想:能把设计稿直接生成代码该多好,忽而想起了Flutter Interact上大佬们演示的插件,感觉有得搞 br/>sketch准备

没有vip不能下载,就自己照着预览图画一个,丑莫怪~

呵呵~

好在XD和sketch,那么话不多说,下载安装,导入

这里直接选择全部页面

搞定~,so easy

生成的代码可以直接点击右上角的到处图标到处成项目或者单个文件,

这样就完工啦~

生成的项目结构大致如下:

运行

生成的代码在安装完成后可以直接运行。用VSCode打开刚刚生成的项目,flutter pub get一波没有问题,

flutter run起来看看

海星,感觉哪里不对?字体图标和字体怎么都这样样子的??

原来这里的字体图标被转成了图片,但是字体并没有问题,看来字体阴影的识别还是有一定问题。

不过 hot-reload(但是无论怎样都不能hot-reload...)

代码品读

简单来看看生成的list组件:

整个页面全部是stack,额~,又不是不能用。

虽然做成这样不太智能,但是我们可以手动改生成组件的类型,点击选中要更改类型的组件,右键选择Convert to Component-> Text Field,我们尝试将它转换成一个输入框。

/// 更改前的代码

Container(

width: 57,

height: 57,

decoration: BoxDecoration(

color: Color.fromARGB(255, 111, 124, 132),

boxShadow: [

BoxShadow(

color: Color.fromARGB(44, 29, 30, 32),

offset: Offset(2, 2),

blurRadius: 3,

),

],

borderRadius: BorderRadius.all(Radius.circular(8)),

),

child: Container(),

)

/// 更改后的代码

Container(

width: 57,

height: 57,

decoration: BoxDecoration(

color: Color.fromARGB(255, 111, 124, 132),

boxShadow: [

BoxShadow(

color: Color.fromARGB(44, 29, 30, 32),

offset: Offset(2, 2),

blurRadius: 3,

),

],

borderRadius: BorderRadius.all(Radius.circular(8)),

),

child: TextField(

style: TextStyle(

color: Color.fromARGB(255, 0, 0, 0),

fontWeight: FontWeight.w400,

fontSize: 12,

),

maxLines: 1,

autocorrect: false,

),

)

还是可以的,只要稍加修改就可以使用。

从上面的代码来看,values目录下:

// colors.dart

import 'dart:ui';

class AppColors {

static const Color primaryBackground = Color.fromARGB(255, 38, 173, 211);

static const Color secondaryBackground = Color.fromARGB(255, 36, 38, 44);

static const Color ternaryBackground = Color.fromARGB(255, 74, 78, 122);

static const Color primaryElement = Color.fromARGB(255, 38, 43, 47);

static const Color secondaryElement = Color.fromARGB(255, 243, 64, 61);

static const Color accentElement = Color.fromARGB(255, 47, 52, 57);

static const Color primaryText = Color.fromARGB(255, 93, 99, 106);

static const Color secondaryText = Color.fromARGB(255, 183, 190, 199);

static const Color accentText = Color.fromARGB(255, 137, 145, 152);

}

// gradients.dart

import 'package:flutter/rendering.dart';

class Gradients {

static const Gradient primaryGradient = LinearGradient(

begin: Alignment(0.5, 0),

end: Alignment(0.5, 1),

stops: [

0,

1,

],

colors: [

Color.fromARGB(255, 41, 44, 49),

Color.fromARGB(255, 49, 54, 59),

],

);

static const Gradient secondaryGradient = LinearGradient(

begin: Alignment(0.5, 0),

end: Alignment(0.5, 1),

stops: [

0,

1,

],

colors: [

Color.fromARGB(255, 51, 54, 59),

Color.fromARGB(255, 37, 40, 45),

],

);

}

实际项目中我们可能不止一套主题,那么将上面的生成的样式稍加组织,就可以生成符合项目需求的主题:

// custom_theme.dart

// 蠢蠢的写法,大佬们勿笑

import 'package:flutter/material.dart';

class CustomTheme {

CustomTheme({

this.lightShadowColor,

this.darkShadowColor,

this.lightShadowBlur,

this.weightShadowBlur,

this.lightShadowOffset,

this.weightShadowOffset,

});

Color lightShadowColor;

Color darkShadowColor;

double lightShadowBlur;

double weightShadowBlur;

Offset lightShadowOffset;

Offset weightShadowOffset;

factory CustomTheme.light() => CustomTheme(

...

);

factory CustomTheme.dark() => CustomTheme(

lightShadowColor: Color.fromARGB(255, 46, 42, 53),

darkShadowColor: Color.fromARGB(255, 85, 59, 60),

lightShadowOffset: Offset.zero,

weightShadowOffset: Offset.zero,

lightShadowBlur: 3,

weightShadowBlur: 3,

);

static ThemeData darkTheme = ThemeData(

appBarTheme: AppBarTheme(elevation: 0),

scaffoldBackgroundColor: Color(0xFF2E3439),

primarySwatch: MaterialColor(

0xFF2E3439,

{

50: Color(0xFF8293A1),

100: Color(0xFF768693),

200: Color(0xFF6D7B87),

300: Color(0xFF606D78),

400: Color(0xFF515C66),

500: Color(0xFF48535C),

600: Color(0xFF3F4850),

700: Color(0xFF384046),

800: Color(0xFF30383E),

900: Color(0xFF2E3439),

},

),

);

static ThemeData lightTheme = ThemeData(

appBarTheme: AppBarTheme(elevation: 0),

scaffoldBackgroundColor: Color(0xFF2E3439),

...,

);

static CustomTheme of(BuildContext context) {

Brightness brightness = MediaQuery.of(context).platformBrightness;

return brightness == Brightness.dark ? CustomTheme.dark() : CustomTheme.light();

}

static ThemeData systemTheme(BuildContext context, [Brightness brightness]) {

Brightness _brightness = brightness ?? MediaQuery.of(context).platformBrightness;

return _brightness == Brightness.dark ? darkTheme : lightTheme;

}

}

到这里我们基本就结束了,都学会了吗 br/>总结UI直接生成UI代码可行,但离完美还有很长一段路

图标字体会直接生成图片,并引入

带阴影的字体阴影想过不理想

生成的代码不能直接用在项目中,只有个别组件可以直接应用

生成的样式可利用价值比较高

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值