Skip to content

Instantly share code, notes, and snippets.

@oshibka404
Created June 10, 2020 16:30
Show Gist options
  • Save oshibka404/5af8c63a9e9687c7aef06947ca99a4ac to your computer and use it in GitHub Desktop.
Save oshibka404/5af8c63a9e9687c7aef06947ca99a4ac to your computer and use it in GitHub Desktop.
Copy value from one Text widget to another using two state variables
import 'package:flutter/material.dart';
class TextWidget extends StatefulWidget {
_TextWidgetState createState() => _TextWidgetState();
}
class _TextWidgetState extends State<TextWidget> {
String _text1 = "Value to be copied";
String _text2 = "";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
_text1,
key: Key('text1')
),
RaisedButton(
onPressed: (){
setState(() {
_text2 = _text1;
});
},
child: Text("Copy value"),
),
SizedBox(height: 40),
Text(
_text2,
key: Key('text2')
)
],
),
),
);
}
}
// End of the meaningful part
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: TextWidget(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment