Reputation: 103
I have the following code where i have buttons. i wpould like to get the numbers of the buttons to store the data and then also display the values on the screen in the text boxes in the order of them being inputted.
I will later on need to store the data in a DB when a user clicks next
How many numbers can be pressed will depend on a variable so it can vary between 1 and 12 numbers before hitting next.
I number which has been inputted should also be able to be changed. Should they then be a button aswell or how would i go about this?
THanks so much for the help
import 'package:flutter/material.dart';
import 'package:flutter_grid_button/flutter_grid_button.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.purple,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
Size screenSize(BuildContext context) {
return MediaQuery.of(context).size;
}
double screenHeight(BuildContext context, {double dividedBy = 1}) {
return screenSize(context).height / dividedBy;
}
double screenWidth(BuildContext context, {double dividedBy = 1}) {
return screenSize(context).width / dividedBy;
}
List<String> test =[];
@override
Widget build(BuildContext context) {
const textStyle = TextStyle(fontSize: 26);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('VIS Scoring'),
),
body: Builder(builder: (context) {
return Padding(
padding: EdgeInsets.only(top: 15, bottom: 0, left: 5, right: 5),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
if(test == [])...[
Text("A is greater than 10"),
]else...[
Text("A is less than or Equal to 10")
],
Text(
"Hello World",
),
Text(
test.toString(),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
"Hello World",
),
Text(
"Hello World",
),
Text(
"Hello World",
),
],
),
Expanded(
//padding: const EdgeInsets.all(18.0),
child: GridButton(
textStyle: textStyle,
borderColor: Colors.grey[300],
borderWidth: 2,
onPressed: (dynamic val) {
test.add(val);
print(test);
// ScaffoldMessenger.of(context).showSnackBar(
// SnackBar(
// content: Text(val.toString()),
// duration: Duration(milliseconds: 4000),
// ),
//
// );
},
items: [
[
GridButtonItem(
title: "9",
),
GridButtonItem(
title: "10",
),
GridButtonItem(
title: "X",
),
],
[
GridButtonItem(
title: "6",
),
GridButtonItem(
title: "7",
),
GridButtonItem(
title: "8",
),
],
[
GridButtonItem(
title: "3",
),
GridButtonItem(
title: "4",
),
GridButtonItem(
title: "5",
),
],
[
GridButtonItem(
title: "0",
),
GridButtonItem(
title: "1",
color: Colors.white,
textStyle: textStyle.copyWith(
color: Colors.black, fontWeight: FontWeight.bold),
),
GridButtonItem(
title: "2",
color: Colors.white,
textStyle: textStyle.copyWith(
color: Colors.black, fontWeight: FontWeight.bold),
),
],
[],
[
GridButtonItem(
title: "Back",
color: Colors.blue[900],
textStyle: textStyle.copyWith(
color: Colors.black, fontWeight: FontWeight.bold),
),
GridButtonItem(
title: "Next",
color: Colors.deepOrangeAccent,
textStyle: textStyle.copyWith(
color: Colors.black, fontWeight: FontWeight.bold),
),
]
],
),
),
],
),
);
}),
),
);
}
}
Upvotes: 1
Views: 301
Reputation: 539
Upvotes: 1