paws_ed

app

Jul 14th, 2020
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 15.41 KB | None | 0 0
  1.  
  2. void main() => runApp(
  3.     MyApp()); // Main used arrow notation for calling one-line function or methods
  4.  
  5. class MyApp extends StatelessWidget {
  6.   @override
  7.   Widget build(BuildContext context) {
  8.     return MaterialApp(
  9.       title: 'NL',
  10.       home: ScrollTab(),
  11.     );
  12.   }
  13. }
  14.  
  15. class ScrollTab extends StatefulWidget {
  16.   @override
  17.   _ScrollTabState createState() => _ScrollTabState();
  18. }
  19.  
  20. class _ScrollTabState extends State<ScrollTab>
  21.     with SingleTickerProviderStateMixin {
  22.   // Extending the stateless widget makes the app itself a widget
  23.   TabController _tabController;
  24.   ScrollController _scrollViewController;
  25.  
  26.   @override
  27.   void initState() {
  28.     super.initState();
  29.     _scrollViewController = new ScrollController();
  30.     _tabController = TabController(initialIndex: 0, vsync: this, length: 2);
  31.   }
  32.  
  33.   @override
  34.   void dispose() {
  35.     _scrollViewController.dispose();
  36.     _tabController.dispose();
  37.     super.dispose();
  38.   }
  39.  
  40.   @override
  41.   Widget build(BuildContext context) {
  42.  
  43.     return DefaultTabController(
  44.       length: 2,
  45.       child: new Scaffold(
  46.         body: new NestedScrollView(
  47.           headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  48.             return <Widget>[
  49.               new SliverAppBar(
  50.                 title: Text("Application"),
  51.                 floating: true,
  52.                 pinned: true,
  53.                 snap:
  54.                     true, // <--- this is required if I want the application bar to show when I scroll up
  55.                 bottom: new TabBar(
  56.                   tabs: <Tab>[
  57.                     new Tab(text: "T"),
  58.                     new Tab(text: "B"),
  59.                   ], // <-- total of 2 tabs
  60.                 ),
  61.               ),
  62.             ];
  63.           },
  64.           body: new TabBarView(
  65.             children: <Widget>[
  66.               new RandomWords(),
  67.               new RandomWords(),
  68.             ], // <--- the array item is a ListView
  69.           ),
  70.         ),
  71.       ),
  72.     );
  73.     // new Scaffold(
  74.     //   body: new NestedScrollView(
  75.     //     controller: _scrollViewController,
  76.     //     headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
  77.     //       return <Widget>[
  78.     //         new SliverAppBar(
  79.     //           title: new Text("NA"),
  80.     //           pinned: true,
  81.     //           floating: true,
  82.     //           forceElevated: innerBoxIsScrolled,
  83.     //           //snap: true,
  84.     //           bottom: new TabBar(
  85.     //             tabs: <Tab>[
  86.     //               new Tab(text: "T"),
  87.     //               new Tab(text: "B"),
  88.     //             ],
  89.     //             controller: _tabController,
  90.     //           ),
  91.     //         )
  92.     //       ];
  93.     //     },
  94.     //     body: new TabBarView(
  95.     //       children: <Widget>[
  96.     //         new RandomWords(),
  97.     //         new RandomWords(),
  98.     //       ],
  99.     //       controller: _tabController,
  100.     //     ),
  101.     //   ),
  102.     // );
  103.     //,    );
  104.   }
  105. }
  106.  
  107. // Creating stateful widget needs two things
  108. // Stateful Widget which creates an instance of a state class.
  109. // So in essence its not the stateful widget which is immutable but the state
  110. // class which persists.
  111. class RandomWords extends StatefulWidget {
  112.   @override
  113.   _RandomWordsState createState() => _RandomWordsState();
  114. }
  115.  
  116. class _RandomWordsState extends State<RandomWords> {
  117.   final _suggestions = <WordPair>[];
  118.   final _biggerFont = TextStyle(fontSize: 40.0);
  119.   List<Note> _notes = List<Note>();
  120.  
  121.   Widget _buildSuggestions() {
  122.     fetchNotes().then((value) {
  123.       setState(() {
  124.         _notes.addAll(value);
  125.       });
  126.     });
  127.     return ListView.builder(
  128.         itemCount: _notes.length,
  129.         padding:
  130.             EdgeInsets.only(top: 10.0, bottom: 10.0, left: 4.0, right: 4.0),
  131.         itemBuilder: (context, i) {
  132.  
  133.           return Card(
  134.             child: new InkWell(
  135.               onTap: () {
  136.                 _navigateToPageDetails(context, _notes[i]);
  137.               },
  138.               child: Padding(
  139.                 padding: const EdgeInsets.only(
  140.                     top: 32.0, bottom: 32.0, left: 16.0, right: 16.0),
  141.                 child: Column(
  142.                   crossAxisAlignment: CrossAxisAlignment.start,
  143.                   children: <Widget>[
  144.                     Text(
  145.                       _notes[i].title,
  146.                       style:
  147.                           TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
  148.                     ),
  149.                     Text(
  150.                       _notes[i].text,
  151.                       style: TextStyle(color: Colors.grey.shade700),
  152.                     ),
  153.                     Image.network(_notes[i].image)
  154.  
  155.                     // Image,
  156.                   ],
  157.                 ),
  158.               ),
  159.             ),
  160.           );
  161.         });
  162.   }
  163.  
  164.   _navigateToPageDetails(BuildContext context, Note item) async {
  165.     //Holds the results returned from PageDetails.
  166.     final resultFromPageDetails = await Navigator.push(
  167.       context,
  168.       MaterialPageRoute(
  169.         builder: (context) => PageDetails(
  170.           item: item,
  171.         ),
  172.       ),
  173.     );
  174.     Scaffold.of(context)
  175.       ..removeCurrentSnackBar()
  176.       ..showSnackBar(SnackBar(content: Text("$resultFromPageDetails")));
  177.   }
  178.  
  179.   Widget _buildRow(WordPair pair) {
  180.     return ListTile(
  181.       title: Text(
  182.         pair.asPascalCase,
  183.         style: _biggerFont,
  184.       ),
  185.     );
  186.   }
  187.  
  188.  
  189.   Future<List<Note>> fetchNotes() async {
  190.     var url =
  191.         '...'
  192.     var response = await http.get(url);
  193.     var notes = List<Note>();
  194.  
  195.     if (response.statusCode == 200) {
  196.      ...
  197.       }
  198.     }
  199.     return notes;
  200.   }
  201.  
  202.   @override
  203.   Widget build(BuildContext context) {
  204.     // final words = WordPair.random();
  205.     // return Text(words.asPascalCase);
  206.     // Now adding the infinite scroll list
  207.     return Scaffold(
  208.       body: _buildSuggestions(),
  209.     );
  210.   }
  211. }
  212.  
  213. class PageDetails extends StatefulWidget {
  214.   PageDetails({Key key, this.item}) : super(key: key);
  215.   final Note item;
  216.   @override
  217.   _PageDetails createState() => new _PageDetails();
  218. }
  219.  
  220. class _PageDetails extends State<PageDetails> {
  221.  
  222.   String description =
  223.       "The Griffith Observatory is the most iconic building in Los Angeles, perched high in the Hollywood Hills, 1,134 feet above sea level.";
  224.   bool isPlaying = false;
  225.   FlutterTts _flutterTts;
  226.  
  227.   @override
  228.   void initState() {
  229.     super.initState();
  230.     initializeTts();
  231.   }
  232.  
  233.   @override
  234.   void dispose() {
  235.     super.dispose();
  236.     _flutterTts.stop();
  237.   }
  238.  
  239.   initializeTts() {
  240.     _flutterTts = FlutterTts();
  241.  
  242.     if (PlatformUtil.myPlatform() == MyPlatform.ANDROID) {
  243.       _flutterTts.ttsInitHandler(() {
  244.         setTtsLanguage();
  245.       });
  246.     } else if (PlatformUtil.myPlatform() == MyPlatform.IOS) {
  247.       setTtsLanguage();
  248.     } else if (PlatformUtil.myPlatform() == MyPlatform.WEB) {
  249.       //not-supported by plugin
  250.     }
  251.  
  252.     _flutterTts.setStartHandler(() {
  253.       setState(() {
  254.         isPlaying = true;
  255.       });
  256.     });
  257.  
  258.     _flutterTts.setCompletionHandler(() {
  259.       setState(() {
  260.         isPlaying = false;
  261.       });
  262.     });
  263.  
  264.     _flutterTts.setErrorHandler((err) {
  265.       setState(() {
  266.         print("error occurred: " + err);
  267.         isPlaying = false;
  268.       });
  269.     });
  270.   }
  271.  
  272.   void setTtsLanguage() async {
  273.     await _flutterTts.setLanguage("en-US");
  274.   }
  275.  
  276.   void speechSettings1() {
  277.     _flutterTts.setVoice("en-us-x-sfg#male_1-local");
  278.     _flutterTts.setPitch(1.5);
  279.     _flutterTts.setSpeechRate(.9);
  280.   }
  281.  
  282.   void speechSettings2() {
  283.     _flutterTts.setVoice("en-us-x-sfg#male_2-local");
  284.     _flutterTts.setPitch(1);
  285.     _flutterTts.setSpeechRate(0.5);
  286.   }
  287.  
  288.   Future _speak(String text) async {
  289.     if (text != null && text.isNotEmpty) {
  290.       var result = await _flutterTts.speak(text);
  291.       if (result == 1)
  292.         setState(() {
  293.           isPlaying = true;
  294.         });
  295.     }
  296.   }
  297.  
  298.   Future _stop() async {
  299.     var result = await _flutterTts.stop();
  300.     if (result == 1)
  301.       setState(() {
  302.         isPlaying = false;
  303.       });
  304.   }
  305.  
  306.   Widget playButton(BuildContext context) {
  307.     return Container(
  308.       child: Stack(
  309.         children: <Widget>[
  310.           Container(
  311.             padding:
  312.                 const EdgeInsets.symmetric(vertical: 5.0, horizontal: 16.0),
  313.             margin: const EdgeInsets.only(
  314.                 top: 30, left: 30.0, right: 30.0, bottom: 20.0),
  315.             child: FlatButton(
  316.               onPressed: () {
  317.                 //fetch another image
  318.                 setState(() {
  319.                   //speechSettings1();
  320.                   //isPlaying ? _stop() : _speak("descriptions yes no");
  321.                   _speak(widget.item.text);
  322.                 });
  323.               },
  324.               child: isPlaying
  325.                   ? Icon(
  326.                       Icons.stop,
  327.                       size: 60,
  328.                       color: Colors.red,
  329.                     )
  330.                   : Icon(
  331.                       Icons.play_arrow,
  332.                       size: 60,
  333.                       color: Colors.green,
  334.                     ),
  335.             ),
  336.           ),
  337.         ],
  338.       ),
  339.     );
  340.   }
  341.  
  342.   @override
  343.   Widget build(BuildContext context) {
  344.     return Scaffold(
  345.       appBar: AppBar(
  346.         title: Text(widget.item.title),
  347.       ),
  348.       body: Column(
  349.         children: [
  350.           Text(
  351.             widget.item.text,
  352.             style: TextStyle(color: Colors.black),
  353.           ),
  354.           Image.network(widget.item.image),
  355.           Text(
  356.             "yolo",
  357.             style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
  358.           ),
  359.           playButton(context),
  360.         ],
  361.       ),
  362.       floatingActionButton: FloatingActionButton(
  363.         child: Icon(Icons.favorite_border),
  364.         onPressed: () {
  365.           //Current item's name along with message is sent back to last screen
  366.           Navigator.pop(context, '${widget.item.title} is marked as favorite.');
  367.         },
  368.       ),
  369.     );
  370.   }
  371. }
  372.  
  373. class TTSPluginRecipe extends StatefulWidget {
  374.   @override
  375.   _TTSPluginRecipeState createState() => _TTSPluginRecipeState();
  376. }
  377.  
  378. class _TTSPluginRecipeState extends State<TTSPluginRecipe> {
  379.   String description =
  380.       "The Griffith Observatory is the most iconic building in Los Angeles, perched high in the Hollywood Hills, 1,134 feet above sea level.";
  381.   bool isPlaying = false;
  382.   FlutterTts _flutterTts;
  383.  
  384.   @override
  385.   void initState() {
  386.     super.initState();
  387.     initializeTts();
  388.   }
  389.  
  390.   @override
  391.   void dispose() {
  392.     super.dispose();
  393.     _flutterTts.stop();
  394.   }
  395.  
  396.   initializeTts() {
  397.     _flutterTts = FlutterTts();
  398.  
  399.     if (PlatformUtil.myPlatform() == MyPlatform.ANDROID) {
  400.       _flutterTts.ttsInitHandler(() {
  401.         setTtsLanguage();
  402.       });
  403.     } else if (PlatformUtil.myPlatform() == MyPlatform.IOS) {
  404.       setTtsLanguage();
  405.     } else if (PlatformUtil.myPlatform() == MyPlatform.WEB) {
  406.       //not-supported by plugin
  407.     }
  408.  
  409.     _flutterTts.setStartHandler(() {
  410.       setState(() {
  411.         isPlaying = true;
  412.       });
  413.     });
  414.  
  415.     _flutterTts.setCompletionHandler(() {
  416.       setState(() {
  417.         isPlaying = false;
  418.       });
  419.     });
  420.  
  421.     _flutterTts.setErrorHandler((err) {
  422.       setState(() {
  423.         print("error occurred: " + err);
  424.         isPlaying = false;
  425.       });
  426.     });
  427.   }
  428.  
  429.   void setTtsLanguage() async {
  430.     await _flutterTts.setLanguage("en-US");
  431.   }
  432.  
  433.   void speechSettings1() {
  434.     _flutterTts.setVoice("en-us-x-sfg#male_1-local");
  435.     _flutterTts.setPitch(1.5);
  436.     _flutterTts.setSpeechRate(.9);
  437.   }
  438.  
  439.   void speechSettings2() {
  440.     _flutterTts.setVoice("en-us-x-sfg#male_2-local");
  441.     _flutterTts.setPitch(1);
  442.     _flutterTts.setSpeechRate(0.5);
  443.   }
  444.  
  445.   Future _speak(String text) async {
  446.     if (text != null && text.isNotEmpty) {
  447.       var result = await _flutterTts.speak(text);
  448.       if (result == 1)
  449.         setState(() {
  450.           isPlaying = true;
  451.         });
  452.     }
  453.   }
  454.  
  455.   Future _stop() async {
  456.     var result = await _flutterTts.stop();
  457.     if (result == 1)
  458.       setState(() {
  459.         isPlaying = false;
  460.       });
  461.   }
  462.  
  463.   @override
  464.   Widget build(BuildContext context) {
  465.     return Scaffold(
  466.       body: Stack(
  467.         children: <Widget>[
  468.           Container(
  469.             height: 360,
  470.             decoration: BoxDecoration(
  471.                 borderRadius: BorderRadius.only(
  472.                     bottomLeft: Radius.circular(50.0),
  473.                     bottomRight: Radius.circular(50.0)),
  474.                 gradient: LinearGradient(
  475.                     colors: [Colors.blue, Colors.blueAccent],
  476.                     begin: Alignment.topLeft,
  477.                     end: Alignment.bottomRight)),
  478.           ),
  479.           Container(
  480.             margin: const EdgeInsets.only(top: 80),
  481.             child: Column(
  482.               children: <Widget>[
  483.                 Padding(
  484.                   padding: const EdgeInsets.all(8.0),
  485.                   child: Center(
  486.                     child: Text(
  487.                       "Griffith Observatory",
  488.                       style: TextStyle(
  489.                           color: Colors.white,
  490.                           fontSize: 28,
  491.                           fontStyle: FontStyle.normal),
  492.                     ),
  493.                   ),
  494.                 ),
  495.                 SizedBox(height: 20.0),
  496.                 Expanded(
  497.                   child: Stack(
  498.                     children: <Widget>[
  499.                       Container(
  500.                         height: double.infinity,
  501.                         margin: const EdgeInsets.only(
  502.                             left: 30.0, right: 30.0, top: 10.0),
  503.                         child: ClipRRect(
  504.                           borderRadius: BorderRadius.circular(30.0),
  505.                           child: Container(
  506.                             height: MediaQuery.of(context).size.height / 1.25,
  507.                             width: MediaQuery.of(context).size.width / 1.25,
  508.                             child: Image.asset(
  509.                                 "assets/images/griffith_observatory.jpg"),
  510.                           ),
  511.                         ),
  512.                       ),
  513.                     ],
  514.                   ),
  515.                 ),
  516.                 Padding(
  517.                   padding: EdgeInsets.all(8.0),
  518.                   child: Text(description),
  519.                 ),
  520.                 playButton(context),
  521.               ],
  522.             ),
  523.           ),
  524.         ],
  525.       ),
  526.     );
  527.   }
  528.  
  529.   Widget playButton(BuildContext context) {
  530.     return Container(
  531.       child: Stack(
  532.         children: <Widget>[
  533.           Container(
  534.             padding:
  535.                 const EdgeInsets.symmetric(vertical: 5.0, horizontal: 16.0),
  536.             margin: const EdgeInsets.only(
  537.                 top: 30, left: 30.0, right: 30.0, bottom: 20.0),
  538.             child: FlatButton(
  539.               onPressed: () {
  540.                 //fetch another image
  541.                 setState(() {
  542.                   //speechSettings1();
  543.                   isPlaying ? _stop() : _speak(description);
  544.                 });
  545.               },
  546.               child: isPlaying
  547.                   ? Icon(
  548.                       Icons.stop,
  549.                       size: 60,
  550.                       color: Colors.red,
  551.                     )
  552.                   : Icon(
  553.                       Icons.play_arrow,
  554.                       size: 60,
  555.                       color: Colors.green,
  556.                     ),
  557.             ),
  558.           ),
  559.         ],
  560.       ),
  561.     );
  562.   }
  563. }
Add Comment
Please, Sign In to add comment