Advertisement
Guest User

Untitled

a guest
May 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. import 'dart:math';
  2.  
  3. import 'package:flutter/material.dart';
  4.  
  5. class PageViewDot extends StatefulWidget {
  6. final bool isFirst;
  7.  
  8. PageViewDot({Key key, this.isFirst = true}) : super(key: key);
  9.  
  10. @override
  11. _PageViewDotState createState() => _PageViewDotState();
  12. }
  13.  
  14. class _PageViewDotState extends State<PageViewDot> {
  15. final _controller = new PageController();
  16.  
  17. List<Widget> _pages;
  18. List<Color> _backgroundColor;
  19. bool _isPink = false;
  20.  
  21. @override
  22. void initState() {
  23. _pages = <Widget>[
  24. _TutorialPage(
  25. color: <Color>[
  26. Color.fromRGBO(102, 70, 203, 1),
  27. Color.fromRGBO(115, 87, 205, 1),
  28. Color.fromRGBO(135, 101, 218, 1),
  29. Color.fromRGBO(199, 166, 255, 1),
  30. ],
  31. child: ConstrainedBox(
  32. constraints: const BoxConstraints.expand(),
  33. child: new FlutterLogo(colors: Colors.blue),
  34. ),
  35. ),
  36. _TutorialPage(
  37. color: <Color>[
  38. Color.fromRGBO(255, 170, 0, 1),
  39. Color.fromRGBO(255, 188, 75, 1),
  40. Color.fromRGBO(255, 207, 112, 1),
  41. Color.fromRGBO(255, 211, 144, 1),
  42. ],
  43. child: new ConstrainedBox(
  44. constraints: const BoxConstraints.expand(),
  45. child: new FlutterLogo(
  46. style: FlutterLogoStyle.stacked, colors: Colors.red),
  47. ),
  48. ),
  49. _TutorialPage(
  50. color: <Color>[
  51. Color.fromRGBO(0, 183, 98, 1),
  52. Color.fromRGBO(35, 194, 120, 1),
  53. Color.fromRGBO(89, 223, 143, 1),
  54. Color.fromRGBO(171, 247, 206, 1),
  55. ],
  56. child: new ConstrainedBox(
  57. constraints: const BoxConstraints.expand(),
  58. child: new FlutterLogo(
  59. style: FlutterLogoStyle.horizontal, colors: Colors.green),
  60. ),
  61. ),
  62. _TutorialPage(
  63. color: <Color>[
  64. Color.fromRGBO(246, 98, 98, 1),
  65. Color.fromRGBO(246, 118, 118, 1),
  66. Color.fromRGBO(245, 115, 115, 1),
  67. Color.fromRGBO(253, 187, 187, 1),
  68. ],
  69. child: Text(
  70. "The End",
  71. style: TextStyle(color: Colors.white, fontSize: 100),
  72. ),
  73. ),
  74. ];
  75.  
  76. _backgroundColor = <Color>[
  77. Color.fromRGBO(102, 70, 203, 1),
  78. Color.fromRGBO(115, 87, 205, 1),
  79. Color.fromRGBO(135, 101, 218, 1),
  80. Color.fromRGBO(199, 166, 255, 1),
  81. ];
  82. _controller.addListener(() {
  83. if (_controller.page < 0.3 && _isPink) {
  84. setState(() {
  85. _isPink = false;
  86. _backgroundColor = <Color>[
  87. Color.fromRGBO(102, 70, 203, 1),
  88. Color.fromRGBO(115, 87, 205, 1),
  89. Color.fromRGBO(135, 101, 218, 1),
  90. Color.fromRGBO(199, 166, 255, 1),
  91. ];
  92. });
  93. } else if (_controller.page > 2.7 && !_isPink) {
  94. setState(() {
  95. _isPink = true;
  96. _backgroundColor = <Color>[
  97. Color.fromRGBO(246, 98, 98, 1),
  98. Color.fromRGBO(246, 118, 118, 1),
  99. Color.fromRGBO(245, 115, 115, 1),
  100. Color.fromRGBO(253, 187, 187, 1),
  101. ];
  102. });
  103. }
  104. });
  105. super.initState();
  106. }
  107.  
  108. @override
  109. Widget build(BuildContext context) {
  110. final theme = Theme.of(context);
  111. if (_pages != null && _pages.isNotEmpty) {
  112. return Scaffold(
  113. body: Stack(
  114. children: <Widget>[
  115. Container(
  116. decoration: BoxDecoration(
  117. gradient: LinearGradient(
  118. begin: Alignment.topLeft,
  119. end: Alignment.bottomRight,
  120. colors: _backgroundColor))),
  121. PageView.builder(
  122. physics: AlwaysScrollableScrollPhysics(),
  123. controller: _controller,
  124. itemCount: _pages.length,
  125. itemBuilder: (BuildContext context, int index) {
  126. return _pages[index % _pages.length];
  127. },
  128. ),
  129. Positioned(
  130. bottom: 50.0,
  131. left: 0.0,
  132. right: 0.0,
  133. child: Container(
  134. padding: const EdgeInsets.all(20.0),
  135. child: Center(
  136. child: DotsIndicator(
  137. controller: _controller,
  138. itemCount: _pages.length,
  139. onPageSelected: (int page) {
  140. _controller.animateToPage(
  141. page,
  142. duration: const Duration(milliseconds: 300),
  143. curve: Curves.ease,
  144. );
  145. },
  146. ),
  147. ),
  148. ),
  149. ),
  150. Positioned(
  151. top: 0.0,
  152. left: 0.0,
  153. child: SafeArea(
  154. child: IconButton(
  155. color: Colors.white,
  156. padding: const EdgeInsets.all(20.0),
  157. icon: Icon(Icons.keyboard_arrow_left),
  158. onPressed: () => Navigator.pop(context),
  159. ),
  160. ),
  161. )
  162. ],
  163. ),
  164. );
  165. } else {
  166. return Scaffold(body: Center(child: Text("Tutorial not available")));
  167. }
  168. }
  169. }
  170.  
  171. class DotsIndicator extends AnimatedWidget {
  172. DotsIndicator({
  173. this.controller,
  174. this.itemCount,
  175. this.onPageSelected,
  176. this.color: Colors.white,
  177. }) : super(listenable: controller);
  178.  
  179. /// The PageController that this DotsIndicator is representing.
  180. final PageController controller;
  181.  
  182. /// The number of items managed by the PageController
  183. final int itemCount;
  184.  
  185. /// Called when a dot is tapped
  186. final ValueChanged<int> onPageSelected;
  187.  
  188. /// The color of the dots.
  189. ///
  190. /// Defaults to `Colors.white`.
  191. final Color color;
  192.  
  193. // The base size of the dots
  194. static const double _kDotSize = 8.0;
  195.  
  196. // The increase in the size of the selected dot
  197. static const double _kMaxZoom = 2.0;
  198.  
  199. // The distance between the center of each dot
  200. static const double _kDotSpacing = 25.0;
  201.  
  202. Widget _buildDot(int index) {
  203. double selectedness = Curves.easeOut.transform(
  204. max(
  205. 0.0,
  206. 1.0 - ((controller.page ?? controller.initialPage) - index).abs(),
  207. ),
  208. );
  209. double zoom = 1.0 + (_kMaxZoom - 1.0) * selectedness;
  210. return new Container(
  211. width: _kDotSpacing,
  212. child: new Center(
  213. child: new Material(
  214. color: color,
  215. type: MaterialType.circle,
  216. child: new Container(
  217. width: _kDotSize * zoom,
  218. height: _kDotSize * zoom,
  219. child: new InkWell(
  220. onTap: () => onPageSelected(index),
  221. ),
  222. ),
  223. ),
  224. ),
  225. );
  226. }
  227.  
  228. Widget build(BuildContext context) {
  229. return new Row(
  230. mainAxisAlignment: MainAxisAlignment.center,
  231. children: new List<Widget>.generate(itemCount, _buildDot),
  232. );
  233. }
  234. }
  235.  
  236. class _TutorialPage extends StatelessWidget {
  237. final List<Color> color;
  238. final Widget child;
  239.  
  240. const _TutorialPage({Key key, this.color, this.child}) : super(key: key);
  241.  
  242. @override
  243. Widget build(BuildContext context) {
  244. return Container(
  245. decoration: BoxDecoration(
  246. gradient: LinearGradient(
  247. begin: Alignment.topLeft,
  248. end: Alignment.bottomRight,
  249. colors: color,
  250. ),
  251. ),
  252. child: Center(child: child),
  253. );
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement