'Flutter : how to actualize the state of a global variable in every widget?
so i have 2 pages and 1 file where i have my variable . when i run the app my variable total is update every time i enter income page or add a new income . but when i navigate back to the home page the total shows only the first value ive entered .
Home.dart
import 'package:flutter/material.dart';
import 'componets.dart';
import 'income.dart';
import 'variables.dart';
....
bottomNavigationBar: Container(
color: Colors.white,
child: Row(
children: <Widget>[
Expanded(
child: ListTile(
title: new Text("Balance:"),
subtitle: new Text("$Total"),
),
),
the variable total is shown in the bottom of home page
Income.dart
import 'package:flutter/material.dart';
import 'componets.dart';
import 'home.dart';
import 'variables.dart';
class Income extends StatefulWidget {
const Income({Key? key}) : super(key: key);
@override
State<Income> createState() => _IncomeState();
}
class _IncomeState extends State<Income> {
TextEditingController _textFieldController = TextEditingController();
List<double> transactions = [];
Future<void> _displayTextInputDialog(BuildContext context) async {
double Amount = 0;
String valueText = '';
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Add income'),
content: TextField(
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
valueText = value;
});
},
controller: _textFieldController,
),
actions: <Widget>[
FlatButton(
color: Colors.red,
textColor: Colors.white,
child: Text('CANCEL'),
onPressed: () {
setState(() {
Navigator.pop(context);
});
},
),
FlatButton(
color: Colors.green,
textColor: Colors.white,
child: Text('OK'),
onPressed: () {
setState(() {
Amount = double.parse(valueText);
transactions.add(Amount);
Total = Total + Amount;
print(Amount);
Navigator.pop(context);
});
},
),
],
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Income"),
backgroundColor: Colors.deepOrange,
centerTitle: false,
elevation: 1.0,
),
body: Center(
child: FlatButton(
color: Colors.teal,
textColor: Colors.white,
onPressed: () {
_displayTextInputDialog(context);
},
child: Text('Press For Alert'),
),
),
// NavigationBar
bottomNavigationBar: Container(
color: Colors.white,
child: Row(
children: <Widget>[
Expanded(
child: ListTile(
title: Text("Balance:"),
subtitle: Text('${Total}'),
),
),
the variable in the same place as the home page variables.dart
double Total = 0;
Solution 1:[1]
I have created example of updating total value using provider by refering yours
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
//Provider for updating totalvalue
class TotalValue with ChangeNotifier {
double _total = 0;
double get value => _total;
void sum(double val) {
_total += val;
notifyListeners();
}
}
//Income Page
class Income extends StatefulWidget {
const Income({Key? key}) : super(key: key);
@override
State<Income> createState() => _IncomeState();
}
class _IncomeState extends State<Income> {
TextEditingController _textFieldController = TextEditingController();
List<double> transactions = [];
Future<void> _displayTextInputDialog(BuildContext context) async {
double Amount = 0;
String valueText = '';
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Add income'),
content: TextField(
keyboardType: TextInputType.number,
onChanged: (value) {
setState(() {
valueText = value;
});
},
controller: _textFieldController,
),
actions: <Widget>[
FlatButton(
color: Colors.red,
textColor: Colors.white,
child: Text('CANCEL'),
onPressed: () {
Navigator.pop(context);
},
),
FlatButton(
color: Colors.green,
textColor: Colors.white,
child: Text('OK'),
onPressed: () {
Amount = double.parse(valueText);
transactions.add(Amount);
context.read<TotalValue>().sum(Amount);
Navigator.pop(context);
},
),
],
);
});
}
@override
Widget build(BuildContext context) {
return Center(
child: FlatButton(
color: Colors.teal,
textColor: Colors.white,
onPressed: () {
_displayTextInputDialog(context);
},
child: Text('Press For Alert'),
),
);
}
}
//Home Page
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int _selectedIndex = 0;
static TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static List<Widget> _widgetOptions(BuildContext context) {
return <Widget>[
Income(),
Text(
'${context.watch<TotalValue>().value}',
style: optionStyle,
),
];
}
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Income'),
),
body: Center(
child: _widgetOptions(context).elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.money),
label: 'Income',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mohit yadav |
