Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. // Copyright 2019 The Flutter Team. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. import 'dart:ui';
  6.  
  7. import 'package:flutter/cupertino.dart';
  8. import 'package:flutter/foundation.dart' as foundation;
  9. import 'package:flutter/material.dart';
  10. import 'package:multiplatform/widgets.dart';
  11.  
  12. bool get _isIOS => foundation.defaultTargetPlatform == TargetPlatform.iOS;
  13.  
  14. class AdaptiveSegmentedControl extends StatelessWidget {
  15. AdaptiveSegmentedControl({
  16. this.onValueChanged,
  17. this.children,
  18. this.groupValue,
  19. });
  20.  
  21. final ValueChanged<int> onValueChanged;
  22. final Map<int, Widget> children;
  23. final int groupValue;
  24.  
  25. @override
  26. Widget build(BuildContext context) {
  27. if (_isIOS) {
  28. return CupertinoSegmentedControl<int>(
  29. children: children,
  30. groupValue: groupValue,
  31. onValueChanged: onValueChanged,
  32. );
  33. } else {
  34. return Row(
  35. mainAxisSize: MainAxisSize.max,
  36. mainAxisAlignment: MainAxisAlignment.center,
  37. children: [
  38. for (final key in children.keys)
  39. Padding(
  40. padding: const EdgeInsets.only(right: 8.0),
  41. child: RaisedButton(
  42. child: children[key],
  43. color: groupValue == key
  44. ? Theme.of(context).buttonTheme.colorScheme.primary
  45. : Color(0xffcccccc),
  46. onPressed: () {
  47. onValueChanged(key);
  48. },
  49. ),
  50. ),
  51. ],
  52. );
  53. }
  54. }
  55. }
  56.  
  57. class AdaptiveBackground extends StatelessWidget {
  58. const AdaptiveBackground({this.color, this.intensity = 25, this.child});
  59.  
  60. final Color color;
  61. final double intensity;
  62. final Widget child;
  63.  
  64. @override
  65. Widget build(BuildContext context) {
  66. return ClipRect(
  67. child: (_isIOS)
  68. ? BackdropFilter(
  69. filter: ImageFilter.blur(sigmaX: intensity, sigmaY: intensity),
  70. child: DecoratedBox(
  71. decoration: BoxDecoration(
  72. color: color,
  73. ),
  74. child: child,
  75. ),
  76. )
  77. : DecoratedBox(
  78. decoration: BoxDecoration(
  79. color: color.withAlpha(0xe8),
  80. ),
  81. child: child,
  82. ),
  83. );
  84. }
  85. }
  86.  
  87. class AdaptivePageScaffold extends StatelessWidget {
  88. const AdaptivePageScaffold({
  89. @required this.title,
  90. @required this.child,
  91. }) : assert(title != null),
  92. assert(child != null);
  93.  
  94. final String title;
  95. final Widget child;
  96.  
  97. @override
  98. Widget build(BuildContext context) {
  99. if (_isIOS) {
  100. return AdaptiveTextTheme(
  101. child: CupertinoPageScaffold(
  102. navigationBar: CupertinoNavigationBar(
  103. middle: Text(title),
  104. ),
  105. resizeToAvoidBottomInset: false,
  106. child: child,
  107. ),
  108. );
  109. } else {
  110. return AdaptiveTextTheme(
  111. child: Scaffold(
  112. appBar: AppBar(
  113. title: Text(title),
  114. ),
  115. drawer: ModalRoute.of(context).isFirst ? MainDrawer() : null,
  116. body: child,
  117. ),
  118. );
  119. }
  120. }
  121. }
  122.  
  123. class AdaptiveSlider extends StatelessWidget {
  124. const AdaptiveSlider({
  125. this.value,
  126. this.onChanged,
  127. this.min = 0.0,
  128. this.max = 1.0,
  129. this.divisions,
  130. Key key,
  131. }) : super(key: key);
  132.  
  133. final double value;
  134. final ValueChanged<double> onChanged;
  135. final double min;
  136. final double max;
  137. final int divisions;
  138.  
  139. @override
  140. Widget build(BuildContext context) {
  141. if (_isIOS) {
  142. return CupertinoSlider(
  143. value: value,
  144. onChanged: onChanged,
  145. min: min,
  146. max: max,
  147. divisions: divisions,
  148. );
  149. } else {
  150. return Slider(
  151. value: value,
  152. onChanged: onChanged,
  153. min: min,
  154. max: max,
  155. divisions: divisions,
  156. );
  157. }
  158. }
  159. }
  160.  
  161. class AdaptiveTextField extends StatelessWidget {
  162. const AdaptiveTextField({
  163. this.maxLines,
  164. this.controller,
  165. this.focusNode,
  166. this.textAlign,
  167. this.keyboardType,
  168. this.maxLength,
  169. this.onChanged,
  170. this.style,
  171. Key key,
  172. }) : super(key: key);
  173.  
  174. final int maxLines;
  175. final TextEditingController controller;
  176. final FocusNode focusNode;
  177. final TextAlign textAlign;
  178. final TextInputType keyboardType;
  179. final int maxLength;
  180. final ValueChanged<String> onChanged;
  181. final TextStyle style;
  182.  
  183. @override
  184. Widget build(BuildContext context) {
  185. if (_isIOS) {
  186. return CupertinoTextField(
  187. maxLines: maxLines,
  188. controller: controller,
  189. focusNode: focusNode,
  190. textAlign: textAlign,
  191. keyboardType: keyboardType,
  192. maxLength: maxLength,
  193. onChanged: onChanged,
  194. style: style,
  195. );
  196. } else {
  197. return TextField(
  198. maxLines: maxLines,
  199. controller: controller,
  200. focusNode: focusNode,
  201. textAlign: textAlign,
  202. keyboardType: keyboardType,
  203. maxLength: maxLength,
  204. onChanged: onChanged,
  205. style: style,
  206. );
  207. }
  208. }
  209. }
  210.  
  211. class AdaptiveButton extends StatelessWidget {
  212. const AdaptiveButton({
  213. this.child,
  214. this.onPressed,
  215. Key key,
  216. }) : super(key: key);
  217.  
  218. final Widget child;
  219. final VoidCallback onPressed;
  220.  
  221. @override
  222. Widget build(BuildContext context) {
  223. if (_isIOS) {
  224. return CupertinoButton.filled(
  225. child: child,
  226. onPressed: onPressed,
  227. );
  228. } else {
  229. return RaisedButton(
  230. child: child,
  231. onPressed: onPressed,
  232. );
  233. }
  234. }
  235. }
  236.  
  237. class AdaptiveTextTheme extends StatelessWidget {
  238. const AdaptiveTextTheme({
  239. Key key,
  240. @required this.child,
  241. }) : assert(child != null),
  242. super(key: key);
  243.  
  244. final Widget child;
  245.  
  246. @override
  247. Widget build(BuildContext context) {
  248. final materialThemeData = Theme.of(context);
  249. final cupertinoThemeData = CupertinoTheme.of(context);
  250.  
  251. return _AdaptiveTextThemeProvider(
  252. data: AdaptiveTextThemeData(
  253. materialThemeData?.textTheme,
  254. cupertinoThemeData?.textTheme,
  255. ),
  256. child: child,
  257. );
  258. }
  259.  
  260. static AdaptiveTextThemeData of(BuildContext context) {
  261. final provider =
  262. context.inheritFromWidgetOfExactType(_AdaptiveTextThemeProvider)
  263. as _AdaptiveTextThemeProvider;
  264. return provider?.data;
  265. }
  266. }
  267.  
  268. class _AdaptiveTextThemeProvider extends InheritedWidget {
  269. _AdaptiveTextThemeProvider({
  270. this.data,
  271. @required Widget child,
  272. Key key,
  273. }) : super(child: child, key: key);
  274.  
  275. final AdaptiveTextThemeData data;
  276.  
  277. @override
  278. bool updateShouldNotify(_AdaptiveTextThemeProvider oldWidget) {
  279. return data != oldWidget.data;
  280. }
  281. }
  282.  
  283. class AdaptiveTextThemeData {
  284. const AdaptiveTextThemeData(this.materialThemeData, this.cupertinoThemeData);
  285.  
  286. final TextTheme materialThemeData;
  287. final CupertinoTextThemeData cupertinoThemeData;
  288.  
  289. TextStyle get headline =>
  290. (materialThemeData?.headline ?? cupertinoThemeData.navLargeTitleTextStyle)
  291. .copyWith(
  292. fontSize: 32,
  293. fontWeight: FontWeight.bold,
  294. letterSpacing: 1.6,
  295. );
  296.  
  297. TextStyle get subhead =>
  298. (materialThemeData?.subhead ?? cupertinoThemeData.textStyle).copyWith(
  299. color: Color(0xDE000000),
  300. fontSize: 14,
  301. letterSpacing: 0.1,
  302. );
  303.  
  304. TextStyle get tileTitle =>
  305. (materialThemeData?.body2 ?? cupertinoThemeData.textStyle).copyWith(
  306. fontSize: 21,
  307. fontWeight: FontWeight.w600,
  308. letterSpacing: -0.2,
  309. );
  310.  
  311. TextStyle get bodySmall =>
  312. (materialThemeData?.body2 ?? cupertinoThemeData.textStyle).copyWith(
  313. color: Color(0xDE000000),
  314. fontSize: 12,
  315. letterSpacing: 0.4,
  316. fontWeight: FontWeight.w500,
  317. );
  318.  
  319. TextStyle get body =>
  320. (materialThemeData?.subhead ?? cupertinoThemeData.navTitleTextStyle)
  321. .copyWith(
  322. color: Color(0xDE000000),
  323. fontSize: 14.05,
  324. letterSpacing: 0.25,
  325. fontWeight: FontWeight.w500,
  326. );
  327.  
  328. TextStyle get label =>
  329. (materialThemeData?.body2 ?? cupertinoThemeData.textStyle).copyWith(
  330. fontStyle: FontStyle.italic,
  331. fontSize: 12,
  332. letterSpacing: 0.4,
  333. fontWeight: FontWeight.w500,
  334. color: Color(0x99000000),
  335. );
  336.  
  337. @override
  338. int get hashCode => materialThemeData.hashCode ^ cupertinoThemeData.hashCode;
  339.  
  340. @override
  341. bool operator ==(dynamic other) {
  342. if (identical(this, other)) return true;
  343. if (other.runtimeType != runtimeType) return false;
  344. final AdaptiveTextThemeData typedOther = other;
  345. return materialThemeData != typedOther.materialThemeData ||
  346. cupertinoThemeData != typedOther.cupertinoThemeData;
  347. }
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement