Created
February 15, 2021 05:31
-
-
Save siumhossain/84eab29ff04ee1191e10a06efc6a6167 to your computer and use it in GitHub Desktop.
custom class in flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Quote { | |
| String text; | |
| String author; | |
| // constructor with named parameters | |
| // Quote({ String author, String text }){ | |
| // this.text = text; | |
| // this.author = author; | |
| // } | |
| // constructor with named parameters | |
| // & automatically assigns named arguments to class properties | |
| Quote({ this.text, this.author }); | |
| } | |
| ========================================main.dirt========================================================= | |
| import 'package:flutter/material.dart'; | |
| import 'quote.dart'; | |
| void main() => runApp(MaterialApp( | |
| home: QuoteList() | |
| )); | |
| class QuoteList extends StatefulWidget { | |
| @override | |
| _QuoteListState createState() => _QuoteListState(); | |
| } | |
| class _QuoteListState extends State<QuoteList> { | |
| // List<String> quotes = [ | |
| // 'Be yourself; everyone else is already taken', | |
| // 'I have nothing to declare except my genius', | |
| // 'The truth is rarely pure and never simple' | |
| // ]; | |
| List<Quote> quotes = [ | |
| Quote(author: 'Oscar Wilde', text: 'Be yourself; everyone else is already taken'), | |
| Quote(author: 'Oscar Wilde', text: 'I have nothing to declare except my genius'), | |
| Quote(author: 'Oscar Wilde', text: 'The truth is rarely pure and never simple') | |
| ]; | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| backgroundColor: Colors.grey[200], | |
| appBar: AppBar( | |
| title: Text('Awesome Quotes'), | |
| centerTitle: true, | |
| backgroundColor: Colors.redAccent, | |
| ), | |
| body: Column( | |
| children: quotes.map((quote) => Text('${quote.text} - ${quote.author}')).toList(), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment