Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Created March 10, 2018 05:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branflake2267/7e84f21a30c1355b2ddef11f89e32867 to your computer and use it in GitHub Desktop.
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.
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,
],
),
);
}
}
@branflake2267
Copy link
Author

branflake2267 commented Mar 10, 2018

https://youtu.be/x3VvE6WukeY - Flutter - Swap Widgets video

videos - flutter

@SekandarAli
Copy link

  • llllllllllllllllllllllllllllllll

  • llllllllll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment