Home > Blockchain >  Why application is falling
Why application is falling

Time:04-21

I'm try to changed colors of appbar and container in the random way, using provider. But, as always, all go wrong( Where did i make a mistake? I've read some topics on this thema, but don't understand, how does it work

import 'package:provider/provider.dart';
import 'dart:math';
void main() => runApp(MyApp());

class SwitchProvider extends ChangeNotifier {
  bool is_switched = false;

  bool _switch() {
    is_switched = !is_switched;
    notifyListeners();
    return is_switched;
  }
}

class MyApp extends StatelessWidget {
  bool is_changed = true;

  @override
  Widget build(BuildContext context) {
    SwitchProvider _state = Provider.of<SwitchProvider>(context);
    return MaterialApp(
      home: ChangeNotifierProvider<SwitchProvider>.value(
        value: SwitchProvider(),
        child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text(
              "Homework Provider",
            ),
            backgroundColor: Color(
              generateRandomHexColor(),
            ),
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                color: Color(
                  generateRandomHexColor(),
                ),
                height: 200,
                width: 200,
              ),
              Consumer(
                builder: (context, value, child) => Switch(
                  value: is_changed,
                  onChanged: (is_changed) => _state.is_switched,
                  // onChanged: (bool is_changed) => !is_changed,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Random random = new Random();

int generateRandomHexColor() {
  int length = 6;
  String chars = '0123456789ABCDEF';
  String hex = '';
  while (length-- > 0) hex  = chars[(random.nextInt(16)) | 0];
  print(hex);
  return int.parse("0xFF"   hex);
}

CodePudding user response:

You need to firstly initialize your provider and then you can access the provider. So you need to wrap your scaffold into another widget. So that you can access the provider after initializing the provider in material app widget. The code below should work.

import 'package:provider/provider.dart';
import 'dart:math';
void main() => runApp(MyApp());

class SwitchProvider extends ChangeNotifier {
  bool is_switched = false;

  bool _switch() {
    is_switched = !is_switched;
    notifyListeners();
    return is_switched;
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: ChangeNotifierProvider<SwitchProvider>.value(
        value: SwitchProvider(),
        child: Example()
      ),
    );
  }
}

class Example extends StatelessWidget {
  Widget build(BuildContext context) {
    final SwitchProvider _state = Provider.of<SwitchProvider>(context);
    return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text(
              "Homework Provider",
            ),
            backgroundColor: Color(
              generateRandomHexColor(),
            ),
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                color: Color(
                  generateRandomHexColor(),
                ),
                height: 200,
                width: 200,
              ),
              Consumer(
                builder: (context, value, child) => Switch(
                  value: _state.is_switched,
                  onChanged: (_) => _state._switch(),
                  // onChanged: (bool is_changed) => !is_changed,
                ),
              ),
            ],
          ),
        );
  }
}

Random random = new Random();
int generateRandomHexColor() {
  int length = 6;
  String chars = '0123456789ABCDEF';
  String hex = '';
  while (length-- > 0) hex  = chars[(random.nextInt(16)) | 0];
  print(hex);
  return int.parse("0xFF"   hex);
}
  • Related