rajath_pai

ninja20 Extracting widgets

Jul 31st, 2021
1,338
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ___________________________
  2. //main.dart
  3. ___________________________
  4. import 'package:flutter/material.dart';
  5. import 'quote.dart';
  6. import 'quote_card.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.  
  26.   Widget build(BuildContext context) {
  27.     return Scaffold(
  28.       backgroundColor: Colors.grey[200],
  29.       appBar: AppBar(
  30.         title: Text('Quote'),
  31.         centerTitle: true,
  32.         backgroundColor: Colors.redAccent,
  33.       ),
  34.       body : Column(
  35.         children : quotes.map((quote) => QuoteCard(quote : quote)).toList(),
  36.       ),
  37.     );
  38.   }
  39. }
  40.  
  41.  
  42.  
  43. ___________________________
  44. //quote_card.dart
  45. ___________________________
  46.  
  47. import 'package:flutter/material.dart';
  48. import 'quote.dart';
  49.  
  50. class QuoteCard extends StatelessWidget {
  51.  
  52.   final Quote quote;
  53.   QuoteCard({ required this.quote });
  54.  
  55.   @override
  56.   Widget build(BuildContext context) {
  57.     return Card(
  58.       margin : const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
  59.       child : Padding(
  60.         padding: const EdgeInsets.all(12.0),
  61.         child: Column(
  62.           crossAxisAlignment: CrossAxisAlignment.stretch,
  63.           children: <Widget>[
  64.             Text(
  65.               quote.text,
  66.               style: TextStyle(
  67.                 fontSize: 18.0,
  68.                 color : Colors.grey[600],
  69.               ),
  70.             ),
  71.             SizedBox(height: 6.0),
  72.             Text(
  73.                 quote.author,
  74.                 style : TextStyle(
  75.                   fontSize : 14.0,
  76.                   color : Colors.grey[800],
  77.                 )
  78.             ),
  79.           ],
  80.         ),
  81.       ),
  82.     );
  83.   }
  84. }
  85.  
  86. ___________________________
  87. //quote.dart
  88. ___________________________
  89.  
  90. class Quote{
  91.  
  92.   String text;
  93.   String author;
  94.  
  95.   Quote( { required this.text, required this.author });
  96. }
  97.  
  98. //This is one way to do it
  99. // Quote(String text, String author){
  100. //   this.text = text;
  101. //   this.author = author;
  102. // }
  103. //The instance for this class would look like
  104.  
  105. // so here you'll have to maintian the same order that is
  106. // have the quote first and then the author
  107.  
  108. //The second way to do it is by parameterising it
  109. // Quote( { String text, String author } ){ .... }
  110. // when does this help? => When creating an instance of the class
  111. // the order in which you input the values wont matter
  112. // Quote myquote = Quote(text : 'Dont get mad, get even', author : 'Jesus')
  113. // This can also be jumbled and written as
  114. // Quote myquote = Quote(author : 'Jesus' , text : 'Dont get mad, get even')
  115.  
RAW Paste Data