How to put this method
void fontControl() {
setState(() {
TextStyle(fontSize: 10 count.toDouble());
if (count == 100) {
const TextStyle(fontSize: 100);
}
});
}
to the text widget
Text(count.toString(), style: fontControl())
CodePudding user response:
wrap it in a GestureDetector widget with a GestureDetector.onTap handler :
onTap: (){
// your function here
},
CodePudding user response:
You can use method to provide style. For this you need to return TextStyle, therefore it cannot be void. Also, you can choose textStyle variable.
You can make it dynamic or better nullable return type. And use SetState on button click
import 'package:flutter/material.dart';
class TestS extends StatefulWidget {
final String? name;
const TestS({
Key? key,
this.name,
}) : super(key: key);
@override
State<TestS> createState() => _TestSState();
}
class _TestSState extends State<TestS> {
int count = 0;
TextStyle? fontControl() {
if (count == 1) {
return TextStyle(fontSize: 100);
}
return TextStyle(fontSize: 10 count.toDouble()); //default
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(onPressed: () {
count ;
setState(() {});
}),
body: Text(
widget.name ?? "default value",
style: fontControl(),
),
);
}
}
CodePudding user response:
You can't do it like that. What you can do is having a private mutable property name _textStyle that should change each time the count variable change.
If I take the flutter counter app example, you will have something like that
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
late TextStyle _textStyle = TextStyle(fontSize: 10.0 _counter);
void _incrementCounter() {
setState(() {
_counter ;
if (_counter > 100) {
_textStyle = const TextStyle(fontSize: 100);
} else {
_textStyle = TextStyle(fontSize: 10.0 _counter);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'$_counter',
style: _textStyle,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
