Skip to content

Instantly share code, notes, and snippets.

@georgeherby
Created March 4, 2020 21:56
Show Gist options
  • Save georgeherby/e2720e94fc074316cdcd6976f64b906b to your computer and use it in GitHub Desktop.
Save georgeherby/e2720e94fc074316cdcd6976f64b906b to your computer and use it in GitHub Desktop.
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(
listFixed: [1, 2], stringFixed: "String"),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.stringFixed, this.listFixed}) : super(key: key);
final List<int> listFixed;
final String stringFixed;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<int> _listVariable;
String _stringVariable;
@override
void initState() {
_stringVariable = widget.stringFixed;
_listVariable = widget.listFixed;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("hello"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
_stringVariable,
),
Text(
"Should not update: ${widget.stringFixed}",
),
Text(_listVariable.toString()),
Text("Should not update: ${widget.listFixed.toString()}"),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_counter++;
_listVariable.add(_counter);
_stringVariable = "$_stringVariable $_counter";
});
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment