Last active
February 18, 2021 16:39
-
-
Save yaashwardhan/d8d96899aeef474e2add014618919f25 to your computer and use it in GitHub Desktop.
Passing values from Stateless Widgets to other Stateless Widgets and Making New widgets to write cleaner code and remove repetitive code
This file contains 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
//Passing values from Stateless Widgets to other Stateless Widgets and Making New widgets to write cleaner code and remove repetitive code | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark(), | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Stateless/Clean Code'), | |
), | |
body: StatelessOne(),),);} | |
} | |
class StatelessOne extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
Text( | |
'Widget color can be customised without \nhaving to retype the entire code'), | |
StatelessTwo(myColor = Colors | |
.green), //here we wrote cleaner code, because made the repeated code as a custom widget. | |
StatelessTwo(Colors.pink), | |
StatelessTwo(Colors.blue), | |
StatelessTwo(Colors.orange), | |
],),);} | |
} | |
//creating our own stateless widget which we will use multiple times in the first class called StatelessOne | |
class StatelessTwo extends StatelessWidget { | |
// for e.g u have a UI that makes these boxes above (there are other ways to do it also but for begginers use this without going into listview etc.) | |
final Color myColor; | |
StatelessTwo(this.myColor); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
height: 120, | |
width: 250, | |
color: myColor, | |
child: Center(child: Text('Your Repetitive Widget')), | |
);} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment