rajath_pai

ninja18 List, class, text & author

Jul 31st, 2021
1,677
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ___________________________
  2. //main.dart
  3. ___________________________
  4.  
  5. import 'package:flutter/material.dart';
  6. import 'quote.dart';
  7.  
  8. void main() => runApp(MaterialApp(
  9.   home: QuoteList(),
  10. ));
  11.  
  12. class QuoteList extends StatefulWidget {
  13.   @override
  14.   _QuoteListCardState createState() => _QuoteListCardState();
  15. }
  16.  
  17. class _QuoteListCardState extends State<QuoteList> {
  18.  
  19.   List<Quote> quotes = [
  20.     Quote(author: 'Pai', text: 'Hello world'),
  21.     Quote(author: 'Raj', text: 'Hello there'),
  22.     Quote(author: 'Jordan', text: 'It became personal with me')
  23.   ];
  24.  
  25.   Widget build(BuildContext context) {
  26.     return Scaffold(
  27.       backgroundColor: Colors.grey[200],
  28.       appBar: AppBar(
  29.         title: Text('Quote'),
  30.         centerTitle: true,
  31.         backgroundColor: Colors.redAccent,
  32.       ),
  33.       body : Column(
  34.         children : quotes.map((quote) => Text('${quote.text} - ${quote.author}')).toList(),
  35.       ),
  36.     );
  37.   }
  38. }
  39. ___________________________
  40. //quote.dart
  41. ___________________________
  42. class Quote{
  43.  
  44.   String text;
  45.   String author;
  46.  
  47.   Quote( { required this.text, required this.author });
  48. }
  49.  
  50. //This is one way to do it
  51. // Quote(String text, String author){
  52. //   this.text = text;
  53. //   this.author = author;
  54. // }
  55. //The instance for this class would look like
  56.  
  57. // so here you'll have to maintain the same order that is
  58. // have the quote first and then the author
  59.  
  60. //The second way to do it is by parameterising it
  61. // Quote( { String text, String author } ){ .... }
  62. // when does this help? => When creating an instance of the class
  63. // the order in which you input the values wont matter
  64. // Quote myquote = Quote(text : 'Dont get mad, get even', author : 'Jesus')
  65. // This can also be jumbled and written as
  66. // Quote myquote = Quote(author : 'Jesus' , text : 'Dont get mad, get even')
  67.  
RAW Paste Data