プログラミング初心者です。
アプリ上で入力した値を足し算して、結果を表示したいのですが表示してくれません。
恥ずかしながら、どこに不備があるかわからないため、指摘いただけると幸いです。
よろしくお願いします。
Dart
1import 'package:flutter/material.dart'; 2 3void main() { 4 runApp(const MyApp()); 5} 6 7class MyApp extends StatelessWidget { 8 const MyApp({Key? key}) : super(key: key); 9 10 // This widget is the root of your application. 11 12 Widget build(BuildContext context) { 13 return MaterialApp( 14 title: 'Flutter Demo', 15 theme: ThemeData( 16 17 primarySwatch: Colors.blue, 18 ), 19 home: const MyHomePage(title: 'Flutter Demo Home Page'), 20 ); 21 } 22} 23 24class MyHomePage extends StatefulWidget { 25 const MyHomePage({Key? key, required this.title}) : super(key: key); 26 27 28 final String title; 29 30 31 State<MyHomePage> createState() => _MyHomePageState(); 32} 33 34class _MyHomePageState extends State<MyHomePage> { 35 int _counter = 0; 36 37 void _incrementCounter() { 38 setState(() { 39 40 _counter++; 41 }); 42 } 43 44 var _ControllerA = TextEditingController(); 45 var _ControllerB = TextEditingController(); 46 var _ControllerC = TextEditingController(); 47 48 int A = 0; 49 int B = 0; 50 int C = 0; 51 52 53 void initState() { 54 super.initState(); 55 setState(() { 56 _ControllerA = new TextEditingController(); 57 _ControllerB = new TextEditingController(); 58 _ControllerC = new TextEditingController(); 59 }); 60 } 61 62 63 void dispose() { 64 super.dispose(); 65 print("dispose"); 66 } 67 68 69 70 Widget build(BuildContext context) { 71 72 return Scaffold( 73 appBar: AppBar( 74 75 title: Text(widget.title), 76 ), 77 body: Center( 78 79 child: Column( 80 81 children: <Widget>[ 82 83 Padding( 84 padding: EdgeInsets.all(10), 85 child: TextField( 86 controller: _ControllerA, 87 decoration: InputDecoration( 88 border: OutlineInputBorder(), 89 labelText: 'A', 90 ), 91 ), 92 ), 93 94 Padding( 95 padding: EdgeInsets.all(10), 96 child: TextField( 97 controller: _ControllerB, 98 decoration: InputDecoration( 99 border: OutlineInputBorder(), 100 labelText: 'B', 101 ), 102 ), 103 ), 104 Container( 105 padding: EdgeInsets.all(16.0), 106 child: RaisedButton( 107 child: Text('計算'), 108 onPressed: () { 109 setState(() { 110 C = A + B; 111 print('$C'); 112 }); 113 }, 114 ), 115 ), 116 Text( 117 "A+B='$C'", 118 style: TextStyle( 119 fontSize:30, 120 ), 121 ), 122 ], 123 ), 124 ), 125 ); 126 } 127} 128
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。