How to Implement fontSize adjustment dialog box in flutter? - flutter

I am new to flutter. I have taken inspiration from this app https://play.google.com/store/apps/details?id=com.nikitadev.usconstitution&hl=en and I am implementing the fontsize adjustment dialogbox with flutter. I am really stuck.

Make static variable and use the value in your default theme. When font size is increased set the value of that variable to this font size.
//Initialize a variable at the top to change
double _textDefaultSize = 16;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
textTheme: TextTheme(
bodyText1: TextStyle(
fontSize: _textDefaultSize,
//Use the text Size in the main theme
),
),
),
home: Scaffold(
backgroundColor: Colors.red,
appBar: AppBar(
title: Text("TextColor checking"),
),
body: Center(
child: Column(
children: <Widget>[
Text(
"Example Text",
style: TextStyle(fontSize: _textDefaultSize),
//Use the font size in all texts
),
FlatButton(
onPressed: () {
setState() {
_textDefaultSize++; //Change the value of variable here
}
},
child: Text("Increase Size"),
),
FlatButton(
onPressed: () {
setState() {
_textDefaultSize--; //Change the value of variable here
}
},
child: Text("Increase Size"),
),
],
),
),
),
);
}
}
```

Related

How to change the Value of a Widget in Flutter through dart

i would like to know o can i change the value of a child widget in flutter if a certain condition is met, for example the color of an icon in the trailing
Here is some pseudo-code:
if(condition){
trailing Icon(Icons.favorite).color = Colors.red[500]
}else{
trailing = Icon(Icons.Favorite).color = Colors.blue[300]
}
Thank you.
you wanna something like this?
if yes, try this code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool colorIndex = true;
void _changeColor(val) {
setState(() {
this.colorIndex = val;
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: Text('Welcome to Flutter'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_changeColor(!colorIndex);
},
child: Icon(Icons.touch_app),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(50.0),
child: Card(
child: ListTile(
title: Text('Click FAB to change color'),
trailing: Icon(
Icons.favorite,
color: colorIndex ? Colors.red[500] : Colors.blue[300],
),
),
),
),
),
),
);
}
}
You can change anything under any condition you define. The most simple example is using setState to update a value that can be inspected during build. This value could change under any condition you like. Calling setState will trigger the UI to rebuild (calls the build method).
Here is a Widget. It displays the text "Hello, World!" in the center of the screen. The AppBar at the top has a leading IconButton Widget. When the IconButton is pressed, it will toggle the color of the "Hello, World!" text. It does this by updating the state of Widget and toggling the value of the blue variable. The condition is: if (blue) {} or "if blue is equal to true then change the color." During the build of the UI, the code inspects the value of blue and determines what TextStyle to apply to the "Hello, World!" text.
import 'package:flutter/material.dart';
class ColorChangeWidget extends StatefulWidget {
#override
State<StatefulWidget> createState() => _ColorChangeWidgetState();
}
class _ColorChangeWidgetState extends State<ColorChangeWidget> {
bool blue = false;
#override
Widget build(BuildContext context) {
TextStyle style = TextStyle(color: Colors.black);
if (blue) {
style = TextStyle(color: Colors.blue);
}
return Scaffold(
appBar: AppBar(
title: Text("Test"),
centerTitle: true,
leading: IconButton(
icon: Icon(Icons.add),
onPressed: () {
setState(() {
blue = !blue;
});
},
),
),
body: Container(
alignment: Alignment.center,
padding: EdgeInsets.all(8.0),
child: Text("Hello, World!", style: style)
)
);
}
}

FLUTTER: How to set auto font size in TextSpan

I want to implement that text with divider in between lines:
Smart City Text
Do you guys have any ideas how can I implement that in flutter? I tried few things but it is kind of a challenge for me, because I am beginner. Divider line have to be long as text above and under line.
You can make a screen using auto_size_text package.
Using AutoSizeText and AutoSizeText.rich widget in that package,
and set a maxLines option to 1 and set font size big.
Because we set maxLines to 1, although set font size big, text will not be overflowed screen.
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _buildBody() {
return Container(
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
AutoSizeText(
'Smart City',
maxLines: 1,
style: TextStyle(
fontSize: 100,
fontWeight: FontWeight.bold,
),
),
Divider(color: Colors.black),
AutoSizeText.rich(
TextSpan(
children: [
TextSpan(
text: 'Advanced',
),
TextSpan(
text: ' urban',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
TextSpan(
text: ' lighting solution',
),
],
),
style: TextStyle(fontSize: 100),
maxLines: 1,
),
],
),
);
}
}

Initiate the display of multiple text sequentially using animated_text_kit plugin on flutter

I have implemented a TyperAnimatedTextKit as so:
TyperAnimatedTextKit(
onTap: () {
print("Tap Event");
},
text: [
"Welcome to this application!",
"This application demonstrates TyperAnimatedTextKit",
],
displayFullTextOnTap: true,
isRepeatingAnimation: false,
speed: new Duration(milliseconds: 50),
textStyle: TextStyle(
fontSize: 24.0,
fontFamily: "Arvo-Regular",
color: Colors.white,
),
textAlign: TextAlign.center,
alignment: AlignmentDirectional.centerStart,
onFinished: intiateWidget2(),
)
Now I need to use intiateWidget2() to start another TyperAnimatedTextKit to type.
Basically I want the second 'TyperAnimatedTextKit' to start typing after the first one finishes.
Please advise as to how I should go about implementing this.
Edit 1(04/07/2020): The two TyperAnimatedTextKit widgets need to be implemented at different locations.
A simple way to do this is use a variable for the TyperAnimatedTextKit widget in the Widget tree, initialize that variable in initState() with an instance of TyperAnimatedTextKit (the first instance), then, in initiateWidget2, set that variable to a new instance of TyperAnimatedTextKit (the second instance), in a setState() call. The catch is, the TyperAnimatedTextKits have to be initialized with a different key or the animation won't play for the second instance. I tried to put this in code below, hth:
import 'package:flutter/material.dart';
import 'package:animated_text_kit/animated_text_kit.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
canvasColor: Colors.black,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TyperAnimatedTextKit _animatedTextWidget;
Widget _animatedTextWidget2 = Container();
#override
void initState() {
_animatedTextWidget = intiateWidget1();
super.initState();
}
TyperAnimatedTextKit intiateWidget1() {
print('initiate 1');
return TyperAnimatedTextKit(
key: GlobalKey(),
onTap: () {
print("Tap Event");
},
text: [
"Welcome to this application!",
"This application demonstrates TyperAnimatedTextKit",
],
displayFullTextOnTap: true,
isRepeatingAnimation: false,
speed: new Duration(milliseconds: 50),
textStyle: TextStyle(
fontSize: 24.0,
fontFamily: "Arvo-Regular",
color: Colors.white,
),
textAlign: TextAlign.center,
alignment: AlignmentDirectional.centerStart,
onFinished: intiateWidget2
);
}
void intiateWidget2() {
print('in initiate 2');
setState(() {
this._animatedTextWidget2 = TyperAnimatedTextKit(
key: GlobalKey(),
onTap: () {
print("Tap Event");
},
text: [
"Let's get started",
"with our top secret impossible mission",
],
displayFullTextOnTap: true,
isRepeatingAnimation: false,
speed: new Duration(milliseconds: 50),
textStyle: TextStyle(
fontSize: 24.0,
fontFamily: "Arvo-Regular",
color: Colors.green,
),
textAlign: TextAlign.center,
alignment: AlignmentDirectional.centerStart,
onFinished: () => print('finished the second'));
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
this._animatedTextWidget,
Container(height: 15,),
this._animatedTextWidget2
],
),
)
);
}
}

In flutter, can you set the appbar backgorund to change base on the value of a dropdown box?

my drop down box cycles through 5 strings
['blue','red','yellow','orange','grey']
I want my appbar title to be that dropdown box and for the value in the dropdown to determine the appbar color
DropDownWidget ddw = DropDownWidget();
var color = {
"blue": Colors.blue,
"red": Colors.red,
"yellow": Colors.yellow,
"orange": Colors.orange,
"grey": Colors.grey,
};
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: ddw,
backgroundColor: color[ddw],
),
}
The dropdown (ddw) shows up as the title, no problem.
I made a dictionary with those strings as the keys and the corresponding color as the value, but I am not able to use the string value of the dropdown to change the background.
Any suggestions?
You can copy paste run full code below
You can call setState in onChanged of DropdownButton
code snippet
appBar: AppBar(
backgroundColor: _appbarColor,
...
DropdownButton<Item>(
hint: Text("Select item"),
value: selectedColor,
onChanged: (Item Value) {
setState(() {
selectedColor = Value;
_appbarColor = selectedColor.color;
});
},
working demo
full code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class Item {
const Item(this.name, this.color);
final String name;
final Color color;
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Color _appbarColor = Colors.blue;
Item selectedColor;
List<Item> colorList = <Item>[
const Item('blue', Colors.blue),
const Item('red', Colors.red),
const Item('yellow', Colors.yellow),
];
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: _appbarColor,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton<Item>(
hint: Text("Select item"),
value: selectedColor,
onChanged: (Item Value) {
setState(() {
selectedColor = Value;
_appbarColor = selectedColor.color;
});
},
items: colorList.map((Item item) {
return DropdownMenuItem<Item>(
value: item,
child: Row(
children: <Widget>[
Container(
height: 15,
width: 15,
color: item.color,
),
SizedBox(
width: 10,
),
Text(
item.name,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList()),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

Using FontAwesomeIcons with value from custom class in Flutter

I'm trying to use fontawesome together with flutter. Depending on certain content of an Item I'd like to display a certain Icon from fontawesome.
Transaction(id: 1, title: 'lunch', amount: -23.10, type:'utensils'),
Transaction(id: 2, title: 'new shows', amount: -59.99, type:'tshirt'),
Transaction(id: 3, title: 'Falcon launch', amount: -62000000, type:'rocket')
so, I'd like to use the type as an indicator for my fontawesome icon.
When using FontAwesomeIcons.rocket, everything works quite well.
Column(children: <Widget>[
Card(
child: IconButton(
onPressed: null,
icon: new Icon(FontAwesomeIcons.rocket),
),
elevation: 0,
)
],),
since I'm using the map function I'm able to call the type itself without an issue like Text(tx.type). Is there a way to replace the static (in my case) rocket with the type from my transaction class? I'm trying to avoid if/switch cases at the moment just to get the basics going.
Any help very appreciated.
You can copy paste run full code below
You can use https://pub.dev/packages/icons_helper
just prefix rocket with fa.rocket
code snippet
Icon(getIconUsingPrefix(name: 'fa.rocket'),
color: Theme.of(context).backgroundColor, size: 60.0),
working demo
full code
import 'package:flutter/material.dart';
import 'package:icons_helper/icons_helper.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo + Icon Helper Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Map iconMap = {"a":'fa.rocket'};
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(getIconUsingPrefix(name: 'fa.fiveHundredPx'),
color: Theme.of(context).backgroundColor, size: 60.0),
Icon(getIconUsingPrefix(name: 'fa.rocket'),
color: Theme.of(context).backgroundColor, size: 60.0),
Icon(getIconUsingPrefix(name: iconMap["a"]),
color: Theme.of(context).backgroundColor, size: 60.0),
Text(
'There should be an icon above. It\'s neat, isn\'t?\n\nYou can also push the + button and increment this counter for fun:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}