Created
March 10, 2018 05:28
-
-
Save branflake2267/7e84f21a30c1355b2ddef11f89e32867 to your computer and use it in GitHub Desktop.
Flutter - Swap Widgets. Easily swap widgets using internal or external state variables in Flutter.
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
import 'package:flutter/material.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(swap: false), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
final bool swap; | |
MyHomePage({Key key, this.swap}) : super(key: key); | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
bool swap = false; | |
@override | |
void initState() { | |
swap = widget.swap; | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
var buttonTile = new ListTile( | |
title: new RaisedButton( | |
child: new Text("Swap Widget"), | |
onPressed: (){ | |
setState((){ | |
swap = !swap; | |
}); | |
} | |
), | |
); | |
Widget swapWidget; | |
if (swap) { | |
swapWidget = new Text("Brandon"); | |
} else { | |
swapWidget = new Icon(Icons.cake); | |
} | |
var swapTile = new ListTile( | |
title: swapWidget, | |
); | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text("App Bar Title"), | |
), | |
body: new ListView( | |
children: <Widget>[ | |
buttonTile, | |
swapTile, | |
], | |
), | |
); | |
} | |
} |
-
llllllllllllllllllllllllllllllll
- llllllllll
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://youtu.be/x3VvE6WukeY - Flutter - Swap Widgets video