survey app

mail@pastecode.io avatar
unknown
dart
11 days ago
6.4 kB
2
Indexable
Never
import 'package:myapp/components/topbar.dart';
import 'package:myapp/home_page.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(SurveiPage());
}

class SurveiPage extends StatelessWidget {
  const SurveiPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Survei',
      home: MySurveiPage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MySurveiPage extends StatefulWidget {
  const MySurveiPage({Key? key}) : super(key: key);

  @override
  State<MySurveiPage> createState() => _MySurveiPageState();
}

class _MySurveiPageState extends State<MySurveiPage> {
  final _formKey = GlobalKey<FormState>();
  int _currentQuestionIndex = 0;

  void _nextQuestion() {
    if (_formKey.currentState!.validate()) {
      setState(() {
        _currentQuestionIndex++;
      });
    }
  }

  void _prevQuestion() {
    setState(() {
      _currentQuestionIndex--;
    });
  }

  @override
  void initState() {
    super.initState();
  }

  Widget _buildQuestion(int index) {
    switch (index) {
      case 0:
        return Expanded(
          child: ListView(
            shrinkWrap: true,
            children: [
              TextFormField(
                decoration: InputDecoration(
                  labelText: 'Nama',
                  border: OutlineInputBorder(),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter an answer';
                  }
                  return null;
                },
              ),
              SizedBox(
                height: 50,
              ),
              TextFormField(
                decoration: InputDecoration(
                  labelText: 'Alamat',
                  border: OutlineInputBorder(),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter an answer';
                  }
                  return null;
                },
              ),
            ],
          ),
        );
      case 1:
        return TextFormField(
          decoration: InputDecoration(
            labelText: 'Question 1',
            border: OutlineInputBorder(),
          ),
          validator: (value) {
            if (value == null || value.isEmpty) {
              return 'Please enter an answer';
            }
            return null;
          },
        );
      case 2:
        return TextFormField(
          decoration: InputDecoration(
            labelText: 'Question 2',
            border: OutlineInputBorder(),
          ),
          validator: (value) {
            if (value == null || value.isEmpty) {
              return 'Please enter an answer';
            }
            return null;
          },
        );
      case 3:
        return TextFormField(
          decoration: InputDecoration(
            labelText: 'Question 3',
            border: OutlineInputBorder(),
          ),
          validator: (value) {
            if (value == null || value.isEmpty) {
              return 'Please enter an answer';
            }
            return null;
          },
        );
      case 4:
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // result
          ],
        );
      default:
        return Container();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TopBar(
                title: 'Survei',
                ontap: () => Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                    builder: (context) {
                      return const HomePage();
                    },
                  ),
                ),
              ),
              SizedBox(
                height: 30,
              ),
              Expanded(
                child: AnimatedSwitcher(
                  duration: Duration(milliseconds: 300),
                  child: _buildQuestion(_currentQuestionIndex),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  if (_currentQuestionIndex > 0)
                    _buildButton(
                      () {
                        _prevQuestion();
                      },
                      Icons.arrow_back_ios_new_rounded,
                    ),
                  if (_currentQuestionIndex < 4)
                    _buildButton(
                      () {
                        _nextQuestion();
                      },
                      Icons.arrow_forward_ios_rounded,
                    ),
                  if (_currentQuestionIndex == 4)
                    ElevatedButton(
                      onPressed: () {
                        //
                      },
                      child: Text('Submit'),
                    ),
                ],
              ),
              SizedBox(
                height: 20,
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildButton(VoidCallback onTap, IconData iconData) {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 25.0),
        child: InkWell(
          onTap: onTap,
          borderRadius: BorderRadius.circular(25),
          child: Container(
            height: 60,
            padding: const EdgeInsets.all(20),
            decoration: BoxDecoration(
              color: Colors.black,
              borderRadius: BorderRadius.circular(25),
            ),
            child: Center(
              child: Icon(
                iconData,
                size: 12,
                color: Colors.white,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
Leave a Comment