Created
December 24, 2020 06:50
-
-
Save lattice0/968be8755d5deab1ca5c8c84a993eafc to your computer and use it in GitHub Desktop.
StatefulWidget wont change
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(MyApp()); | |
class MyApp extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() { | |
return _MyAppState(); | |
} | |
} | |
class _MyAppState extends State<MyApp> { | |
int value = 0; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: Text('App Example')), | |
body: Row(children:[ | |
MyStatefulWidget(title: value.toString()), | |
RaisedButton( | |
textColor: Colors.white, | |
color: Colors.blue, | |
onPressed: (){setState(() { value+=1; });}, | |
child: new Text("Add"), | |
) | |
]), | |
), | |
); | |
} | |
} | |
class MyStatefulWidget extends StatefulWidget { | |
MyStatefulWidget({Key key, this.title}):super(key: key); | |
final String title; | |
@override | |
State<StatefulWidget> createState() { | |
return _MyStatefulWidgetState(); | |
} | |
} | |
class _MyStatefulWidgetState extends State<MyStatefulWidget> { | |
String title; | |
@override | |
void initState() { | |
super.initState(); | |
if (widget.title!=null) { | |
title = widget.title; | |
} else { | |
title = ""; | |
} | |
} | |
int value = 0; | |
@override | |
Widget build(BuildContext context) { | |
return Text(title); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment