mercredi 25 août 2021

I'm just a beginner in flutter, I was once a programmer in turbo pascal and visual basic. now I'm studying flutter to develop my own app

I'm trying to Create a Title in LIST I have a counter(from default flutter program) and I want to change the title everytime I click the floating action button. I have list of titles from [a to d] and put in the title. now there's an error if the list value exceed to 4. it returns an error. Can you help me with the program If counter > 5 then the counter will reset to 0 or decrease value... here's the sample 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: Center(child: MyHomePage(title: 'Flutter Demo Home Page!')),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key? key, required this.title}) : super(key: key);
      final String title;
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      final List<String> pamagat = <String>['a', 'b', 'c', 'd'];
    
      void bilangin() {
        if (_counter > 4) {
          _counter = _counter--;
        } else {
          _counter++;
        }
      }
    
      void _incrementCounter() {
        setState(() {
          bilangin();
          //_counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(pamagat[_counter]),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                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),
          ),
        );
      }
    }

Aucun commentaire:

Enregistrer un commentaire